[PATCH 4/9] kallsyms: do not call qsort() with a NULL table

From: lzhan011

Date: Mon Oct 05 2026 - 06:41:46 EST


From: lzhan011 <zhangleizhen645@xxxxxxxxx>

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: lzhan011 <zhangleizhen645@xxxxxxxxx>
---
scripts/kallsyms.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
index bd9e9ce20..9300e10ad 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