[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How does string.format handle undefined behavior?
- From: Lorenzo Donati <lorenzodonatibz@...>
- Date: Wed, 8 Sep 2021 19:39:24 +0200
On 08/09/2021 18:35, Roberto Ierusalimschy wrote:
I completely agree with Roberto that the sentence is confusing. And
(I guess) especially for us non-native English speakers (Roberto
and I, I mean), because, as you pointed out, it is grammatically
incorrect. At least the first comma should be omitted.
A more plausible explanation is that the grammar is fine, it says
what it wants to say, and you are undestanding it correctly, but
nevertheless you want to understand something else. :-)
Scrutinizing once again the standard, I found this:
4. Conformance [...] 2 If a ''shall'' or ''shall not'' requirement
that appears outside of a constraint is violated, the behavior is
undefined. Undefined behavior is otherwise indicated in this
International Standard by the words ‘‘undefined behavior’’ or by the
omission of any explicit definition of behavior. There is no
difference in emphasis among these three; they all describe
‘‘behavior that is undefined’’.
It says with all words that the omission of any explicit definition
of behavior is as "splorky" as the words "undefined behavior".
Urgh! I definitely missed that! So I stand corrected. Back to square
Now I have to reconcile in my head this fact and the fact that people
much more expert than me keep on focusing on enumerating all the source
of UB as there were no other ones.
Even the standard plays this game with annex J (I know it's informative
and not normative, however...). But I admit that, reading that carefully
now, at the beginning it says (J.2):
"The behavior is undefined in the following circumstances:"
"The behavior is undefined ONLY in the following circumstances:"
but I always interpreted it in this latter way. Uff!
<...*browses his PDF collection*...>
Maybe an explanation is what I found in the book by Derek M. Jones (a
former C committee member and compiler expert) "The New C Standard - An
Economic and Cultural Commentary" (freely available online here
It addresses the C99 standard (the language part only), commenting every
sentence in the perspective of gaining insight in how to choose
Commenting that sentence in the "conformance" section it states the
following (clause n.85 of the book at page 205 and 206 of the PDF):
"Undefined behavior is otherwise indicated in this International
Standard by the words “undefined behavior” or undefined behavior
indicated by the omission of any explicit definition of behavior."
Failure to find an explicit definition of behavior could, of course, be
because the reader did not look hard enough. Or it could be because
there was nothing to find, implicitly undefined behavior. On the whole
the Committee does not seem to have made any obvious omissions of
definitions of behavior. Those DRs that have been submitted to WG14,
which have later turned out to be implicitly undefined behavior, have
involved rather convoluted constructions. This specification for the
omissions of an explicit definition is more of a catch-all rather than
an intent to minimize wording in the standard (although your author has
heard some Committee members express the view that it was never the
intent to specify every detail).
So this confirms the words of the standard in that "implicit UB" by
omission is a thing, but states that the intent of the committee was to
really enumerate explicitly every source of UB and left the "implicit
UB" there just as a fallback for something they might have not caught,
although Mr.Jones is careful not to state his interpretation of the
committee's intent as 100% sure.
As I said in a previous message, I keep on getting puzzled by C and its
standard. Oh well! At least I learned something new today, even if it is
another "small-print paragraph" in the C standard! :-)