Created attachment 584761 [details] SRPM showing problem and workaround I would like to update pari to 2.5.x but it fails one of the tests in its test suite when built with the default Fedora %{optflags} value, which includes -O2. I have narrowed this failure down to the function vecsort0() in the file pari-2.5.1/src/basemath/bibli2.c and can work around the issue by wrapping the function with an optimization pragma that disables -ftree-dse (Patch13 in the attached SRPM). If the application of this patch is commented out in the spec file, the package fails to build in F-17 and Rawhide (though it builds OK for F-16). Looking at the intermediate code generated after optimization, it appears that the dead store elimination is removing the assignments: v.cmp = cmp; v.k = k; at lines 1442 and 1443 of the original code, and this seems wrong because the next line is: E = (void*)&v; and then E is passed as a parameter to other functions later on in the code, and there are no further assignments to v. Am I right that this is an optimization bug or is there something wrong with the code that I'm missing?
That is not a gcc bug. if (k) { ... struct veccmp_s v; ... v.cmp = cmp; v.k = k; E = (void*)&v; CMP = &veccmp; } else { E = (void*)((typ(x) == t_VECSMALL)? cmp_small: cmp); CMP = &cmp_nodata; } ... use *E This is undefined behavior, after the v variable goes out of scope (at } before else), you can't reference it any longer, even when the E pointer is still in scope. It is the same bug as returning an address of an automatic variable from a function: int *foo (void) { int x = 1; return &x; }
Excellent - moving the "struct veccmp_s v" declaration up to the top of the function's scope seems to fix this. Thank you very much.
Created attachment 585026 [details] Fix for scoping issue Attached patch sent upstream (http://pari.math.u-bordeaux.fr/cgi-bin/bugreport.cgi?bug=1314) and should be included in pari 2.5.2.