[Lemon-user] bug in ListDigraph::erase(Node &) ?

Duraid Madina duraid at riken.jp
Wed Mar 30 05:50:16 CEST 2011


Hi everyone,

	I think there is a bug in list_graph.h's erase(Node) function, which
in my version is this:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    void erase(const Node& node) {
      int n = node.id;
      
      if(nodes[n].next != -1) {
        nodes[nodes[n].next].prev = nodes[n].prev;
      }
       
      if(nodes[n].prev != -1) {
        nodes[nodes[n].prev].next = nodes[n].next;
      } else {
        first_node = nodes[n].next;
      }
       
      nodes[n].next = first_free_node;
      first_free_node = n;
      nodes[n].prev = -2;
    
    }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	Suppose nodes[n].prev is -2. Then the check against 1 will succeed,
and nodes[-2].next will be accessed, which will _usually_ not lead to a
crash, just silent corruption.

	If the intended interpretation of "x != -1" here is "x is valid",
maybe it would make sense to change all these comparisons to "x >= 0" ?

	I hope this helps,

	Duraid




More information about the Lemon-user mailing list