r265934 - [clang-format] Walk backwards from end() instead of forwards from rend().

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 11 15:13:09 PDT 2016


On Mon, Apr 11, 2016 at 11:15 PM, Martin Probst <martin at probst.io> wrote:
>> > Was/is the 2 correct then? (if it was next(rbegin, 2) that'd be
>> > equivalent
>> > to prev(end, 3), yes?)
>>
>> I think it is from looking at the test case, looping in martin to see
>> whether I'm right :)
>
>
> At that point in the code, we have [..., PrePrev = '@', Prev = 'SomeToken']
> and CurrentToken = 'Whatever', CurrentToken has not been appended to the
> list yet. So moving two from the end should be correct.
>
> I don't quite understand why rend() actually works in my code with the unit
> tests. Is that some unintentional side effect of its implementation leaking?
> I think I explicitly confirmed that my test does test what it's trying to
> test.

The implementation of std::list in common standard libraries is
actually cyclic. So ++end() brings you back to the beginning of the
list. This made the test work magically :)

Dropping off the end of the list is of course undefined behavior and
there may be non-cyclic implementations where this doesn't work. Debug
STL also threw assertion failures, which made me aware of this issue
in the first place.

It's also interesting that due to the cyclic nature of std::list, asan
(or valgrind) cannot detect this kind of bug.


More information about the cfe-commits mailing list