bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Some compile-time warnings


From: Martin D Kealey
Subject: Re: Some compile-time warnings
Date: Tue, 30 Sep 2025 17:03:20 +1000

On Tue, 30 Sept 2025 at 03:22, Chet Ramey <chet.ramey@case.edu> wrote:

> On 9/29/25 1:00 AM, Martin D Kealey wrote:
>
> > The malloc size warning is concerning, as the excessive “size” is
> > (uint64_t)(-1).
>
> It seems theoretical,


Sure, since

I guess realloc(p,0) and realloc(p,-1) are both likely to return NULL,
except the latter will set errno to ENOMEM.

The anomaly occurs in shell_getc in parse.y. It appears because new
compilers can perform constant folding between statements, and then
complain about value ranges that are more stringent that what's implied by
the parameter types:

2575:          if (shell_input_line_size > (SIZE_MAX - 256))
…
2587:              if (shell_input_line_size < SIZE_MAX)
2588:                {
2589:                  shell_input_line_size = SIZE_MAX;
2590:                  shell_input_line = xrealloc (shell_input_line,
*shell_input_line_size*);

The fallback value of SIZE_MAX appears in typemax.h:
#ifndef SIZE_MAX
#  define SIZE_MAX      ((size_t) ~(size_t)0)
#endif
which is the same as typical values from <limits.h>, and is numerically
identical to my earlier suggestion.

C11 defined a new macro RSIZE_MAX (see annex §K.3.4) as the run-time
maximum size; this is likely to be significantly smaller than SIZE_MAX,
since the latter simply expresses the maximum value that can be stored in a
size_t variable, rather than the actual maximum object size. For reasons I
don't quite follow, the committee left the definitions of malloc & realloc
as taking size_t rather than rsize_t, perhaps because they clearly signal
failure by returning NULL, so the option of undefined behaviour was not
necessary.

Attached is one possible resolution.

-Martin

Attachment: rsize_max.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]