Szabolcs Nagy <
n...@port70.net> writes:
> James Kuyper <
james...@verizon.net> wrote:
>> On 08/26/2012 11:45 AM, Szabolcs Nagy wrote:
>>> 7.21.6.2p10:
>>> "... if the result of the conversion cannot be
>>> represented in the object, the behavior is undefined."
>>>
>>> the last line here could be interpreted
>>> in a way that if the result is too big
>>> for a signed int then the behaviour is
>>> undefined
>>
>> That's correct.
>
> but then scanf is no better than gets
>
> actually it's even worse as bad input
> can invoke undefined behaviour in
> unexpected ways
It's not as bad as gets(). scanf() *can* be used safely, if you're
sufficiently careful about the format string; for example "%20s" is ok
as long as the target array is big enough. (And sscanf is safe if you
exercise control over the input string.)
But yes, it's very easy to use it unsafely.
> and many of the scanf examples promote
> the use of %d and none of them mention
> anything about undefined behaviour
>
> and in annex I, where the undefined
> behaviours are collected, this particular
> item is not listed
That's in J.2, at least as of N1570, the latest draft. Yes, I agree
that that's a serious oversight.
> and fscanf_s in annex K, which tries to
> "mitigate security vulnerabilities", does
> not address this issue either
>
> these suggest me that the ub interpretation
> is not the right one, otherwise at least
> there should be a note about it
But I can't think of another interpretation for N1370 7.21.6.2p10:
If this object does not have an appropriate type, or if the
result of the conversion cannot be represented in the object,
the behavior is undefined.
I would *love* to see this corrected.
One solution would be to say that if the result cannot be represented
in the object, it's treated as a matching failure. Another would be
to say that an implementation-defined value is stored in the object
(but that would make it difficult to detect the problem).
--
Keith Thompson (The_Other_Keith)
ks...@mib.org <
http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"