Skip to content

Commit db7e953

Browse files
committed
Plug memory leak in range_cmp function.
B-tree operators are not allowed to leak memory into the current memory context. Range_cmp leaked detoasted copies of the arguments. That caused a quick out-of-memory error when creating an index on a range column. Reported by Marian Krucina, bug #8468.
1 parent 769c36a commit db7e953

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/backend/utils/adt/rangetypes.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,16 +1120,22 @@ range_cmp(PG_FUNCTION_ARGS)
11201120

11211121
/* For b-tree use, empty ranges sort before all else */
11221122
if (empty1 && empty2)
1123-
PG_RETURN_INT32(0);
1123+
cmp = 0;
11241124
else if (empty1)
1125-
PG_RETURN_INT32(-1);
1125+
cmp = -1;
11261126
else if (empty2)
1127-
PG_RETURN_INT32(1);
1127+
cmp = 1;
1128+
else
1129+
{
1130+
cmp = range_cmp_bounds(typcache, &lower1, &lower2);
1131+
if (cmp == 0)
1132+
cmp = range_cmp_bounds(typcache, &upper1, &upper2);
1133+
}
11281134

1129-
if ((cmp = range_cmp_bounds(typcache, &lower1, &lower2)) != 0)
1130-
PG_RETURN_INT32(cmp);
1135+
PG_FREE_IF_COPY(r1, 0);
1136+
PG_FREE_IF_COPY(r2, 1);
11311137

1132-
PG_RETURN_INT32(range_cmp_bounds(typcache, &upper1, &upper2));
1138+
PG_RETURN_INT32(cmp);
11331139
}
11341140

11351141
/* inequality operators using the range_cmp function */

0 commit comments

Comments
 (0)