8000 Make replace(), split_part(), and string_to_array() behave somewhat s… · danielcode/postgres@c556447 · GitHub
[go: up one dir, main page]

Skip to content

Commit c556447

Browse files
committed
Make replace(), split_part(), and string_to_array() behave somewhat sanely
when handed an invalidly-encoded pattern. The previous coding could get into an infinite loop if pg_mb2wchar_with_len() returned a zero-length string after we'd tested for nonempty pattern; which is exactly what it will do if the string consists only of an incomplete multibyte character. This led to either an out-of-memory error or a backend crash depending on platform. Per report from Wiktor Wodecki.
1 parent f1dda4c commit c556447

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/backend/utils/adt/varlena.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.92.2.5 2006/05/21 20:07:11 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.92.2.6 2007/07/19 20:34:54 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -698,20 +698,23 @@ text_position(Datum str, Datum search_str, int matchnum)
698698
(void) pg_mb2wchar_with_len((unsigned char *) VARDATA(t2), p2, len2);
699699
len2 = pg_wchar_strlen(p2);
700700

701-
/* no use in searching str past point where search_str will fit */
702-
px = (len1 - len2);
703-
704-
for (p = 0; p <= px; p++)
701+
if (len1 > 0 && len2 > 0)
705702
{
706-
if ((*p2 == *p1) && (pg_wchar_strncmp(p1, p2, len2) == 0))
703+
/* no use in searching str past point where search_str will fit */
704+
px = (len1 - len2);
705+
706+
for (p = 0; p <= px; p++)
707707
{
708-
if (++match == matchnum)
708+
if ((*p2 == *p1) && (pg_wchar_strncmp(p1, p2, len2) == 0))
709709
{
710-
pos = p + 1;
711-
break;
710+
if (++match == matchnum)
711+
{
712+
pos = p + 1;
713+
break;
714+
}
712715
}
716+
p1++;
713717
}
714-
p1++;
715718
}
716719

717720
pfree(ps1);

0 commit comments

Comments
 (0)
0