Skip to content

Commit 72d225e

Browse files
committed
binutils stabs type list
Fuzzers have found that specifying a large stab type number results in lots of memory being requested, as the list is extended with a 16 element array at a time until we reach the given stab type. It also takes a long time. Of course normal sane stab types use small positive integers, but it's not hard to modify the code to handle type numbers starting anyhere. * stabs.c (struct stab_types): Add base_index. (stab_find_slot): Simplify filenum check. Delete type number check. Don't allocate entire array from 0 to type number, allocate a sparse array.
1 parent 3cd0b4f commit 72d225e

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

binutils/stabs.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ struct stab_types
121121
{
122122
/* Next set of slots for this file. */
123123
struct stab_types *next;
124+
/* Where the TYPES array starts. */
125+
unsigned int base_index;
124126
/* Types indexed by type number. */
125127
#define STAB_TYPES_SLOTS (16)
126128
debug_type types[STAB_TYPES_SLOTS];
@@ -3413,40 +3415,32 @@ stab_emit_pending_vars (void *dhandle, struct stab_handle *info)
34133415
static debug_type *
34143416
stab_find_slot (struct stab_handle *info, const int *typenums)
34153417
{
3416-
int filenum;
3417-
int tindex;
3418+
unsigned int filenum;
3419+
unsigned int tindex;
3420+
unsigned int base_index;
34183421
struct stab_types **ps;
34193422

34203423
filenum = typenums[0];
34213424
tindex = typenums[1];
34223425

3423-
if (filenum < 0 || (unsigned int) filenum >= info->files)
3426+
if (filenum >= info->files)
34243427
{
34253428
fprintf (stderr, _("Type file number %d out of range\n"), filenum);
34263429
return NULL;
34273430
}
3428-
if (tindex < 0)
3429-
{
3430-
fprintf (stderr, _("Type index number %d out of range\n"), tindex);
3431-
return NULL;
3432-
}
34333431

34343432
ps = info->file_types + filenum;
3433+
base_index = tindex / STAB_TYPES_SLOTS * STAB_TYPES_SLOTS;
3434+
tindex -= base_index;
3435+
while (*ps && (*ps)->base_index < base_index)
3436+
ps = &(*ps)->next;
34353437

3436-
while (tindex >= STAB_TYPES_SLOTS)
3437-
{
3438-
if (*ps == NULL)
3439-
{
3440-
*ps = (struct stab_types *) xmalloc (sizeof **ps);
3441-
memset (*ps, 0, sizeof **ps);
3442-
}
3443-
ps = &(*ps)->next;
3444-
tindex -= STAB_TYPES_SLOTS;
3445-
}
3446-
if (*ps == NULL)
3438+
if (*ps == NULL || (*ps)->base_index != base_index)
34473439
{
3448-
*ps = (struct stab_types *) xmalloc (sizeof **ps);
3449-
memset (*ps, 0, sizeof **ps);
3440+
struct stab_types *n = xcalloc (1, sizeof (*n));
3441+
n->next = *ps;
3442+
n->base_index = base_index;
3443+
*ps = n;
34503444
}
34513445

34523446
return (*ps)->types + tindex;

0 commit comments

Comments
 (0)