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>
This commit is contained in:
Fergus Dall 2022-09-23 07:58:13 +10:00 committed by Simon Ser
parent 7f1c51a556
commit 41aed7a38a

View File

@ -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)