Questions tagged [undefined-behavior]

Some languages specify "undefined behavior" for code or actions outside of the specified bounds of the language, which enables certain optimizations. Use this tag for questions about specifying undefined behaviour in a language, or optimizations relating to implementing languages with this behavior. Do not use only for undefined behavior encountered during implementation.

Filter by
Sorted by
Tagged with
10 votes
4 answers
253 views

Are there languages that implement a more granular precondition system than just safe/unsafe

In a language like C, many functions and operators have some preconditions to work. Violating a precondition is undefined behavior. For example, when indexing an array the index must be less than the ...
mousetail's user avatar
  • 6,746
9 votes
1 answer
159 views

What was the rationale for making realloc(ptr, 0) have UB in C23

This is the first breaking change that C made, which was making realloc(ptr, 0) have UB instead of being roughly equivalent to ...
user1345541's user avatar
0 votes
0 answers
188 views

Pros and cons of treating endless loops as "anything can happen" UB, versus allowing limited reordering

Many programming languages, including C89, specify that the behavior of a program in terms of sequentially executed steps, whose behavior is in turn defined in terms of the program's state when the ...
supercat's user avatar
  • 1,562
11 votes
3 answers
476 views

When was our current definition of "undefined behavior" first used?

In my answer to another question, What was the purpose of Tartan's "disable" statement?, I asserted that it's anachronistic to use our modern C-derived definition of "undefined ...
Glenn Willen's user avatar
13 votes
3 answers
1k views

What was the purpose of Tartan's "disable" statement?

The Tartan language, designed during the process that eventually led to Ada, has a disable statement defined as follows: A disable declaration in an inner block ...
Solal Pirelli's user avatar
17 votes
3 answers
2k views

What optimizations does the strict aliasing rule facilitate?

This question is tangentially related to: Why is type reinterpretation considered highly problematic in many programming languages? Regardless how 'problematic' type reinterpretation is, why do some ...
user16217248's user avatar
  • 7,285
19 votes
10 answers
4k views

Why would accessing uninitialized memory necessarily be undefined behavior?

In C, accessing any indeterminate/uninitialized memory is undefined behavior, period. Even in the case that the type in question is guaranteed to have no trap representations, such as ...
user16217248's user avatar
  • 7,285
5 votes
4 answers
502 views

How do interpreters avoid stack buffer overflow-related undefined behavior and exploits?

Most interpreters (including those for bytecode-based languages like Java) are assumed to be exempt of UB at the low-level. For instance, I would never expect NodeJS or the Hotspot JVM to allow ...
MrAnima's user avatar
  • 213
23 votes
4 answers
3k views

Pros and cons of "anything-can-happen" UB versus allowing particular deviations from sequential program execution

Some language specifications invite compilers to make certain assumptions, and behave in completely arbitrary fashion if such functions are violated, even if the code in question would have ...
supercat's user avatar
  • 1,562
15 votes
8 answers
5k views

Why would a language need to have trap representations?

In C there is a concept of trap representations, or non-value representations. If such a value is produced or used, immediate undefined behavior is invoked. This is one of the dangers of using ...
user16217248's user avatar
  • 7,285
21 votes
6 answers
2k views

How does one transpile valid code that corresponds to undefined behavior in the target language?

Consider the following C# snippet: Object bad= null; Console.WriteLine(bad.ToString()); This is valid C#. It will raise a ...
Robert Columbia's user avatar
5 votes
1 answer
278 views

How could I implement a non-null pointer qualifier in a C-style language?

In C currently, pointers just are, and there are no qualifiers to tell programmers whether they can be null or not. Objective-C kind of solves this problem by adding ...
user16217248's user avatar
  • 7,285
31 votes
8 answers
3k views

Why would a language have a concept of undefined behavior instead of raising an error?

Certain constructs or conditions in programming just are not allowed. Languages such as Java or Swift handle these by raising an error when encountered. C and C++ on the other hand say 'Anything could ...
user16217248's user avatar
  • 7,285