Guard against null plan pointer in CachedPlanIsSimplyValid().
authorTom Lane <[email protected]>
Thu, 20 Jul 2023 18:23:46 +0000 (14:23 -0400)
committerTom Lane <[email protected]>
Thu, 20 Jul 2023 18:23:46 +0000 (14:23 -0400)
If both the passed-in plan pointer and plansource->gplan are
NULL, CachedPlanIsSimplyValid would think that the plan pointer
is possibly-valid and try to dereference it.  For the one extant
call site in plpgsql, this situation doesn't normally happen
which is why we've not noticed. However, it appears to be possible
if the previous use of the cached plan failed, as per report from
Justin Pryzby.  Add an extra check to prevent crashing.
Back-patch to v13 where this code was added.

Discussion: https://postgr.es/m/ZLlV+STFz1l/[email protected]

src/backend/utils/cache/plancache.c

index e1a873967f09f980ff6ef5c9c75e841a38171505..1355c3a6f1d837fc4314cb7f7acf793884d79aea 100644 (file)
@@ -1440,7 +1440,9 @@ CachedPlanIsSimplyValid(CachedPlanSource *plansource, CachedPlan *plan,
     * that here we *do* check plansource->is_valid, so as to force plan
     * rebuild if that's become false.
     */
-   if (!plansource->is_valid || plan != plansource->gplan || !plan->is_valid)
+   if (!plansource->is_valid ||
+       plan == NULL || plan != plansource->gplan ||
+       !plan->is_valid)
        return false;
 
    Assert(plan->magic == CACHEDPLAN_MAGIC);