-1

Follow-up for:

Is the following table correct?

                  ===== leads to UB ====
program           compile time  run time
well-formed       never         may
ill-formed        may           may
pmor
  • 5,392
  • 4
  • 17
  • 36
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/225440/discussion-on-question-by-pmor-is-it-true-that-ill-formed-program-does-not-alway). – Samuel Liew Dec 03 '20 at 01:07

1 Answers1

2

If a program is ill-formed, a conforming implementation which has adequate resources to process it would be required to issue a diagnostic. Once an implementation has done so, anything it might do after that would be outside the Standard's jurisdiction. Some possible outcomes would be:

  1. Terminate processing after outputting the diagnostic.

  2. After outputting the diagnostic, proceed as though the language were extended to allow whatever constructs would otherwise make the program ill-formed.

  3. Run the last successfully-compiled version of the program, whose behavior might bear no relationship to anything in the current source files.

If an construct is characterized as "Ill-formed; no diagnostic required", all of the above remain possible in addition to a fourth possibility, namely proceeding as though the language were extended to allow the construct without bothering to output a diagnostic first.

Since the Standard only seeks to describe program behavior in terms of the current contents of the source files, and not anything they might have contained at some time in the past, it cannot meaningfully describe might happen if an implementation is fed a ill-formed source file. That doesn't imply that quality implementations shouldn't seek to avoid accidental execution of obsolete versions of programs, but any measures they might take toward that end would be outside the Standard's jurisdiction.

Note that on some implementations that target embedded or remote systems, option #3 may not be nearly as absurd as it sounds. In many cases, a build-and-execute cycle would involve stopping execution on the remote system, feeding it new code, and then restarting execution on that system. If no new code image is available, restarting whatever code image was previously present may in many cases may be more useful than leaving the system in a dead state, but there's no way the Standard can impose anything meaningful about what that program does, implying that its execution as a consequence of the failure to build the new program cannot be characterized as anything other than Undefined Behavior.

supercat
  • 77,689
  • 9
  • 166
  • 211
  • What about "ill-formed, no diagnostic required" cases? Consider a sample C++ program with two expressions, which are functionally equivalent but not equivalent and, hence, "ill-formed, no diagnostic required" (N4713, 17.6.6.1p5). These expressions render the entire program ill-formed. However, if these expressions are not used (i.e. referred) in the rest of the program, what are the chances to get UB (either at CT or at RT)? Hence, in the table I've drawn the conclusion that ill-formed program does not always lead to UB (either at CT or at RT). – pmor Dec 02 '20 at 23:35
  • @pmor: Well, I thought the phrase "no diagnostic required" was sufficiently self-explanatory that I didn't need to explicitly specify that an implementation may at its leisure skip the diagnostic, but may also do anything it could do with any other ill-formed program, including #3 above. Note that especially in the embedded world, #3 is hardly contrived. If a dev system takes control of a target board before it tries to load a compiled program, and needs to release control of the target board before it exits, and if target platform's memory can only be reprogrammed a few hundred... – supercat Dec 02 '20 at 23:51
  • 1
    ...or a few thousand times before it wears out, it may be desirable for the dev system not to disturb the target system's flash memory if it doesn't have anything useful to put there. When it releases control, however, the target system will run whatever was last programmed into it. – supercat Dec 02 '20 at 23:53