7 comments

  • digitalPhonix4 days ago
    Herb Sutter&#x27;s comment on why it&#x27;s ok is confusing to me:<p>&gt; Regarding the use of UB internally: It&#x27;s okay and if anyone is worried about it the use of UB is benign on the platforms we target (e.g., they don&#x27;t involve hitting any hardware trap representations for these types)<p>Isn&#x27;t the outcome of the UB (ie. whether it will &quot;rm -rf &#x2F;&quot; or something else) dependent on both the target and the compiler? And the compiler (or future compiler) could plausibly make the assumption that the narrowing to an unrepresentable value will never occur and change behaviour because of it?
    • wavemode1 hour ago
      For what it&#x27;s worth, a GSL developer later reopened that GitHub issue and stated that they&#x27;re going to look into fixing the UB. Sutter may have just been stating an assumption.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;GSL&#x2F;issues&#x2F;786#issuecomment-5133269178" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;GSL&#x2F;issues&#x2F;786#issuecomment-513...</a><p>&gt; I&#x27;ll raise this issue in the next internal GSL sync. I&#x27;d agree with y&#x27;all that this behavior: <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;4Tr1fe9xG" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;4Tr1fe9xG</a> is undesirable
    • jcranmer1 hour ago
      In LLVM, the result of floating-to-int conversion that is out of range of the int is a poison value, which means you get essentially the full unpredictability of UB.<p>That said, I&#x27;m a little hard-pressed to think of optimizations that would actually take advantage of poison, because floating-point range isn&#x27;t really computed in the optimizer.
    • 20k17 minutes ago
      Yeah Herb&#x27;s 100% wrong here. Its common when people are downplaying the memory safety issues with C++ that they say things like this, but its completely incorrect. All invoked UB is potentially equally serious, and this is exploitable memory unsafety. Compilers can and do optimise away this kind of stuff (as other people have explained here)<p>There&#x27;s also important context in that Herb is currently one of the people leading the current memory safety approach for C++
    • Maxatar1 hour ago
      Yes, this is all true but Sutter&#x27;s comment is that the specific platforms that this specific implementation of the GSL targets results in the correct output. The platforms officially supported are:<p>GCC 12, 13, 14<p>XCode 14.3.1, 15.4<p>Clang 16, 17, 18<p>Visual Studio with MSVC VS2019, VS2022<p>Visual Studio with LLVM VS2019, VS2022
    • pjmlp2 hours ago
      No, UB is allowed special powers for compiler and standard library implementors, which is what Herb Sutter means with internal behaviour.<p>Meaning MSVC is aware of these cases, so the compiler has special cases for it.
      • 20k14 minutes ago
        Clang is a target for the GSL though. How can MSVC&#x27;s special powers prevent this from being exploitable UB in Clang&#x2F;LLVM?<p>This code boils down to static_cast&lt;int&gt;(some_double); so nothing fancy is going on here
      • wavemode1 hour ago
        GSL is not the standard library nor an internal runtime library. Its GitHub page claims that it supports a variety of compilers:<p>&gt; The GSL officially supports recent major versions of Visual Studio with both MSVC and LLVM, GCC, Clang, and XCode with Apple-Clang
        • pjmlp1 hour ago
          It was originally created by Microsoft and as Herb mentions &quot;all our target platforms&quot;, so most likely it gets special treatment.
          • Maxatar1 hour ago
            There is absolutely no special treatment afforded to the GSL by any of the target platforms. While we can&#x27;t inspect MSVC&#x27;s source code, both clang and GCC do not have any support or affordance for the GSL whatsoever and it would be very unusual to expect MSVC&#x27;s source code to have some kind of affordance for this library.
            • achierius1 hour ago
              The &#x27;special treatment&#x27; isn&#x27;t technical, it&#x27;s procedural -- insofar as if, during development for a new release, MSVC were to land some changes that broke GSL, Microsoft&#x27;s testing would catch that and ensure that the changes were reverted or fixed to support the latter, prior to shipping. Since they&#x27;re built as part of the same operating system, they can make sure not to step on one another&#x27;s toes -- which is not a guarantee that they can make to third-party applications.
              • tsimionescu11 minutes ago
                If you&#x27;ve ever read anything about the internal culture in Microsoft, you&#x27;d know this is extremely implausible.
              • Maxatar55 minutes ago
                I have no idea where you possibly got this idea from since the Github Issues tracker for GSL has numerous instances of new releases of MSVC breaking GSL compilation.
      • digitalPhonix1 hour ago
        That&#x27;s my point - GSL is <i>NOT</i> MSVC only, it&#x27;s a general purpose library and <i>NOT</i> a standard library implementation of a toolchain so any compiler is expected to be able to compile it (it also explicitly targets clang &amp; gcc).
        • pjmlp1 hour ago
          It was originally created by Microsoft and as Herb mentions &quot;all our target platforms&quot;, so most likely it gets special treatment.
          • aw16211071 hour ago
            At the time Herb made that comment GSL was documented as supporting XCode 12.5.1&#x2F;13.2.1, GCC 10&#x2F;11, Clang 11&#x2F;12, and Visual Studio 2019&#x2F;2022 using both MSVC&#x2F;LLVM [0]. Even if MSVC had special support for GSL I&#x27;m a bit more skeptical that such support would extend to XCode, GCC, and Clang.<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;GSL&#x2F;tree&#x2F;99a29ce797c8337b8923f2688ba1489be6f65bc4" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;GSL&#x2F;tree&#x2F;99a29ce797c8337b8923f2...</a>
    • LoganDark1 hour ago
      UB is bad not because it actually leads to any particular result on any particular platform or compiler, but because semantically it invalidates assumptions about a program. Rust is explicit on this, but it absolutely still applies to C&#x2F;C++.
      • em3rgent0rdr19 minutes ago
        Well because it could lead to any result on some platform or compiler, it invalidates assumptions about the program.
  • gpvos21 minutes ago
    Sounds like the standard should say that it results in an implementation-defined value (or wording to that effect). Saying it&#x27;s UB gives the compilers way too much leeway.
    • 20k11 minutes ago
      Its incredibly hard to get changes like this into the standard, because there&#x27;s a core contingent of people who seem to feel that UB is part of C++&#x27;s identity, and then there&#x27;s very vague hand waving about performance. There is luckily a pretty successful push in wg21 to start removing a lot of the more unnecessary UB, so hopefully this gets sent to the sausage factory as well
  • pjmlp2 hours ago
    Hopefully this will be part of UB fixes for C++29, where plenty of UB is being redefined as erroneous behaviour instead.
  • orangepanda2 hours ago
    How could it be defined behaviour, when the result is different on ARM and x86?
    • marcosdumay2 hours ago
      Architecture dependent is not the same as undefined.
      • stouset1 hour ago
        Also, the spec says it’s undefined. But compiler authors can always special-case their own compilers.
        • 20k10 minutes ago
          Clang&#x2F;LLVM at least treats this as UB, not as implementation defined behaviour however according to an LLVM dev
    • cataphract1 hour ago
      Undefined behavior is not the same as implementation-defined or unspecified behavior. A program with undefined behavior is by definition an incorrect program. But there are cases where the spec actually gives some margin to the implementation. Programs relying on the choices of the implementation may be correct, even if non-portable.
      • Maxatar47 minutes ago
        &gt;A program with undefined behavior is by definition an incorrect program.<p>This is simply false and an oft repeated myth. Undefined behavior has a specific technical definition that is in the C++ standard [1] and there is absolutely no mention in that definition or the implication of that definition that undefined behavior necessarily results in an invalid or incorrect program.<p>The definition of undefined behavior, right from the standard itself is... and I quote... get ready for it...<p>&quot;behavior for which this document imposes no requirements&quot;<p>That&#x27;s it, nothing more, nothing less.<p>The standard even goes out of its way to state the following:<p>&quot;Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, *to behaving during translation or program execution in a documented manner* characteristic of the environment&quot;.<p>Behaving in a documented manner characteristic of an environment is a far cry from being by incorrect by definition.<p>[1] <a href="https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg21&#x2F;docs&#x2F;papers&#x2F;2024&#x2F;n4986.pdf" rel="nofollow">https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg21&#x2F;docs&#x2F;papers&#x2F;2024&#x2F;n49...</a>
        • 20k2 minutes ago
          &gt;This document imposes no requirements on the behavior of programs that contain undefined behavior<p>That&#x27;s saying that programs that exhibit undefined behaviour are not governed by the C++ spec. For a program to be a valid, spec governed piece of C++ code it has to exhibit no undefined behaviour (outside of some constraints). Its accurate to say that any undefined behaviour results in the code being executed no longer being C++, and it can have any behaviour. That&#x27;s synonymous in common developer speak with &#x27;incorrect&#x27;, as its desirable for your C++ code to be executed as C++
        • cataphract11 minutes ago
          I appreciate the correction. Although it should be said that modern implementations tend to opt to assume that UB never happens.
  • lionkor4 days ago
    The core guidelines library is definitely not doing the right thing here. Very odd.
  • Byte-Naut4 days ago
    [dead]