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

Commit 5d2e1cc

Browse files
committed
Replace some strtok() with strsep()
strtok() considers adjacent delimiters to be one delimiter, which is arguably the wrong behavior in some cases. Replace with strsep(), which has the right behavior: Adjacent delimiters create an empty token. Affected by this are parsing of: - Stored SCRAM secrets ("SCRAM-SHA-256$<iterations>:<salt>$<storedkey>:<serverkey>") - ICU collation attributes ("und@colStrength=primary;colCaseLevel=yes") for ICU older than version 54 - PG_COLORS environment variable ("error=01;31:warning=01;35:note=01;36:locus=01") - pg_regress command-line options with comma-separated list arguments (--dbname, --create-role) (currently only used pg_regress_ecpg) Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com> Reviewed-by: David Steele <david@pgmasters.net> Discussion: https://www.postgresql.org/message-id/flat/79692bf9-17d3-41e6-b9c9-fc8c3944222a@eisentraut.org
1 parent 90c1ba5 commit 5d2e1cc

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

src/backend/libpq/auth-scram.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -608,16 +608,15 @@ parse_scram_secret(const char *secret, int *iterations,
608608
* SCRAM-SHA-256$<iterations>:<salt>$<storedkey>:<serverkey>
609609
*/
610610
v = pstrdup(secret);
611-
if ((scheme_str = strtok(v, "$")) == NULL)
611+
if ((scheme_str = strsep(&v, "$")) == NULL)
612612
goto invalid_secret;
613-
if ((iterations_str = strtok(NULL, ":")) == NULL)
613+
if ((iterations_str = strsep(&v, ":")) == NULL)
614614
goto invalid_secret;
615-
if ((salt_str = strtok(NULL, "$")) == NULL)
615+
if ((salt_str = strsep(&v, "$")) == NULL)
616616
goto invalid_secret;
617-
if ((storedkey_str = strtok(NULL, ":")) == NULL)
618-
goto invalid_secret;
619-
if ((serverkey_str = strtok(NULL, "")) == NULL)
617+
if ((storedkey_str = strsep(&v, ":")) == NULL)
620618
goto invalid_secret;
619+
serverkey_str = v;
621620

622621
/* Parse the fields */
623622
if (strcmp(scheme_str, "SCRAM-SHA-256") != 0)

src/backend/utils/adt/pg_locale.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2813,6 +2813,7 @@ icu_set_collation_attributes(UCollator *collator, const char *loc,
28132813
char *icu_locale_id;
28142814
char *lower_str;
28152815
char *str;
2816+
char *token;
28162817

28172818
/*
28182819
* The input locale may be a BCP 47 language tag, e.g.
@@ -2838,7 +2839,7 @@ icu_set_collation_attributes(UCollator *collator, const char *loc,
28382839
return;
28392840
str++;
28402841

2841-
for (char *token = strtok(str, ";"); token; token = strtok(NULL, ";"))
2842+
while ((token = strsep(&str, ";")))
28422843
{
28432844
char *e = strchr(token, '=');
28442845

src/common/logging.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ pg_logging_init(const char *argv0)
119119

120120
if (colors)
121121
{
122-
for (char *token = strtok(colors, ":"); token; token = strtok(NULL, ":"))
122+
char *token;
123+
124+
while ((token = strsep(&colors, ":")))
123125
{
124126
char *e = strchr(token, '=');
125127

src/test/regress/pg_regress.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,11 @@ static void
234234
split_to_stringlist(const char *s, const char *delim, _stringlist **listhead)
235235
{
236236
char *sc = pg_strdup(s);
237-
char *token = strtok(sc, delim);
237+
char *token;
238238

239-
while (token)
239+
while ((token = strsep(&sc, delim)))
240240
{
241241
add_stringlist_item(listhead, token);
242-
token = strtok(NULL, delim);
243242
}
244243
free(sc);
245244
}

0 commit comments

Comments
 (0)