This patch to libbacktrace handles short reads correctly. Short reads are unlikely, but they can reportedly happen when the debug sections are very large and we aren't using mmap. Bootstrapped and ran libbacktrace tests on x86_64-pc-linux-gnu. Committed to mainline.
Ian 2020-05-09 Ian Lance Taylor <i...@golang.org> * read.c (backtrace_get_view): Support short read.
diff --git a/libbacktrace/read.c b/libbacktrace/read.c index 57e4701bbeb..1a6052bf613 100644 --- a/libbacktrace/read.c +++ b/libbacktrace/read.c @@ -50,7 +50,8 @@ backtrace_get_view (struct backtrace_state *state, int descriptor, backtrace_error_callback error_callback, void *data, struct backtrace_view *view) { - ssize_t got; + uint64_t got; + ssize_t r; if ((uint64_t) (size_t) size != size) { @@ -70,15 +71,22 @@ backtrace_get_view (struct backtrace_state *state, int descriptor, view->data = view->base; view->len = size; - got = read (descriptor, view->base, size); - if (got < 0) + got = 0; + while (got < size) { - error_callback (data, "read", errno); - free (view->base); - return 0; + r = read (descriptor, view->base, size - got); + if (r < 0) + { + error_callback (data, "read", errno); + free (view->base); + return 0; + } + if (r == 0) + break; + got += (uint64_t) r; } - if ((size_t) got < size) + if (got < size) { error_callback (data, "file too short", 0); free (view->base);