[PATCH v4 2/2] stdio-common: Fix buffer overflow in scanf %mc [BZ #34008]
Rocket Ma
marocketbd@gmail.com
Mon Apr 6 16:17:50 GMT 2026
Florian Weimer <fw@deneb.enyo.de> 于2026年4月6日周一 17:00写道:
> The last (extra) argument is constant. I'd suggest two functions with
> descriptive names instead (maybe grow_to_fit_for_fixed for the %c
> family, and grow_to_fit_with_null for the %s/%[] family that perform
> null termination). The new functions probably shouldn't be inline, so
> that the compiler can apply its heuristics.
If the function need to be separated, then the old behavior, "size_t
newsize = strsize + (strsize >= width ? width : strsize)", is not
worth a new function. And the function only expands to several
instructions, less than 10, observed via Compiler Explorer. I think
it's OK to inline the function.
> As written, the interaction with the extra argument and a negative
> need argument is not quite obvious from the function alone (code and
> comments). The function seems correct because need (called width in
> the caller) can only be -1 for the %s/%[] case, where extra is 1.
Since the behavior mentioned has become some sort of convention, the
function should be OK? Readers has the constant to distinguish between
%ms and %mc, and they can understand the code via enough comments.
Personally I think it's worth to put these two actions together to do
one thing: calculate the size of expanded array.
More information about the Libc-alpha
mailing list