Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Make object address handling more robust
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Wed, 20 Feb 2019 12:12:02 +0000 (09:12 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Wed, 20 Feb 2019 12:16:00 +0000 (09:16 -0300)
pg_identify_object_as_address crashes when passed certain tuples from
inconsistent system catalogs.  Make it more defensive.

Author: Álvaro Herrera
Reviewed-by: Michaël Paquier
Discussion: https://postgr.es/m/20190218202743.GA12392@alvherre.pgsql

src/backend/catalog/objectaddress.c

index f609461f4b0d152ba8c74c31139d99925b1d8482..172568c002960fb39b2e36ccbb823304819f6c4d 100644 (file)
@@ -3508,7 +3508,10 @@ pg_identify_object_as_address(PG_FUNCTION_ARGS)
    pfree(identity);
 
    /* object_names */
-   values[1] = PointerGetDatum(strlist_to_textarray(names));
+   if (names != NIL)
+       values[1] = PointerGetDatum(strlist_to_textarray(names));
+   else
+       values[1] = PointerGetDatum(construct_empty_array(TEXTOID));
    nulls[1] = false;
 
    /* object_args */
@@ -4717,10 +4720,12 @@ strlist_to_textarray(List *list)
 {
    ArrayType  *arr;
    Datum      *datums;
+   bool       *nulls;
    int         j = 0;
    ListCell   *cell;
    MemoryContext memcxt;
    MemoryContext oldcxt;
+   int         lb[1];
 
    memcxt = AllocSetContextCreate(CurrentMemoryContext,
                                   "strlist to array",
@@ -4730,17 +4735,25 @@ strlist_to_textarray(List *list)
    oldcxt = MemoryContextSwitchTo(memcxt);
 
    datums = palloc(sizeof(text *) * list_length(list));
+   nulls = palloc(sizeof(bool) * list_length(list));
    foreach(cell, list)
    {
        char       *name = lfirst(cell);
 
-       datums[j++] = CStringGetTextDatum(name);
+       if (name)
+       {
+           nulls[j] = false;
+           datums[j++] = CStringGetTextDatum(name);
+       }
+       else
+           nulls[j++] = true;
    }
 
    MemoryContextSwitchTo(oldcxt);
 
-   arr = construct_array(datums, list_length(list),
-                         TEXTOID, -1, false, 'i');
+   lb[0] = 1;
+   arr = construct_md_array(datums, nulls, 1, &j,
+                            lb, TEXTOID, -1, false, 'i');
    MemoryContextDelete(memcxt);
 
    return arr;