Skip to content

Commit a37f94a

Browse files
committed
Prevent using strncpy with src == dest in TupleDescInitEntry.
The C and POSIX standards state that strncpy's behavior is undefined when source and destination areas overlap. While it remains dubious whether any implementations really misbehave when the pointers are exactly equal, some platforms are now starting to force the issue by complaining when an undefined call occurs. (In particular OS X 10.9 has been seen to dump core here, though the exact set of circumstances needed to trigger that remain elusive. Similar behavior can be expected to be optional on Linux and other platforms in the near future.) So tweak the code to explicitly do nothing when nothing need be done. Back-patch to all active branches. In HEAD, this also lets us get rid of an exception in valgrind.supp. Per discussion of a report from Matthias Schmitt.
1 parent 9060cb9 commit a37f94a

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/backend/access/common/tupdesc.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,12 @@ equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
433433
* This function initializes a single attribute structure in
434434
* a previously allocated tuple descriptor.
435435
*
436+
* If attributeName is NULL, the attname field is set to an empty string
437+
* (this is for cases where we don't know or need a name for the field).
438+
* Also, some callers use this function to change the datatype-related fields
439+
* in an existing tupdesc; they pass attributeName = NameStr(att->attname)
440+
* to indicate that the attname field shouldn't be modified.
441+
*
436442
* Note that attcollation is set to the default for the specified datatype.
437443
* If a nondefault collation is needed, insert it afterwards using
438444
* TupleDescInitEntryCollation.
@@ -466,12 +472,12 @@ TupleDescInitEntry(TupleDesc desc,
466472
/*
467473
* Note: attributeName can be NULL, because the planner doesn't always
468474
* fill in valid resname values in targetlists, particularly for resjunk
469-
* attributes.
475+
* attributes. Also, do nothing if caller wants to re-use the old attname.
470476
*/
471-
if (attributeName != NULL)
472-
namestrcpy(&(att->attname), attributeName);
473-
else
477+
if (attributeName == NULL)
474478
MemSet(NameStr(att->attname), 0, NAMEDATALEN);
479+
else if (attributeName != NameStr(att->attname))
480+
namestrcpy(&(att->attname), attributeName);
475481

476482
att->attstattarget = -1;
477483
att->attcacheoff = -1;

0 commit comments

Comments
 (0)