Avoid crash in rare case of concurrent DROP
authorAlvaro Herrera
Fri, 5 Nov 2021 15:29:35 +0000 (12:29 -0300)
committerAlvaro Herrera
Fri, 5 Nov 2021 15:29:35 +0000 (12:29 -0300)
When a role being dropped contains is referenced by catalog objects that
are concurrently also being dropped, a crash can result while trying to
construct the string that describes the objects.  Suppress that by
ignoring objects whose descriptions are returned as NULL.

The majority of relevant codesites were already cautious about this
already; we had just missed a couple.

This is an old bug, so backpatch all the way back.

Reported-by: Alexander Lakhin
Discussion: https://postgr.es/m/17126-21887f04508cb5c8@postgresql.org

src/backend/catalog/dependency.c
src/backend/catalog/pg_shdepend.c

index 85b2bbac02415a24c6d2d0e204aa45834829f9a2..52376949973c9932cd477622ad5cdd3478f4b073 100644 (file)
@@ -1102,6 +1102,10 @@ reportDependentObjects(const ObjectAddresses *targetObjects,
 
        objDesc = getObjectDescription(obj, false);
 
+       /* An object being dropped concurrently doesn't need to be reported */
+       if (objDesc == NULL)
+           continue;
+
        /*
         * If, at any stage of the recursive search, we reached the object via
         * an AUTO, INTERNAL, PARTITION, or EXTENSION dependency, then it's
@@ -1127,23 +1131,28 @@ reportDependentObjects(const ObjectAddresses *targetObjects,
            char       *otherDesc = getObjectDescription(&extra->dependee,
                                                         false);
 
-           if (numReportedClient < MAX_REPORTED_DEPS)
+           if (otherDesc)
            {
+               if (numReportedClient < MAX_REPORTED_DEPS)
+               {
+                   /* separate entries with a newline */
+                   if (clientdetail.len != 0)
+                       appendStringInfoChar(&clientdetail, '\n');
+                   appendStringInfo(&clientdetail, _("%s depends on %s"),
+                                    objDesc, otherDesc);
+                   numReportedClient++;
+               }
+               else
+                   numNotReportedClient++;
                /* separate entries with a newline */
-               if (clientdetail.len != 0)
-                   appendStringInfoChar(&clientdetail, '\n');
-               appendStringInfo(&clientdetail, _("%s depends on %s"),
+               if (logdetail.len != 0)
+                   appendStringInfoChar(&logdetail, '\n');
+               appendStringInfo(&logdetail, _("%s depends on %s"),
                                 objDesc, otherDesc);
-               numReportedClient++;
+               pfree(otherDesc);
            }
            else
                numNotReportedClient++;
-           /* separate entries with a newline */
-           if (logdetail.len != 0)
-               appendStringInfoChar(&logdetail, '\n');
-           appendStringInfo(&logdetail, _("%s depends on %s"),
-                            objDesc, otherDesc);
-           pfree(otherDesc);
            ok = false;
        }
        else
index d0c96b6de1db88c67fa633858869abfab9373e55..d89a9fa2bfcec97bf6c8ac442a9fd54549b52aa8 100644 (file)
@@ -1238,6 +1238,12 @@ storeObjectDescription(StringInfo descs,
 {
    char       *objdesc = getObjectDescription(object, false);
 
+   /*
+    * An object being dropped concurrently doesn't need to be reported.
+    */
+   if (objdesc == NULL)
+       return;
+
    /* separate entries with a newline */
    if (descs->len != 0)
        appendStringInfoChar(descs, '\n');