replacing getenv/setenv on Windows - ticket 2636

Jeffrey Altman jaltman at MIT.EDU
Thu Sep 21 10:40:21 EDT 2006

*** I added the following to ticket 2636 last night. ***

I would like to bring this topic back to the foreground.
As a refresher, the Kerberos libraries and applications make
extensive use of getenv() and setenv() for manipulating the
environment.  On Windows this is problem because the getenv
and setenv CRT functions do not access the real environment
of the application.  Instead they manipulate a private
implementation of environ[].

There are several mechanisms via which we can address this
issue.  Here is a proposal for implementation:

We leave the POSIX systems alone and for Windows implement
new support functions:

       const char * win32_getenv(const char * var)
       int win32_setenv(const char *var, const char *value)

and a macro


    win32_getenv() would be implemented in terms of the Win32
    GetEnvironmentVariable API.  This API can be called in two
    ways.  First it can be used to obtain the required buffer
    size and secondly to obtain the value in a provided buffer.
    win32_getenv() would determine the appropriate buffer size,
    allocate the buffer with malloc(), and then obtain the value
    and return the buffer.  This buffer would then need to be
    freed by the caller.

   win32_setenv() would take a variable name and value and
   pass them to the SetEnvironmentVariable API

On Windows, the free_envstr macro would call an appropriate
function to free the allocated memory.

On POSIX systems, the free_envstr macro would be a no-op.

What do people think?

Jeffrey Altman

More information about the krbdev mailing list