Avoid dangling pointer to relation name in RLS code path in DoCopy().
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 6 Mar 2017 21:50:47 +0000 (16:50 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 6 Mar 2017 21:50:47 +0000 (16:50 -0500)
With RLS active, "COPY tab TO ..." failed under -DRELCACHE_FORCE_RELEASE,
and would sometimes fail without that, because it used the relation name
directly from the relcache as part of the parsetree it's building.  That
becomes a potentially-dangling pointer as soon as the relcache entry is
closed, a bit further down.  Typical symptom if the relcache entry chanced
to get cleared would be "relation does not exist" error with a garbage
relation name, or possibly a core dump; but if you were really truly
unlucky, the COPY might copy from the wrong table.

Per report from Andrew Dunstan that regression tests fail with
-DRELCACHE_FORCE_RELEASE.  The core tests now pass for me (but have
not tried "make check-world" yet).

Discussion: https://postgr.es/m/7b52f900-0579-cda9-ae2e-de5da17090e6@2ndQuadrant.com

src/backend/commands/copy.c

index 01a63c823e22e638e45e30cbd0015e5dbb5a19c6..3102ab18c57a45af4756c76d1e949f971ba6b3a0 100644 (file)
@@ -923,7 +923,8 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
             * relation which we have opened and locked.
             */
            from = makeRangeVar(get_namespace_name(RelationGetNamespace(rel)),
-                               RelationGetRelationName(rel), -1);
+                               pstrdup(RelationGetRelationName(rel)),
+                               -1);
 
            /* Build query */
            select = makeNode(SelectStmt);