This is an archive of the discontinued LLVM Phabricator instance.

Remove undefined behavior from list::push_back/front, emplace_back/front.
AbandonedPublic

Authored by mclow.lists on Mar 26 2014, 8:45 PM.

Details

Summary

This is an attempt to fix http://llvm.org/bugs/show_bug.cgi?id=18488, where std::list shows undefined behavior by casting a pointer to a list_node_base to a list_node.

I have added two private routines to list: link_nodes_at_front and link_nodes_at_back, to deal with these cases.

For simplicity, I added a method self to list_node_base, because that expression was used all over the place.

This passes all tests for C++03/11/14, and with ASAN. However, the undefined behavior was observed only with gcc 4.7.2, which I don't have. I'll work on getting that set up.

Also, I am not 100% sure that there isn't a similar set of UB lurking in the insert* routines.

Diff Detail

Event Timeline

awi requested changes to this revision.Mar 27 2014, 4:22 AM

Well, ehm, let's start with the good news: this patch actually solves the example main.cpp that I added to the bug report on January 15th. And it seems to me to be a valid fix for the functions push_front(), push_back(), emplace_front(), and emplace_back().

Here's the bad news: Unfortunately, this patch doesn't solve the actual problem that std::list has: The pointers in list_node_base still have the type pointer to list_node, although they can point to the anchor node which is not a list_node. The same is true for the ptr_ member variables in list_iterator and list_const_iterator. This leads to undefined behaviour whenever these pointers are dereferenced although they currently point to the anchor node, even if they are only dereferenced to access prev_ or next_.

I've slightly changed my example file to use splice() instead of push_front(). And then the described problem reappears:
{F50161}

Please note that I've used a more recent compiler this time: g++ (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1. Please note further, that the patch I have proposed on February 28th (in the bug entry, because I didn't know about phabricator) does fix the problem for main2.cpp, too.

mclow.lists abandoned this revision.Sep 2 2015, 2:04 PM

There are other suggested fixes for this problem, and this has gotten stale.