[PATCH v3 2/6] kallsyms: do not call qsort() with a NULL table
From: Leizhen Zhang
Date: Mon Nov 09 2026 - 19:08:01 EST
link-vmlinux.sh runs kallsyms on an empty input for the first link, in
which case table is NULL and table_cnt is 0. Passing NULL to qsort() is
undefined behaviour even for zero elements, and UBSan reports:
scripts/kallsyms.c: runtime error: null pointer passed as argument 1,
which is declared to never be null
Skip the sort when there are no symbols.
The problem was found with UBSan, and the analysis and fix were
prepared with the help of an AI coding assistant; I reviewed and
tested the result.
Fixes: c442db3f49f2 ("kbuild: remove PROVIDE() for kallsyms symbols")
Signed-off-by: Leizhen Zhang <lzsx618@xxxxxxxxx>
---
scripts/kallsyms.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
index ecb444078c1c..4bbf2e65cb38 100644
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -389,7 +389,8 @@ static int compare_names(const void *a, const void *b)
static void sort_symbols_by_name(void)
{
- qsort(table, table_cnt, sizeof(table[0]), compare_names);
+ if (table_cnt)
+ qsort(table, table_cnt, sizeof(table[0]), compare_names);
}
static void write_src(FILE *out_bin_file, const char *out_bin_name)
@@ -819,7 +820,8 @@ static int compare_symbols(const void *a, const void *b)
static void sort_symbols(void)
{
- qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
+ if (table_cnt)
+ qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
}
int main(int argc, char **argv)
--
2.34.1