Skip to content

Commit d80081e

Browse files
committed
Catch overflow in gas s_space
Also fix an error introduced in 1998 in reporting a zero count for negative counts. * read.c (s_space): Use unsigned multiply, and catch overflow. Correct order of tests for invalid repeat counts. Ensure ignored directives don't affect mri_pending_align.
1 parent d09f4d4 commit d80081e

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

gas/read.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3328,27 +3328,37 @@ s_space (int mult)
33283328

33293329
if (exp.X_op == O_constant)
33303330
{
3331-
offsetT repeat;
3331+
addressT repeat = exp.X_add_number;
3332+
addressT total;
33323333

3333-
repeat = exp.X_add_number;
3334-
if (mult)
3335-
repeat *= mult;
3336-
bytes = repeat;
3337-
if (repeat <= 0)
3334+
bytes = 0;
3335+
if ((offsetT) repeat < 0)
3336+
{
3337+
as_warn (_(".space repeat count is negative, ignored"));
3338+
goto getout;
3339+
}
3340+
if (repeat == 0)
33383341
{
33393342
if (!flag_mri)
33403343
as_warn (_(".space repeat count is zero, ignored"));
3341-
else if (repeat < 0)
3342-
as_warn (_(".space repeat count is negative, ignored"));
33433344
goto getout;
33443345
}
3346+
if ((unsigned int) mult <= 1)
3347+
total = repeat;
3348+
else if (gas_mul_overflow (repeat, mult, &total)
3349+
|| (offsetT) total < 0)
3350+
{
3351+
as_warn (_(".space repeat count overflow, ignored"));
3352+
goto getout;
3353+
}
3354+
bytes = total;
33453355

33463356
/* If we are in the absolute section, just bump the offset. */
33473357
if (now_seg == absolute_section)
33483358
{
33493359
if (val.X_op != O_constant || val.X_add_number != 0)
33503360
as_warn (_("ignoring fill value in absolute section"));
3351-
abs_section_offset += repeat;
3361+
abs_section_offset += total;
33523362
goto getout;
33533363
}
33543364

@@ -3358,13 +3368,13 @@ s_space (int mult)
33583368
if (mri_common_symbol != NULL)
33593369
{
33603370
S_SET_VALUE (mri_common_symbol,
3361-
S_GET_VALUE (mri_common_symbol) + repeat);
3371+
S_GET_VALUE (mri_common_symbol) + total);
33623372
goto getout;
33633373
}
33643374

33653375
if (!need_pass_2)
33663376
p = frag_var (rs_fill, 1, 1, (relax_substateT) 0, (symbolS *) 0,
3367-
(offsetT) repeat, (char *) 0);
3377+
(offsetT) total, (char *) 0);
33683378
}
33693379
else
33703380
{

0 commit comments

Comments
 (0)