From nobody Sat Feb 7 13:45:49 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57200C77B73 for ; Mon, 5 Jun 2023 12:26:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233395AbjFEM0R (ORCPT ); Mon, 5 Jun 2023 08:26:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233391AbjFEM0P (ORCPT ); Mon, 5 Jun 2023 08:26:15 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A389E6; Mon, 5 Jun 2023 05:26:12 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id EA0E062343; Mon, 5 Jun 2023 12:26:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BB104C433EF; Mon, 5 Jun 2023 12:26:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685967971; bh=96gl5J71hpxGYTZSluEX9KNvDN+//VJ4LSUDHkq6+E0=; h=From:To:Cc:Subject:Date:From; b=XnlHVXRTZphIw2VPrOzxCfSjEVVroDblhxmXrC28Qt8f6+KRN8HATipKW6BkjQ4iO kKhJ0CXLaHMTxi2PSGjg+ujshBV31ZWelUZoDd8IX9wgX3rgfzihMX66wzJ9vZmsOf 9Kuzzxf0AMfKHlM4JEr0ZlTbyO64pVx6SUAvXcJ8eDYt6tVt3tnJfWTyxh2TiRmjyQ alHwGuNJ2G6fIKv+J67y4onlL17FkuPbFLjvG5upnin4VkQgabXDPU8qN1IFKasDJA bKvrtwXEdKzKZhWpMKCBWtV1QrCIjimzJvrxws4vqeI5L0rQAjScDxuU9qjYdaa++7 zFNPkoDeZxdhA== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Boqun Feng , Miguel Ojeda , Masahiro Yamada Subject: [PATCH] scripts/kallsyms: remove KSYM_NAME_LEN_BUFFER Date: Mon, 5 Jun 2023 21:26:04 +0900 Message-Id: <20230605122604.1826856-1-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" You do not need to decide the buffer size statically. Use getline() to grow the line buffer as needed. Signed-off-by: Masahiro Yamada Reviewed-by: Nicolas Schier --- scripts/kallsyms.c | 61 ++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 0d2db41177b2..fc9709839b19 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -19,6 +19,7 @@ * */ =20 +#include #include #include #include @@ -29,24 +30,8 @@ =20 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) =20 -#define _stringify_1(x) #x -#define _stringify(x) _stringify_1(x) - #define KSYM_NAME_LEN 512 =20 -/* - * A substantially bigger size than the current maximum. - * - * It cannot be defined as an expression because it gets stringified - * for the fscanf() format string. Therefore, a _Static_assert() is - * used instead to maintain the relationship with KSYM_NAME_LEN. - */ -#define KSYM_NAME_LEN_BUFFER 2048 -_Static_assert( - KSYM_NAME_LEN_BUFFER =3D=3D KSYM_NAME_LEN * 4, - "Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN" -); - struct sym_entry { unsigned long long addr; unsigned int len; @@ -136,24 +121,40 @@ static void check_symbol_range(const char *sym, unsig= ned long long addr, } } =20 -static struct sym_entry *read_symbol(FILE *in) +static struct sym_entry *read_symbol(FILE *in, char **buf, size_t *buf_len) { - char name[KSYM_NAME_LEN_BUFFER+1], type; + char *name, type, *p; unsigned long long addr; - unsigned int len; + size_t len; + ssize_t readlen; struct sym_entry *sym; - int rc; =20 - rc =3D fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &ad= dr, &type, name); - if (rc !=3D 3) { - if (rc !=3D EOF && fgets(name, ARRAY_SIZE(name), in) =3D=3D NULL) - fprintf(stderr, "Read error or end of file.\n"); + readlen =3D getline(buf, buf_len, in); + if (readlen < 0) { + if (errno) { + perror("read_symbol"); + exit(EXIT_FAILURE); + } return NULL; } - if (strlen(name) >=3D KSYM_NAME_LEN) { + + if ((*buf)[readlen - 1] =3D=3D '\n') + (*buf)[readlen - 1] =3D 0; + + addr =3D strtoull(*buf, &p, 16); + + if (*buf =3D=3D p || *p++ !=3D ' ' || !isascii((type =3D *p++)) || *p++ != =3D ' ') { + fprintf(stderr, "line format error\n"); + exit(EXIT_FAILURE); + } + + name =3D p; + len =3D strlen(name); + + if (len >=3D KSYM_NAME_LEN) { fprintf(stderr, "Symbol %s too long for kallsyms (%zu >=3D %d).\n" "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n", - name, strlen(name), KSYM_NAME_LEN); + name, len, KSYM_NAME_LEN); return NULL; } =20 @@ -169,8 +170,7 @@ static struct sym_entry *read_symbol(FILE *in) =20 /* include the type field in the symbol name, so that it gets * compressed together */ - - len =3D strlen(name) + 1; + len++; =20 sym =3D malloc(sizeof(*sym) + len + 1); if (!sym) { @@ -257,6 +257,8 @@ static void read_map(const char *in) { FILE *fp; struct sym_entry *sym; + char *buf =3D NULL; + size_t buflen =3D 0; =20 fp =3D fopen(in, "r"); if (!fp) { @@ -265,7 +267,7 @@ static void read_map(const char *in) } =20 while (!feof(fp)) { - sym =3D read_symbol(fp); + sym =3D read_symbol(fp, &buf, &buflen); if (!sym) continue; =20 @@ -284,6 +286,7 @@ static void read_map(const char *in) table[table_cnt++] =3D sym; } =20 + free(buf); fclose(fp); } =20 --=20 2.39.2