From ee7615e1f3384c36e41fdf176abdabf8f21d1505 Mon Sep 17 00:00:00 2001 From: Andreas Arnez Date: Tue, 12 Nov 2013 18:03:54 +0100 Subject: [PATCH] Fix GDB crash with upstream GCC due to qsort(NULL, ...) Upstream GCC's new pass '-fisolate-erroneous-paths' may introduce traps at places where GCC has determined undefined behavior, e.g. when passing a NULL pointer to a function that defines this argument as __attribute__(__nonnull__(...)). In particular this applies to uniquify_strings(), because it invokes qsort() with NULL when the 'strings' vector is empty. I hit this problem on s390x when trying to execute "break main" on a C program. gdb/ 2013-11-12 Andreas Arnez * objc-lang.c (uniquify_strings): Prevent invoking qsort with NULL. --- gdb/ChangeLog | 5 +++++ gdb/objc-lang.c | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 654183738c8..6ad4b49448c 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2013-11-12 Andreas Arnez + + * objc-lang.c (uniquify_strings): Prevent invoking qsort with + NULL. + 2013-11-12 Doug Evans Work around gold/15646. diff --git a/gdb/objc-lang.c b/gdb/objc-lang.c index 77a61acc057..5c7234035f9 100644 --- a/gdb/objc-lang.c +++ b/gdb/objc-lang.c @@ -1056,6 +1056,11 @@ uniquify_strings (VEC (const_char_ptr) **strings) const char *elem, *last = NULL; int out; + /* If the vector is empty, there's nothing to do. This explicit + check is needed to avoid invoking qsort with NULL. */ + if (VEC_empty (const_char_ptr, *strings)) + return; + qsort (VEC_address (const_char_ptr, *strings), VEC_length (const_char_ptr, *strings), sizeof (const_char_ptr), -- 2.30.2