Skip to main content

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
1 vote
1 answer
243 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 ...
user23013's user avatar
  • 2,390
11 votes
4 answers
392 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
  • 8,229
12 votes
1 answer
279 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
2 votes
0 answers
308 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,792
11 votes
3 answers
526 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
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 ...
Solal Pirelli's user avatar
18 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 ...
CPlus's user avatar
  • 8,261
22 votes
10 answers
5k 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 ...
CPlus's user avatar
  • 8,261
6 votes
4 answers
629 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
  • 223
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 ...
supercat's user avatar
  • 1,792
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 ...
CPlus's user avatar
  • 8,261
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 ...
Robert Columbia's user avatar
5 votes
1 answer
403 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 ...
CPlus's user avatar
  • 8,261
32 votes
9 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 ...
CPlus's user avatar
  • 8,261