Skip to content

Commit

Permalink
builtin: add sanity check on the number of returned frames by C.backt…
Browse files Browse the repository at this point in the history
…race
  • Loading branch information
spytheman committed Aug 6, 2020
1 parent 3a461e7 commit 4568ce8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions vlib/builtin/builtin_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ fn print_backtrace_skipping_top_frames_mac(skipframes int) bool {
$if macos {
buffer := [100]byteptr
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)
}
return true
Expand All @@ -59,6 +63,10 @@ fn print_backtrace_skipping_top_frames_freebsd(skipframes int) bool {
$if freebsd {
buffer := [100]byteptr
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)
}
return true
Expand All @@ -81,6 +89,10 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
}
buffer := [100]byteptr
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
mut sframes := []string{}
//////csymbols := backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames)
Expand Down
6 changes: 5 additions & 1 deletion vlib/builtin/builtin_windows.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ $if msvc {
syminitok := C.SymInitialize( handle, 0, 1)
if syminitok != 1 {
eprintln('Failed getting process: Aborting backtrace.\n')
return true
return false
}

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 {
frame_addr := backtraces[i]
if C.SymFromAddr(handle, frame_addr, &offset, si) == 1 {
Expand Down

0 comments on commit 4568ce8

Please sign in to comment.