I was digging into code of flatbuffers and it does seem to me that there is undefined behavior in the core library. But I just can't believe it is there (and it seems to work in some usecases)...
I'd highly appreciate if somebody verifies my way of thinking/shows me that I missed something :)
.h#L80
difference_type operator-(const VectorIterator &other) const {
return (data_ - other.data_) / element_stride;
}
My rationale, why I think it is UB:
difference_type == std::ptrdiff_t which is based on standard Signed ["something" >= 16b] ()
Let's expect that the first parenthesis end up negative (which should be valid usecase)
Now when I assume the ptrdiff_t is <= size_t:
We get to:
pointer arithmetics also end up as ptrdiff_t. So we are at:
This leads me to:
And based on standard (8/11.5.3) we get the first operand of divison promoted to Unsigned: The standard snippet
Now lets assume ptrdiff_t is > size_t
So I see there 2 possible outcomes of the operator-, both complying with the standard requirements, based on ptrdiff_t implementation by specific compiler.
Thanks!