News for hackers. Stuff that matters.
Re:Raku: A Language for Gremlins by JonChesterfield on Monday August 07, 2023 @09:47PM (#37043450)
Linux kernel space on x64 uses "negative" pointer values so the high bits are set there. Which is probably the more interesting place to find this bug.

Needs to be unsigned to get this failure mode, any signed loop gets compiled assuming no overflow for a different (though similar!) failure mode.

I'm leaning towards "C is surprising". Didn't have to be but as presently implemented is very full of hazards.

Re:Raku: A Language for Gremlins by trealira on Monday August 07, 2023 @10:34PM (#37043784)
> Needs to be unsigned to get this failure mode, any signed loop gets compiled assuming no overflow for a different (though similar!) failure mode.

In C and C++, compilers will optimize assuming that signed integer overflow doesn't happen, but that doesn't stop it from actually happening. Unless you set it to trap on overflow, signed integers still wrap; it's just that compilers make (incorrect) optimizations assuming that it doesn't happen.

You know, this makes me wonder whether it's better pointers to be compared with signed or unsigned comparisons. Currently, my compiler emits unsigned comparison instructions for them.

Re:Raku: A Language for Gremlins by Joker_vD on Tuesday August 08, 2023 @11:00AM (#37049988)
Do you mean unsigned branches? JB/JBE/JA/JAE instead of JL/JLE/JG/JGE? Are there actually code patterns where it's preferable to just JE/JNE? AFAIK, computing a pointer outside of the underlying array's boundaries (except for computing the one-past-the-last-element pointer) is UB so e.g.

    for (SomeStruct * curr = p, * end = &p[N]; curr < end; curr += 2) {
        // processing the pair of curr[0] and curr[1], with care
        // in case when curr[1] doesn't exist
    }
is an invalid optimization of

    for (SomeStruct * curr = p, * end = &p[N]; curr != end; ) {
        // processing the pair of curr[0] and curr[1], with care
        // in case when curr[1] doesn't exist

        if (++curr != end) { ++curr; }
    }
Re:Raku: A Language for Gremlins by trealira on Tuesday August 08, 2023 @12:10PM (#37051251)
> Do you mean unsigned branches? JB/JBE/JA/JAE instead of JL/JLE/JG/JGE?

Yeah, that's what I meant. Thanks for speaking more clearly.

> Are there actually code patterns where it's preferable to just JE/JNE?

That's a good point, and sidesteps the issue of pointer signedness.

I think sometimes JE / JNE isn't enough. For example, if you want to process a buffer in reverse order using pointer arithmetic:

  /* p starts off one past the end of the buffer */
  char *p = buffer + bufsize;
  while (--p >= buffer) {
      /* ... */
  }
I'm not sure if this would technically be undefined behavior, though, as the C standard only explicitly permits computing a pointer one past the end of the array, and other out-of-bounds computations are undefined, IIRC. In practice, I don't think any compiler would miscompile this.