Skip to content

Commit abd04e0

Browse files
committed
Ensure pg_dump_sort.c sorts null vs non-null namespace consistently.
The original coding here (which is, I believe, my fault) supposed that it didn't need to concern itself with the possibility that one object of a given type-priority has a namespace while another doesn't. But that's not reliably true anymore, if it ever was; and if it does happen then it's possible that DOTypeNameCompare returns self-inconsistent comparison results. That leads to unspecified behavior in qsort() and a resultant weird output order from pg_dump. This should end up being only a cosmetic problem, because any ordering constraints that actually matter should be enforced by the later dependency-based sort. Still, it's a bug, so back-patch. Report and fix by Jacob Champion, though I editorialized on his patch to the extent of making NULL sort after non-NULL, for consistency with our usual sorting definitions. Discussion: https://postgr.es/m/CABAq_6Hw+V-Kj7PNfD5tgOaWT_-qaYkc+SRmJkPLeUjYXLdxwQ@mail.gmail.com
1 parent 895fb6e commit abd04e0

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/bin/pg_dump/pg_dump_sort.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,25 +257,32 @@ DOTypeNameCompare(const void *p1, const void *p2)
257257
DumpableObject *obj2 = *(DumpableObject *const *) p2;
258258
int cmpval;
259259

260-
/* Sort by type */
260+
/* Sort by type's priority */
261261
cmpval = newObjectTypePriority[obj1->objType] -
262262
newObjectTypePriority[obj2->objType];
263263

264264
if (cmpval != 0)
265265
return cmpval;
266266

267267
/*
268-
* Sort by namespace. Note that all objects of the same type should
269-
* either have or not have a namespace link, so we needn't be fancy about
270-
* cases where one link is null and the other not.
268+
* Sort by namespace. Typically, all objects of the same priority would
269+
* either have or not have a namespace link, but there are exceptions.
270+
* Sort NULL namespace after non-NULL in such cases.
271271
*/
272-
if (obj1->namespace && obj2->namespace)
272+
if (obj1->namespace)
273273
{
274-
cmpval = strcmp(obj1->namespace->dobj.name,
275-
obj2->namespace->dobj.name);
276-
if (cmpval != 0)
277-
return cmpval;
274+
if (obj2->namespace)
275+
{
276+
cmpval = strcmp(obj1->namespace->dobj.name,
277+
obj2->namespace->dobj.name);
278+
if (cmpval != 0)
279+
return cmpval;
280+
}
281+
else
282+
return -1;
278283
}
284+
else if (obj2->namespace)
285+
return 1;
279286

280287
/* Sort by name */
281288
cmpval = strcmp(obj1->name, obj2->name);

0 commit comments

Comments
 (0)