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.
16 questions
10
votes
4
answers
3k
views
Is it plausible to let modulo-by-zero have a well-defined output value, provided that my language is C-like?
By mathematical definition, for every nonnegative integer n and every positive integer d, it holds that the modulo ...
7
votes
2
answers
826
views
What should the semantics from branching from finally be?
This question is about the expected semantics when combining resource management with branching control flow. From my perspective, it applies equally to all finally-...
1
vote
1
answer
286
views
Has there been languages that explicitly document the order of evaluation of high order functions on enumerables?
There are high order function on lists, such as map, find, reduce, in some languages. If the result of one such function is piped into another such function, there are multiple possible evaluation ...
12
votes
4
answers
514
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 ...
16
votes
3
answers
809
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 ...
3
votes
2
answers
677
views
Pros and cons of treating endless loops as "anything can happen" UB, versus merely 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 ...
13
votes
3
answers
723
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 ...
14
votes
3
answers
2k
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 ...
18
votes
3
answers
3k
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 ...
23
votes
10
answers
6k
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 ...
6
votes
4
answers
1k
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 ...
23
votes
4
answers
4k
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 ...
18
votes
8
answers
6k
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 ...
21
votes
6
answers
3k
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 ...
5
votes
1
answer
803
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 ...