Skip to content

Commit 8557a6f

Browse files
committed
Catch invalid typlens in a couple of places
Rearrange the logic in record_image_cmp() and datum_image_eq() to error out on unexpected typlens (either not supported there or completely invalid due to corruption). Barring corruption, this is not possible today but it seems more future-proof and robust to fix this. Reported-by: Peter Geoghegan <pg@bowt.ie>
1 parent db27b60 commit 8557a6f

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

src/backend/utils/adt/datum.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,17 @@ datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
265265
{
266266
bool result = true;
267267

268-
if (typLen == -1)
268+
if (typByVal)
269+
{
270+
result = (value1 == value2);
271+
}
272+
else if (typLen > 0)
273+
{
274+
result = (memcmp(DatumGetPointer(value1),
275+
DatumGetPointer(value2),
276+
typLen) == 0);
277+
}
278+
else if (typLen == -1)
269279
{
270280
Size len1,
271281
len2;
@@ -294,16 +304,8 @@ datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
294304
pfree(arg2val);
295305
}
296306
}
297-
else if (typByVal)
298-
{
299-
result = (value1 == value2);
300-
}
301307
else
302-
{
303-
result = (memcmp(DatumGetPointer(value1),
304-
DatumGetPointer(value2),
305-
typLen) == 0);
306-
}
308+
elog(ERROR, "unexpected typLen: %d", typLen);
307309

308310
return result;
309311
}

src/backend/utils/adt/rowtypes.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,18 @@ record_image_cmp(FunctionCallInfo fcinfo)
14431443
}
14441444

14451445
/* Compare the pair of elements */
1446-
if (att1->attlen == -1)
1446+
if (att1->attbyval)
1447+
{
1448+
if (values1[i1] != values2[i2])
1449+
cmpresult = (values1[i1] < values2[i2]) ? -1 : 1;
1450+
}
1451+
else if (att1->attlen > 0)
1452+
{
1453+
cmpresult = memcmp(DatumGetPointer(values1[i1]),
1454+
DatumGetPointer(values2[i2]),
1455+
att1->attlen);
1456+
}
1457+
else if (att1->attlen == -1)
14471458
{
14481459
Size len1,
14491460
len2;
@@ -1466,17 +1477,8 @@ record_image_cmp(FunctionCallInfo fcinfo)
14661477
if ((Pointer) arg2val != (Pointer) values2[i2])
14671478
pfree(arg2val);
14681479
}
1469-
else if (att1->attbyval)
1470-
{
1471-
if (values1[i1] != values2[i2])
1472-
cmpresult = (values1[i1] < values2[i2]) ? -1 : 1;
1473-
}
14741480
else
1475-
{
1476-
cmpresult = memcmp(DatumGetPointer(values1[i1]),
1477-
DatumGetPointer(values2[i2]),
1478-
att1->attlen);
1479-
}
1481+
elog(ERROR, "unexpected attlen: %d", att1->attlen);
14801482

14811483
if (cmpresult < 0)
14821484
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy