builtin: add sanity check on the number of returned frames by C.backtrace

pull/6168/head
Delyan Angelov 2020-08-06 19:50:25 +03:00
parent 3a461e7cee
commit 4568ce8f00
2 changed files with 17 additions and 1 deletions

View File

@ -50,6 +50,10 @@ fn print_backtrace_skipping_top_frames_mac(skipframes int) bool {
$if macos { $if macos {
buffer := [100]byteptr buffer := [100]byteptr
nr_ptrs := C.backtrace(buffer, 100) nr_ptrs := C.backtrace(buffer, 100)
if nr_ptrs < 2 {
eprintln('C.backtrace returned less than 2 frames')
return false
}
C.backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2) C.backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2)
} }
return true return true
@ -59,6 +63,10 @@ fn print_backtrace_skipping_top_frames_freebsd(skipframes int) bool {
$if freebsd { $if freebsd {
buffer := [100]byteptr buffer := [100]byteptr
nr_ptrs := C.backtrace(buffer, 100) nr_ptrs := C.backtrace(buffer, 100)
if nr_ptrs < 2 {
eprintln('C.backtrace returned less than 2 frames')
return false
}
C.backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2) C.backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2)
} }
return true return true
@ -81,6 +89,10 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
} }
buffer := [100]byteptr buffer := [100]byteptr
nr_ptrs := C.backtrace(buffer, 100) nr_ptrs := C.backtrace(buffer, 100)
if nr_ptrs < 2 {
eprintln('C.backtrace returned less than 2 frames')
return false
}
nr_actual_frames := nr_ptrs - skipframes nr_actual_frames := nr_ptrs - skipframes
mut sframes := []string{} mut sframes := []string{}
//////csymbols := backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames) //////csymbols := backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames)

View File

@ -103,10 +103,14 @@ $if msvc {
syminitok := C.SymInitialize( handle, 0, 1) syminitok := C.SymInitialize( handle, 0, 1)
if syminitok != 1 { if syminitok != 1 {
eprintln('Failed getting process: Aborting backtrace.\n') eprintln('Failed getting process: Aborting backtrace.\n')
return true return false
} }
frames := int(C.CaptureStackBackTrace(skipframes + 1, 100, backtraces, 0)) frames := int(C.CaptureStackBackTrace(skipframes + 1, 100, backtraces, 0))
if frames < 2 {
eprintln('C.CaptureStackBackTrace returned less than 2 frames')
return false
}
for i in 0..frames { for i in 0..frames {
frame_addr := backtraces[i] frame_addr := backtraces[i]
if C.SymFromAddr(handle, frame_addr, &offset, si) == 1 { if C.SymFromAddr(handle, frame_addr, &offset, si) == 1 {