Re: replace strtok()

Peter Eisentraut <peter@eisentraut.org>

From: Peter Eisentraut <peter@eisentraut.org>
To: Ranier Vilela <ranier.vf@gmail.com>, Alexander Lakhin <exclusion@gmail.com>
Cc: David Steele <david@pgmasters.net>, Michael Paquier <michael@paquier.xyz>, Tom Lane <tgl@sss.pgh.pa.us>, pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2024-10-16T07:45:33Z
Lists: pgsql-hackers
On 15.10.24 14:07, Ranier Vilela wrote:
>     I also wonder, if other places touched by 5d2e1cc11 need corrections
>     too.
>     I played with
>     PG_COLOR=always PG_COLORS="error=01;31" .../initdb
> 
>     and it looks like this free() call in pg_logging_init():
>                  char       *colors = strdup(pg_colors_env);
> 
>                  if (colors)
>                  {
>     ...
>                      while ((token = strsep(&colors, ":")))
>                      {
>     ...
>                      }
> 
>                      free(colors);
>                  }
>     gets null in colors.
> 
> Yeah, I also saw this usage, but I was waiting for a definition for the 
> first report.
> The solution IMO, would be the same.
> 
> diff --git a/src/common/logging.c b/src/common/logging.c
> index aedd1ae2d8..45b5316d48 100644
> --- a/src/common/logging.c
> +++ b/src/common/logging.c
> @@ -121,7 +121,7 @@ pg_logging_init(const char *argv0)
>    {
>    char   *token;
> 
> - while ((token = strsep(&colors, ":")))
> + while ((token = strsep(&colors, ":")) != NULL && colors != NULL)
>    {
>    char   *e = strchr(token, '=');
> The advantage of this change is that it would avoid processing 
> unnecessary tokens.

This wouldn't fix anything, I think.  If colors is NULL, then strsep() 
already returns NULL, so the added code does nothing.




Commits

  1. Fix memory leaks from incorrect strsep() uses

  2. Fix strsep() use for SCRAM secrets parsing

  3. Replace remaining strtok() with strtok_r()

  4. Windows replacement for strtok_r()

  5. Replace some strtok() with strsep()

  6. Add port/ replacement for strsep()