Can an implementation specify undefined behavior

3.4.1
1 implementation-defined behavior
undefined behavior when each implementation documents how choices are made

Can an implementation indicate that the behavior defined by the implementation is undefined behavior in cases where undefined behavior is a possible result?

For instance:

6.3.1.3 Integer and unsigned integers 3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is determined by the implementation or a signal determined by the implementation is generated.

As long as it is documented, can this result be undefined and invoke undefined behavior, or should it have a specific result for this implementation?

+3
source share
4 answers

No, semantically this is not possible. Undefined behavior is what the term means, behavior that is not defined by the standard. If a standard requests an implementation defined by a behavior, it explicitly requests an implementation to indicate what it does when some error occurs.

undefined behavior

when using an intolerable or erroneous program or erroneous data for which this International Standard does not impose any requirements

Thus, the implementation may say: “in this case, and in these circumstances, this implementation causes a commercial signal”, but it cannot say “under this and those circumstances we are not telling you what we are doing.”

Do not mystify Undefined behavior as something that will happen, or even as something that can be defined.

+2
source

You can find a more detailed explanation of what is meant by the definition of "implementation" in the C Justification - not a standard document as such, but a good reference.

In chapter 1.6 Definition of terms :

The behavior defined by the implementation gives the developer the freedom to choose the appropriate approach, but requires that the choice be explained to the user. The behaviors designated as defined by the implementation are usually those in which the user could make meaningful coding decisions based on the definition of the implementation. Developers should keep this criterion in mind when deciding how extensive an implementation definition should be. As with unspecified behavior, simply failing to translate the source containing the behavior defined by the implementation is not an adequate response.

You cannot make a “smart coding decision” based on undefined behavior.

The C Frequently Asked Questions (again, not a standard but well-known link) is perfectly clear :

implementation: The implementation must choose some kind of behavior; It may not compile the program. ( A program using the construct is incorrect. ). The selection must be documented. The standard may indicate a set of acceptable behaviors from which to choose, or it may not impose specific requirements.

Neither the vague nor the behavior defined by the implementation are errors - the compiler cannot fail to translate. They are intended to provide implementation options for creating optimal code for the target environment.

+2
source

Assigning integers to smaller types is somewhat strange, since the standard clearly recognizes that some implementations can trap, but - uniquely - this requires that the capture follows the rules of the signals; the decision to impose this requirement here, but not elsewhere, is somewhat curious, because in many cases this will prevent what would otherwise be a simple optimization - replacing an integer with a meaningful variable whose type is shorter than int and whose address has never been accepted, with int .

However, for some reason, the authors of the standard have gone astray to prohibit this optimization. [Note. If I were responsible for the standard, I would indicate that explicit casting to a shorter integer type will give a value that, when casting to an unsigned type of the same size, will give the same result as the value of the value immediately for each such value exists, but that the storage of an oversized value directly for an lvalue without a throw will not be thus limited; I did not write a standard, though].

This is ironic, in fact: given:

 uint64t signedpow(int32_t n, uint32_t p) { uint64_t result; while(p--) { n*=n; result+=n; } return result; } uint64t unsignedpow(uint32_t n, uint32_t p) { uint64_t result; while(p--) { n*=n; result+=n; } return result; } 

On a platform where int is 32 bits, the latter defined semantics for all values ​​of n and p , while the former will not, but on a platform where int is 64 bits, the opposite would be true. A compiler for a typical 64-bit platform that did not want to spend code on some other specific behavior would be required by the standard to mask and sign signed n after each multiplication, but with some unsigned values ​​the compiler could do whatever it wanted, including come back on time and pretend that no implementation has ever promised to always fulfill half-sized unsigned factors in accordance with modular arithmetic.

+1
source

According to the C11 standard, chapter 3.4.1,

implementation-defined behavior

undefined behavior when each implementation documents how choices are made

So, each implementation must make a choice. Otherwise, it will not match. Thus, we can say that we must have a definite result for this implementation. It can be either lower

  • specific behavior specific to this implementation that will be executed if the instruction occurs.
  • a specific signal that will be at risk if a problem occurs. (Basically, saying you can't handle it.)

Connected:

The behavior defined by the implementation gives the developer the freedom to choose the appropriate approach, but requires that the choice be explained to the user. Behavior designated as being defined by an implementation is usually one in which the user can make meaningful coding decisions based on the definition of the implementation . Developers should keep this criterion in mind when deciding how extensive an implementation definition should be. As with unspecified behavior, simply failing to translate the source containing the behavior defined by the implementation is not an adequate response.

Now no one can make “meaningful coding decisions” based on undefined behavior.

  1. From the C FAQ , question 11.33,

implementation: the implementation must choose some behavior; It may not compile the program. (A program using the construct is incorrect.) The choice must be documented. The standard may indicate a set of acceptable behaviors from which to choose, or it may not impose specific requirements.

The first sentence contains must , as I mentioned earlier in my answer.

0
source

Source: https://habr.com/ru/post/1237529/


All Articles