Re: replace strtok()

From: Peter Eisentraut <peter(at)eisentraut(dot)org>
To: Alexander Lakhin <exclusion(at)gmail(dot)com>, Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
Cc: David Steele <david(at)pgmasters(dot)net>, Michael Paquier <michael(at)paquier(dot)xyz>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: replace strtok()
Date: 2024-10-16 07:42:22
Message-ID: f693a885-f456-42f1-a407-9b0ada4b62c4@eisentraut.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 15.10.24 14:00, Alexander Lakhin 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.

Yes, this is indeed incorrect. We need to keep a separate pointer to
the start of the string to free later. This matches the example on the
strsep man page (https://man.freebsd.org/cgi/man.cgi?strsep(3)) Patch
attached.

Attachment Content-Type Size
0001-Fix-memory-leaks-from-incorrect-strsep-uses.patch text/plain 1.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andy Fan 2024-10-16 07:43:27 Re: New function normal_rand_array function to contrib/tablefunc.
Previous Message Anthonin Bonnefoy 2024-10-16 07:12:52 Re: Set query_id for query contained in utility statement