[LLVMdev] Segfault in ilist_node::getPrevNode for nodes with certain custom traits

Nico Rieck nico.rieck at gmail.com
Thu Jan 16 10:40:05 PST 2014

On 14.01.2014 17:35, Nico Rieck wrote:
> Is ilist_node::getPrevNode() supposed to work for node types with custom
> traits that embed the sentinel? Examples: Instruction, MachineInstr,
> BasicBlock, …
> Calling getPrevNode on the first node in a such a list segfaults because
> "Prev" is 0:
>   NodeTy *getPrevNode() {
>     NodeTy *Prev = this->getPrev();
>     // Check for sentinel.
>     if (!Prev->getNext())
>       return 0;
>     return Prev;
>   }
> None of the ilist unit tests pass/run when done with similar traits.
> They either segfault or run into the sentinel.

Looking through the history this was indeed implemented (in 2009) to
save a pointer. I'm surprised this issue hasn't come up more often (I
can only find 2 mails regarding this issue, without conclusion).

One solution I tested would be to use a special ilist_half_node in these
cases that represents "Prev" and "isSentinel" with a PointerIntPair,
lift the sentinel check into the list traits and then specialize the
check use the PointerIntPair where applicable (see the attached patch
for a draft).

-------------- next part --------------
A non-text attachment was scrubbed...
Name: ilist.patch
Type: text/x-patch
Size: 5649 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140116/265d5540/attachment.bin>

More information about the llvm-dev mailing list