Skip to content

Commit 4c45463

Browse files
fbqojeda
authored andcommitted
kallsyms: avoid hardcoding the buffer size
This makes it easier to update the size later on. Furthermore, a static assert is added to ensure both are updated when that happens. The relationship used is one that keeps the new size (512+1) close to the original buffer size (500). Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Boqun Feng <[email protected]> Co-developed-by: Miguel Ojeda <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 672c0c5 commit 4c45463

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

scripts/kallsyms.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,18 @@
2727

2828
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
2929

30+
#define _stringify_1(x) #x
31+
#define _stringify(x) _stringify_1(x)
32+
3033
#define KSYM_NAME_LEN 128
3134

35+
/* A substantially bigger size than the current maximum. */
36+
#define KSYM_NAME_LEN_BUFFER 512
37+
_Static_assert(
38+
KSYM_NAME_LEN_BUFFER == KSYM_NAME_LEN * 4,
39+
"Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN"
40+
);
41+
3242
struct sym_entry {
3343
unsigned long long addr;
3444
unsigned int len;
@@ -197,15 +207,15 @@ static void check_symbol_range(const char *sym, unsigned long long addr,
197207

198208
static struct sym_entry *read_symbol(FILE *in)
199209
{
200-
char name[500], type;
210+
char name[KSYM_NAME_LEN_BUFFER+1], type;
201211
unsigned long long addr;
202212
unsigned int len;
203213
struct sym_entry *sym;
204214
int rc;
205215

206-
rc = fscanf(in, "%llx %c %499s\n", &addr, &type, name);
216+
rc = fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &addr, &type, name);
207217
if (rc != 3) {
208-
if (rc != EOF && fgets(name, 500, in) == NULL)
218+
if (rc != EOF && fgets(name, sizeof(name), in) == NULL)
209219
fprintf(stderr, "Read error or end of file.\n");
210220
return NULL;
211221
}

0 commit comments

Comments
 (0)