summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFergus Dall <sidereal@google.com>2022-09-23 07:58:13 +1000
committerSimon Ser <contact@emersion.fr>2023-01-03 11:05:35 +0000
commit41aed7a38a928863d5759ac2a93251d35aa1f657 (patch)
tree89bf65d07a8bd179508a72d7de9ffa0c6b814b08
parent7f1c51a55613350488fa88f283462fca18f533a1 (diff)
downloadwayland-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.c9
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)