Fix optimization hazard in gram.y's makeOrderedSetArgs(), redux.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 7 Oct 2020 22:41:39 +0000 (18:41 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 7 Oct 2020 22:41:39 +0000 (18:41 -0400)
It appears that commit cf63c641c, which intended to prevent
misoptimization of the result-building step in makeOrderedSetArgs,
didn't go far enough: buildfarm member hornet's version of xlc
is now optimizing back to the old, broken behavior in which
list_length(directargs) is fetched only after list_concat() has
changed that value.  I'm not entirely convinced whether that's
an undeniable compiler bug or whether it can be justified by a
sufficiently aggressive interpretation of C sequence points.
So let's just change the code to make it harder to misinterpret.

Back-patch to all supported versions, just in case.

Discussion: https://postgr.es/m/1830491.1601944935@sss.pgh.pa.us

src/backend/parser/gram.y

index 0d101d8171545c5abb1f57bfb837e246d1b7e954..480d16834687a0486996317d4e3d79fc89c17165 100644 (file)
@@ -16291,7 +16291,7 @@ makeOrderedSetArgs(List *directargs, List *orderedargs,
                                   core_yyscan_t yyscanner)
 {
        FunctionParameter *lastd = (FunctionParameter *) llast(directargs);
-       int                     ndirectargs;
+       Value      *ndirectargs;
 
        /* No restriction unless last direct arg is VARIADIC */
        if (lastd->mode == FUNC_PARAM_VARIADIC)
@@ -16315,10 +16315,10 @@ makeOrderedSetArgs(List *directargs, List *orderedargs,
        }
 
        /* don't merge into the next line, as list_concat changes directargs */
-       ndirectargs = list_length(directargs);
+       ndirectargs = makeInteger(list_length(directargs));
 
        return list_make2(list_concat(directargs, orderedargs),
-                                         makeInteger(ndirectargs));
+                                         ndirectargs);
 }
 
 /* insertSelectOptions()