[PATCH v2 3/8] kallsyms: do not call qsort() with a NULL table
From: Leizhen Zhang
Date: Thu Oct 08 2026 - 12:41:07 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.
Fixes: c442db3f49f2 ("kbuild: remove PROVIDE() for kallsyms symbols")
Assisted-by: Claude:claude-opus-5-5 UBSan
Signed-off-by: Leizhen Zhang <lzsx618@xxxxxxxxx>
---
v2:
- Use my real name in the From and Signed-off-by lines. No code
changes.
v1: https://lore.kernel.org/r/20261005104050.1786222-5-lzsx618@xxxxxxxxx
scripts/kallsyms.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
index bd9e9ce20e..9300e10ad6 100644
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -390,7 +390,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)
@@ -822,7 +823,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