From 41aed7a38a928863d5759ac2a93251d35aa1f657 Mon Sep 17 00:00:00 2001 From: Fergus Dall Date: Fri, 23 Sep 2022 07:58:13 +1000 Subject: [PATCH] scanner: Fix undefined behavior around qsort According to clang, qsort cannot be passed a null pointer, even if the size is specified to be zero. The scanner can hit this while trying to sort forward declarations if it happens to be building a protocol file that doesn't require any, either in the header or the source. Signed-off-by: Fergus Dall --- src/scanner.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/scanner.c b/src/scanner.c index da8adea..c512d23 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -1634,7 +1634,9 @@ emit_header(struct protocol *protocol, enum side side) *p = i->name; } - qsort(types.data, types.size / sizeof *p, sizeof *p, cmp_names); + if (types.size > 0) + qsort(types.data, types.size / sizeof *p, sizeof *p, cmp_names); + prev = NULL; wl_array_for_each(p, &types) { if (prev && strcmp(*p, prev) == 0) @@ -1844,7 +1846,10 @@ emit_code(struct protocol *protocol, enum visibility vis) emit_types_forward_declarations(protocol, &i->request_list, &types); emit_types_forward_declarations(protocol, &i->event_list, &types); } - qsort(types.data, types.size / sizeof *p, sizeof *p, cmp_names); + + if (types.size > 0) + qsort(types.data, types.size / sizeof *p, sizeof *p, cmp_names); + prev = NULL; wl_array_for_each(p, &types) { if (prev && strcmp(*p, prev) == 0) -- 2.7.4