• Brad King's avatar
    KWSys: Fix SystemTools environment memory handling (#13156) · 48aeb751
    Brad King authored
    The SystemTools::PutEnv function tries to provide the "putenv" API
    without leaking memory.  However, the kwsysDeletingCharVector singleton
    frees memory that may still be referenced by the environment table,
    having been placed there by putenv.  If any static destruction or
    processing by an external tool happens after the singleton is destroyed
    and accesses the environment it will read invalid memory.
    Replace use of putenv with setenv/unsetenv when available.  The latter
    manage internal copies of the values passed instead of referencing the
    original memory.  When setenv/unsetenv are not available use putenv with
    a singleton that removes its values from the environment before freeing
    their memory.  This requires an "unputenv" implementation.  On at least
    some platforms it must be written in terms of "putenv" because other
    APIs are not available and direct modification of the "environ" global
    is not safe (e.g. on Windows there is interaction with "wenviron").
    Fortunately either putenv("A=") or putenv("A") will remove "A" from the
    environment on these platforms.  On other platforms fall back to direct
    manipulation of "environ".
    Also add UnPutEnv to the API and add a test for the behavior of both.
testSystemTools.cxx 12.2 KB