3 ms·
Without context (I'm not familiar with nginx' code) the blatant subtraction of pointers was scary, too. As everyone is of course no doubt aware, subtraction of
by unwind 13y ago
Without context (I'm not familiar with nginx' code) the blatant subtraction of pointers was scary, too.
As everyone is of course no doubt aware, subtraction of pointers is only valid in C if you can guarantee that the pointers point to the same "object" (or at most 1 character past the end of an object).
I would prefer an interface using a size_t count of available space, rather than passing an end-pointer around.
Again, this is based solely on reading the patch, so it might be perfectly fine. Considering nginx' reputation, I guess it's academic.
- atom7 13y agoIt's a common practice to use two pointers when parsing some stream data buffer. If you will use a pointer to the start of the buffer and the size of an available data to parse, then you would need to change two variables instead of one on every parsing stage. So you have more opportunity to make a mistake.
- ambrop7 13y agoIt's kind of implied by the parameters that they point to the same object; they're named "pos" and "end" after all. But I agree, a count variable is nicer. The subtraction also has a (very theoretical) problem that its result must fit in ptrdiff_t (signed), and it would produce undefined behavior when you have a huge object whose size doesn't fit in ptrdiff_t (but does in size_t, by definition).