diff options
author | Fergus Dall <sidereal@google.com> | 2022-09-23 07:58:13 +1000 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2023-01-03 11:05:35 +0000 |
commit | 41aed7a38a928863d5759ac2a93251d35aa1f657 (patch) | |
tree | 89bf65d07a8bd179508a72d7de9ffa0c6b814b08 | |
parent | 7f1c51a55613350488fa88f283462fca18f533a1 (diff) | |
download | wayland-41aed7a38a928863d5759ac2a93251d35aa1f657.tar.gz |
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 <sidereal@google.com>
-rw-r--r-- | src/scanner.c | 9 |
1 files 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) |