News for hackers. Stuff that matters.
Re:You Can't Always Hash Pointers in C by pygy_ on Tuesday May 31, 2016 @06:55AM (#11805808)
Whelp... Is there a non undefined way to do bounds checking in C?
Re:You Can't Always Hash Pointers in C by pjc50 on Tuesday May 31, 2016 @10:32AM (#11806716)
Avoid maintaining pointers into arrays and instead maintain indices. If you maintain them as unsigned then you only need one range check (although you then have to avoid underflow if you want to stay in the realm of defined behaviour).

One of the downsides to C is that K&R and most source code out there will encourage you to do pointer arithmetic freely, but the standard points out all kinds of surprises where it is undefined. So you get technically non-conformant code in widespread use.

Re:You Can't Always Hash Pointers in C by wnoise on Tuesday May 31, 2016 @11:25PM (#11811877)
Unsigned overflow/underflow is defined behavior, unlike signed overflow/underflow. Underflow will be caught by the upper range check.
Re:You Can't Always Hash Pointers in C by tehrei on Tuesday May 31, 2016 @11:04AM (#11806908)
I thought unsigned underflow is defined as wrapping?
Re:You Can't Always Hash Pointers in C by Sacho on Tuesday May 31, 2016 @07:16AM (#11805869)
I'm just a C newbie, so I can't give you a definitive answer. Here's two I came up with:

If you know the bounds of the array, then you could enumerate it and check if the pointers to any of its elements are equal to your pointer.

Alternatively you could store references to array elements as a structure of a start pointer + offset, and arrays as a pair of start and end pointers. Then checking if a reference points to part of the array would be a two-step operation - check that the starts match and that your offset is within the end - start bounds.