Skip to content

Commit efe2222

Browse files
committed
Eliminate some no-longer-needed workarounds for palloc's old behavior
of rejecting palloc(0). Also, tweak like_selectivity() to avoid assuming the presented pattern is nonempty; although that assumption is valid, it doesn't really help much, and the new coding is more correct anyway since it properly handles redundant wildcards. In combination these changes should eliminate a Coverity warning noted by Martijn.
1 parent ea6d54e commit efe2222

File tree

1 file changed

+21
-37
lines changed

1 file changed

+21
-37
lines changed

src/backend/utils/adt/selfuncs.c

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.198 2006/03/05 15:58:44 momjian Exp $
18+
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.199 2006/04/20 17:50:18 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -3736,14 +3736,8 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive,
37363736
bytea *bstr = DatumGetByteaP(patt_const->constvalue);
37373737

37383738
pattlen = VARSIZE(bstr) - VARHDRSZ;
3739-
if (pattlen > 0)
3740-
{
3741-
patt = (char *) palloc(pattlen);
3742-
memcpy(patt, VARDATA(bstr), pattlen);
3743-
}
3744-
else
3745-
patt = NULL;
3746-
3739+
patt = (char *) palloc(pattlen);
3740+
memcpy(patt, VARDATA(bstr), pattlen);
37473741
if ((Pointer) bstr != DatumGetPointer(patt_const->constvalue))
37483742
pfree(bstr);
37493743
}
@@ -3761,7 +3755,7 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive,
37613755
if (patt[pos] == '\\')
37623756
{
37633757
pos++;
3764-
if (patt[pos] == '\0' && typeid != BYTEAOID)
3758+
if (pos >= pattlen)
37653759
break;
37663760
}
37673761

@@ -3794,8 +3788,7 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive,
37943788
*rest_const = string_to_bytea_const(rest, pattlen - pos);
37953789
}
37963790

3797-
if (patt != NULL)
3798-
pfree(patt);
3791+
pfree(patt);
37993792
pfree(match);
38003793

38013794
/* in LIKE, an empty pattern is an exact match! */
@@ -4101,7 +4094,6 @@ like_selectivity(Const *patt_const, bool case_insensitive)
41014094
{
41024095
Selectivity sel = 1.0;
41034096
int pos;
4104-
int start;
41054097
Oid typeid = patt_const->consttype;
41064098
char *patt;
41074099
int pattlen;
@@ -4124,23 +4116,20 @@ like_selectivity(Const *patt_const, bool case_insensitive)
41244116
bytea *bstr = DatumGetByteaP(patt_const->constvalue);
41254117

41264118
pattlen = VARSIZE(bstr) - VARHDRSZ;
4127-
if (pattlen > 0)
4128-
{
4129-
patt = (char *) palloc(pattlen);
4130-
memcpy(patt, VARDATA(bstr), pattlen);
4131-
}
4132-
else
4133-
patt = NULL;
4134-
4119+
patt = (char *) palloc(pattlen);
4120+
memcpy(patt, VARDATA(bstr), pattlen);
41354121
if ((Pointer) bstr != DatumGetPointer(patt_const->constvalue))
41364122
pfree(bstr);
41374123
}
4138-
/* patt should never be NULL in practice */
4139-
Assert(patt != NULL);
41404124

4141-
/* Skip any leading %; it's already factored into initial sel */
4142-
start = (*patt == '%') ? 1 : 0;
4143-
for (pos = start; pos < pattlen; pos++)
4125+
/* Skip any leading wildcard; it's already factored into initial sel */
4126+
for (pos = 0; pos < pattlen; pos++)
4127+
{
4128+
if (patt[pos] != '%' && patt[pos] != '_')
4129+
break;
4130+
}
4131+
4132+
for (; pos < pattlen; pos++)
41444133
{
41454134
/* % and _ are wildcard characters in LIKE */
41464135
if (patt[pos] == '%')
@@ -4151,7 +4140,7 @@ like_selectivity(Const *patt_const, bool case_insensitive)
41514140
{
41524141
/* Backslash quotes the next character */
41534142
pos++;
4154-
if (patt[pos] == '\0' && typeid != BYTEAOID)
4143+
if (pos >= pattlen)
41554144
break;
41564145
sel *= FIXED_CHAR_SEL;
41574146
}
@@ -4161,6 +4150,8 @@ like_selectivity(Const *patt_const, bool case_insensitive)
41614150
/* Could get sel > 1 if multiple wildcards */
41624151
if (sel > 1.0)
41634152
sel = 1.0;
4153+
4154+
pfree(patt);
41644155
return sel;
41654156
}
41664157

@@ -4366,14 +4357,8 @@ make_greater_string(const Const *str_const)
43664357
bytea *bstr = DatumGetByteaP(str_const->constvalue);
43674358

43684359
len = VARSIZE(bstr) - VARHDRSZ;
4369-
if (len > 0)
4370-
{
4371-
workstr = (char *) palloc(len);
4372-
memcpy(workstr, VARDATA(bstr), len);
4373-
}
4374-
else
4375-
workstr = NULL;
4376-
4360+
workstr = (char *) palloc(len);
4361+
memcpy(workstr, VARDATA(bstr), len);
43774362
if ((Pointer) bstr != DatumGetPointer(str_const->constvalue))
43784363
pfree(bstr);
43794364
}
@@ -4429,8 +4414,7 @@ make_greater_string(const Const *str_const)
44294414
}
44304415

44314416
/* Failed... */
4432-
if (workstr != NULL)
4433-
pfree(workstr);
4417+
pfree(workstr);
44344418

44354419
return NULL;
44364420
}

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