Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8527132

Browse files
committed
Tighten checks for whitespace in functions that parse identifiers etc.
This patch replaces isspace() calls with scanner_isspace() in functions that are likely to be presented with non-ASCII input. isspace() has the small advantage that it will correctly recognize no-break space in single-byte encodings (such as LATIN1); but it cannot work successfully for any multibyte character, and depending on platform it might return false positive results for some fragments of multibyte characters. That's disastrous for functions that are trying to discard whitespace between valid strings, as noted in bug #14662 from Justin Muise. Even treating no-break space as whitespace is pretty questionable for the usages touched here, because the core scanner would think it is an identifier character. Affected functions are parse_ident(), parseNameAndArgTypes (underlying regprocedurein() and siblings), SplitIdentifierString (used for parsing GUCs and options that are qualified names or lists of names), and SplitDirectoriesString (used for parsing GUCs that are lists of directories). All the functions adjusted here are parsing SQL identifiers and similar constructs, so it's reasonable to insist that their definition of whitespace match the core scanner. So we can hope that this won't cause many backwards-compatibility problems. I've left alone isspace() calls in places that aren't really expecting any non-ASCII input characters, such as float8in(). Back-patch to all supported branches. Discussion: https://postgr.es/m/10129.1495302480@sss.pgh.pa.us
1 parent d8ba357 commit 8527132

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/backend/utils/adt/misc.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ parse_ident(PG_FUNCTION_ARGS)
775775
nextp = qualname_str;
776776

777777
/* skip leading whitespace */
778-
while (isspace((unsigned char) *nextp))
778+
while (scanner_isspace(*nextp))
779779
nextp++;
780780

781781
for (;;)
@@ -863,14 +863,14 @@ parse_ident(PG_FUNCTION_ARGS)
863863
text_to_cstring(qualname))));
864864
}
865865

866-
while (isspace((unsigned char) *nextp))
866+
while (scanner_isspace(*nextp))
867867
nextp++;
868868

869869
if (*nextp == '.')
870870
{
871871
after_dot = true;
872872
nextp++;
873-
while (isspace((unsigned char) *nextp))
873+
while (scanner_isspace(*nextp))
874874
nextp++;
875875
}
876876
else if (*nextp == '\0')

src/backend/utils/adt/regproc.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "lib/stringinfo.h"
3636
#include "miscadmin.h"
3737
#include "parser/parse_type.h"
38+
#include "parser/scansup.h"
3839
#include "utils/builtins.h"
3940
#include "utils/fmgroids.h"
4041
#include "utils/lsyscache.h"
@@ -1911,7 +1912,7 @@ parseNameAndArgTypes(const char *string, bool allowNone, List **names,
19111912
ptr2 = ptr + strlen(ptr);
19121913
while (--ptr2 > ptr)
19131914
{
1914-
if (!isspace((unsigned char) *ptr2))
1915+
if (!scanner_isspace(*ptr2))
19151916
break;
19161917
}
19171918
if (*ptr2 != ')')
@@ -1928,7 +1929,7 @@ parseNameAndArgTypes(const char *string, bool allowNone, List **names,
19281929
for (;;)
19291930
{
19301931
/* allow leading whitespace */
1931-
while (isspace((unsigned char) *ptr))
1932+
while (scanner_isspace(*ptr))
19321933
ptr++;
19331934
if (*ptr == '\0')
19341935
{
@@ -1984,7 +1985,7 @@ parseNameAndArgTypes(const char *string, bool allowNone, List **names,
19841985
/* Lop off trailing whitespace */
19851986
while (--ptr2 >= typename)
19861987
{
1987-
if (!isspace((unsigned char) *ptr2))
1988+
if (!scanner_isspace(*ptr2))
19881989
break;
19891990
*ptr2 = '\0';
19901991
}

src/backend/utils/adt/varlena.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -3133,7 +3133,7 @@ SplitIdentifierString(char *rawstring, char separator,
31333133

31343134
*namelist = NIL;
31353135

3136-
while (isspace((unsigned char) *nextp))
3136+
while (scanner_isspace(*nextp))
31373137
nextp++; /* skip leading whitespace */
31383138

31393139
if (*nextp == '\0')
@@ -3171,7 +3171,7 @@ SplitIdentifierString(char *rawstring, char separator,
31713171

31723172
curname = nextp;
31733173
while (*nextp && *nextp != separator &&
3174-
!isspace((unsigned char) *nextp))
3174+
!scanner_isspace(*nextp))
31753175
nextp++;
31763176
endp = nextp;
31773177
if (curname == nextp)
@@ -3193,13 +3193,13 @@ SplitIdentifierString(char *rawstring, char separator,
31933193
pfree(downname);
31943194
}
31953195

3196-
while (isspace((unsigned char) *nextp))
3196+
while (scanner_isspace(*nextp))
31973197
nextp++; /* skip trailing whitespace */
31983198

31993199
if (*nextp == separator)
32003200
{
32013201
nextp++;
3202-
while (isspace((unsigned char) *nextp))
3202+
while (scanner_isspace(*nextp))
32033203
nextp++; /* skip leading whitespace for next */
32043204
/* we expect another name, so done remains false */
32053205
}
@@ -3258,7 +3258,7 @@ SplitDirectoriesString(char *rawstring, char separator,
32583258

32593259
*namelist = NIL;
32603260

3261-
while (isspace((unsigned char) *nextp))
3261+
while (scanner_isspace(*nextp))
32623262
nextp++; /* skip leading whitespace */
32633263

32643264
if (*nextp == '\0')
@@ -3295,21 +3295,21 @@ SplitDirectoriesString(char *rawstring, char separator,
32953295
while (*nextp && *nextp != separator)
32963296
{
32973297
/* trailing whitespace should not be included in name */
3298-
if (!isspace((unsigned char) *nextp))
3298+
if (!scanner_isspace(*nextp))
32993299
endp = nextp + 1;
33003300
nextp++;
33013301
}
33023302
if (curname == endp)
33033303
return false; /* empty unquoted name not allowed */
33043304
}
33053305

3306-
while (isspace((unsigned char) *nextp))
3306+
while (scanner_isspace(*nextp))
33073307
nextp++; /* skip trailing whitespace */
33083308

33093309
if (*nextp == separator)
33103310
{
33113311
nextp++;
3312-
while (isspace((unsigned char) *nextp))
3312+
while (scanner_isspace(*nextp))
33133313
nextp++; /* skip leading whitespace for next */
33143314
/* we expect another name, so done remains false */
33153315
}

0 commit comments

Comments
 (0)