Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Fix crasher bug in array_position(s)
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Fri, 9 Dec 2016 15:42:17 +0000 (12:42 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Fri, 9 Dec 2016 15:42:17 +0000 (12:42 -0300)
array_position and its cousin array_positions were caching the element
type equality function's FmgrInfo without being careful enough to put it
in a long-lived context.  This is obviously broken but it didn't matter
in most cases; only when using arrays of records (involving record_eq)
it becomes a problem.  The fix is to ensure that the type's equality
function's FmgrInfo is cached in the array_position's flinfo->fn_mcxt
rather than the current memory context.

Apart from record types, the only other case that seems complex enough
to possibly cause the same problem are range types.  I didn't find a way
to reproduce the problem with those, so I only include the test case
submitted with the bug report as regression test.

Bug report and patch: Junseok Yang
Discussion: https://postgr.es/m/CAE+byMupUURYiZ6bKYgMZb9pgV1CYAijJGqWj-90W=nS7uEOeA@mail.gmail.com
Backpatch to 9.5, where array_position appeared.

src/backend/utils/adt/array_userfuncs.c
src/test/regress/expected/arrays.out
src/test/regress/sql/arrays.sql

index 8d6fa41a3c59b903a7b1ebce0ec75b834d3ddece..9eb678add44354042a271782406b25be2acef2f5 100644 (file)
@@ -795,7 +795,8 @@ array_position_common(FunctionCallInfo fcinfo)
                       format_type_be(element_type))));
 
        my_extra->element_type = element_type;
-       fmgr_info(typentry->eq_opr_finfo.fn_oid, &my_extra->proc);
+       fmgr_info_cxt(typentry->eq_opr_finfo.fn_oid, &my_extra->proc,
+                     fcinfo->flinfo->fn_mcxt);
    }
 
    /* Examine each array element until we find a match. */
@@ -933,7 +934,8 @@ array_positions(PG_FUNCTION_ARGS)
                       format_type_be(element_type))));
 
        my_extra->element_type = element_type;
-       fmgr_info(typentry->eq_opr_finfo.fn_oid, &my_extra->proc);
+       fmgr_info_cxt(typentry->eq_opr_finfo.fn_oid, &my_extra->proc,
+                     fcinfo->flinfo->fn_mcxt);
    }
 
    /*
index baccca14afdf13cacef79b25998c90f8b3475b42..59e4472e4fd5c40ccfecdef31dd42d8e9b95fa68 100644 (file)
@@ -589,6 +589,20 @@ SELECT array_positions('[2:4]={1,2,3}'::int[], 1);
  {2}
 (1 row)
 
+SELECT
+    array_position(ids, (1, 1)),
+    array_positions(ids, (1, 1))
+        FROM
+(VALUES
+    (ARRAY[(0, 0), (1, 1)]),
+    (ARRAY[(1, 1)])
+) AS f (ids);
+ array_position | array_positions 
+----------------+-----------------
+              2 | {2}
+              1 | {1}
+(2 rows)
+
 -- operators
 SELECT a FROM arrtest WHERE b = ARRAY[[[113,142],[1,147]]];
        a       
index a2c3db112742f093e37989961f9e40d6829707fe..2fbc699f6049b47fab7509430ff92ad07c523041 100644 (file)
@@ -262,6 +262,15 @@ $$ LANGUAGE plpgsql;
 SELECT array_position('[2:4]={1,2,3}'::int[], 1);
 SELECT array_positions('[2:4]={1,2,3}'::int[], 1);
 
+SELECT
+    array_position(ids, (1, 1)),
+    array_positions(ids, (1, 1))
+        FROM
+(VALUES
+    (ARRAY[(0, 0), (1, 1)]),
+    (ARRAY[(1, 1)])
+) AS f (ids);
+
 -- operators
 SELECT a FROM arrtest WHERE b = ARRAY[[[113,142],[1,147]]];
 SELECT NOT ARRAY[1.1,1.2,1.3] = ARRAY[1.1,1.2,1.3] AS "FALSE";