Login
You're viewing the ruby.social public feed.

Replies

  • 💬 0🔄 0⭐ 1
  • Nov 25, 2026, 7:19 PM

    @mcc@mastodon.social @complexmath@hachyderm.io Nope. Not at all. The standard explicitly says that when the result of an operation is undefined the compiler can compile the program and the result of executing such program can be anything whatsoever. Such as demons flying out of your nose.

    And the nasal demons properly illustrate how absurd and adversarial such language definition is. For example, in C signed arithmetic overflow is undefined. That means that a C compiler that generates code that on signed arithmetic overflow executes 'rm -rf /' is completely fine and standards-compliant.

    I am not aware of any language other than the C language family into which zig invited itself by adding this to its standard that has such a great feature
    ​:bunhdgoogly:​

    💬 0🔄 0⭐ 0
  • Nov 25, 2026, 7:31 PM

    @bunny @complexmath "The standard explicitly says that when the result of an operation is undefined the compiler can compile the program and the result of executing such program can be anything whatsoever"

    Isn't that the definition of UB?

    💬 1🔄 0⭐ 0
  • 💬 1🔄 0⭐ 0
  • 💬 2🔄 0⭐ 0
  • Nov 25, 2026, 8:18 PM

    @bunny @complexmath One reason it is hard to avoid a de-facto rule of "the compiler may create incorrect, unrelated code when undefined behavior occurs" is that *assuming* undefined behavior does not occur typically allows compilers to do optimizations. Optimizations are in nature code-rewriting. If the compiler is making incorrect assumptions about which optimizations are safe, it is difficult or impossible to put bounds on what kinds of generated code could result.

    💬 1🔄 0⭐ 1
  • Nov 25, 2026, 8:39 PM

    @mcc@mastodon.social @complexmath@hachyderm.io However, where you allow the compiler to assume undefined behavior is on the creator of the standard. That any soundness goes out the window the moment you randomly scribble over memory is understandable, and unfortunately C is historically about randomly scribbling over memory. That 'a-b' can result in pretty much any code whatsoever when a and b are signed is completely different can of worms. And yes, I understand that assuming a-b to not overflow ever allows the compiler to make some stupid optimizations. I don't think such optimizations are are really worth it. It reminds me of the joke about the f00f bug

    i386: Hooowww mmmuuuch iiisss 2 + 2 ...
    Pentium: 5!
    i386: thhhatsss wwrrooonnnggg ...
    Pentium: but fast!

    💬 0🔄 0⭐ 0
  • Nov 25, 2026, 8:24 PM

    @mcc@mastodon.social @complexmath@hachyderm.io That's very different from what C UB provisions are like.

    What it basically says is that if you include unsafe{} in your code you are free to do the equivalent of randomly scribbling over your memory, and any soundness goes out the window as a result. It does not allow 'a-b' to generate code to execute 'rm -rf'.
    ​:abunhd:​

    💬 1🔄 0⭐ 0
  • Seancomplexmath@hachyderm.io
    Nov 25, 2026, 8:30 PM

    @bunny @mcc It's much easier to just say "we don't know what will happen if you violate these rules" than to try and state specifically what will happen when your language targets an unbounded set of computer architectures.

    💬 1🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • Nov 25, 2026, 8:32 PM

    @bunny @complexmath I am not a compiler engineer and have not worked on rustc. But given the way Rust ownership has implications for things like aliasing or whether a variable even exists, I'd be *very* surprised if Rust UB is restricted to "could cause runtime state corruption" rather than encompassing actual incorrect codegen. Rust's confinement of UB to unsafe {} blocks makes it a better language than C, but it doesn't mean Rust is using a different definition of the term "UB" altogether.

    💬 1🔄 0⭐ 0
  • Nov 25, 2026, 8:34 PM

    @bunny @complexmath I do notice the Rustinomicon:

    doc.rust-lang.org/nomicon/what

    early on states "Invoking Undefined Behavior gives the compiler full rights to do arbitrarily bad things to your program". Which sounds equivalent to the definitions in C and Zig. This is the document the Undefined Behavior page in the reference manual credits as the source of in-practice information about how unsafe {} is used.

    💬 0🔄 0⭐ 0
  • Nov 25, 2026, 8:18 PM

    @mcc@mastodon.social @complexmath@hachyderm.io I mean, there are some degrees of leeway possible.

    Like any code for which the result cannot be defined must not be compiled. Thus for the signed arithmetic case if there is a possibility that the operation would overflow it would cause a compilation error.

    Or something called 'implementation-defined', typically. The result is not prescribed but for a specific compiler implementation executing the same operation with same operands always gives same result.

    Or something even more generous: The result is not prescribed, and it need not be consistent but in any case result of a signed operation that overflows is some numeric value, and whatever the case nothing but the result is affected by the operation.

    But no. C went for nasal demons
    ​:bunhdgoogly:​

    💬 0🔄 0⭐ 0
  • Nov 25, 2026, 8:20 PM

    @bunny @complexmath "Like any code for which the result cannot be defined must not be compiled."

    A language with this rule, by definition, does not have UB.

    💬 1🔄 0⭐ 0
  • Nov 25, 2026, 9:02 PM

    @mcc@mastodon.social @complexmath@hachyderm.io Yes, and my impression is that for most programming languages this is the standard. To cause UB you would have to use FFI to load to a C library or something. That rust does allow that inline using unsafe{} is an interesting design choice but whatever. Yes, there is a lot of languages that I did not examine in detail, I do not see any reason whatsoever for them to have UB, and they might ​:abunhd:​

    💬 0🔄 0⭐ 0