Re: [PATCH] Memory leak, at src/common/exec.c - Mailing list pgsql-hackers

From Tom Lane
Subject Re: [PATCH] Memory leak, at src/common/exec.c
Date
Msg-id 27482.1576535596@sss.pgh.pa.us
Whole thread Raw
In response to Re: [PATCH] Memory leak, at src/common/exec.c  (Mark Dilger <hornschnorter@gmail.com>)
List pgsql-hackers
Mark Dilger <hornschnorter@gmail.com> writes:
> Please see the man page for putenv.  Are you certain it is safe to
> free the string passed to putenv after putenv returns?  I think this
> may be implemented differently on various platforms.

POSIX requires the behavior the glibc man page describes:

    The putenv() function shall use the string argument to set environment
    variable values. The string argument should point to a string of the
    form "name=value". The putenv() function shall make the value of
    the environment variable name equal to value by altering an existing
    variable or creating a new one. In either case, the string pointed to
    by string shall become part of the environment, so altering the string
    shall change the environment.

So yeah, that patch is completely wrong.  It might've survived light
testing with non-debug versions of malloc/free, but under any sort
of load the environment variable would become corrupted.  The reason
for the strdup in our code is exactly to make a long-lived string
that can safely be given to putenv.

            regards, tom lane



pgsql-hackers by date:

Previous
From: Tomas Vondra
Date:
Subject: Re: ERROR: could not resize shared memory segment...No space lefton device
Next
From: Ranier Vilela
Date:
Subject: RE: [PATCH] Memory leak, at src/common/exec.c