When compiling the following program with -O2, gcc optimizes the var_unwind_all
function to an endless loop. Without -O, the program is compiled and runs
correctly. The same error is present with gcc 4.3.0.

The program is (no includes):

struct rlist {
        struct rlist *prev;
};

void
_list_remove(struct rlist **last, struct rlist *obj)
{
        *last = obj->prev;
}


struct var {
        struct var *prev;
};

struct var *var_last;

void
var_unwind_all()
{
        while (var_last)
                _list_remove((struct rlist**)&var_last,
                             (struct rlist*)var_last);
                ;
}


/* Implementation */
struct var bucket[2];

int
main()
{
        int i;
        struct var *cur;

        /* Create the list */
        for (i = 0; i < sizeof(bucket)/sizeof(bucket[0]); i++) {
                cur = bucket + i;
                cur->prev = var_last;
                var_last = cur;
        }

        /* Deallocate it */
        var_unwind_all();

        return 0;
}

The generated .s file contains:

.L10:


-- 
           Summary: Gcc optimizes code to an endless loop
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gray at gnu dot org dot ua
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38527

Reply via email to