• Wilzax@lemmy.world
      link
      fedilink
      English
      arrow-up
      30
      ·
      18 hours ago

      If you’re writing sloppy C code your assembly code probably won’t work either

        • calcopiritus@lemmy.world
          link
          fedilink
          English
          arrow-up
          1
          ·
          7 minutes ago

          I recently came across a rust book on how pointers aren’t just ints, because of UB.

          fn main() {
              a = &1
              b = &2
              a++
              if a == b {
                  *a = 3
                  print(b)
              }
          }
          

          This may either: not print anything, print 3 or print 2.

          Depending on the compiler, since b isn’t changed at all, it might optimize the print for print(2) instead of print(b). Even though everyone can agree that it should either not print anything or 3, but never 2.

        • Buddahriffic@lemmy.world
          link
          fedilink
          English
          arrow-up
          1
          ·
          3 hours ago

          A compiler making assumptions like that about undefined behaviour sounds just like a bug. Maybe the bug is in the spec rather than the compiler, but I can’t think of any time it would be better to optimize that code out entirely because UB is detected rather than just throwing an error or warning and otherwise ignoring the edge cases where the behaviour might break. It sounds like the worst possible option exactly for the reasons listed in that blog.