On 5/12/2012 11:00 AM, Krzysztof Żelechowski wrote:
> TR24731, §5.6.3.1 brings a code sample that says { printf (argv); },
> observes that it may leak information and concludes that printf is wrong and
> it must be fixed. How does the introduction of printf_s fix this particular
> case?
ITYM the Rationale, not the document, and 6.5.3.1 (as in your
Subject: line), not 5.6.3.1.
The program fragment is certainly a poor choice, since even a
C89 implementation is obligated to issue a diagnostic for it. As far
as I can see, substituting printf_s solves nothing: The diagnostic is
still required, and if the program executes anyhow the behavior is
still undefined (hence, could be just as undesirable).
> Further it examines the risks behind %n and argues that it can be used to
> modify an arbitrary location in memory because ‘the corresponding argument
> might be the literal characters in the format itself’. How could that
> happen?
The explanation given seems pretty clear, but also far-fetched.
"Since the format itself may be on the stack" is unlikely if the
format is some coercion of `argv' to `char*'.
But really, the `argv' thing is just an ill-thought-out red
herring. We can imagine a truly careless programmer doing something
along the lines of
char buff[500];
fgets(buff, sizeof buff, stdin);
printf(buff);
... which has all the dangers described in the Rationale, and others
besides. The proposed printf_s would address a few of them, but by
no means all.
--
Eric Sosman
eso...@ieee-dot-org.invalid