bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

setenv, unsetenv: On native Windows, don't modify _environ directly


From: Bruno Haible
Subject: setenv, unsetenv: On native Windows, don't modify _environ directly
Date: Thu, 06 Jun 2024 03:02:08 +0200

On a GitHub CI machine, I see the nstrftime tests
  - succeed on mingw,
  - but fail on MSVC.

The failure output is:

  PST8PDT: expected "1969-12-31 16:00:00 -0800 (PST)", got "1970-01-01 00:00:00 
+0000 ()"
  MST7: expected "1969-12-31 17:00:00 -0700 (MST)", got "1970-01-01 00:00:00 
+0000 ()"
  CET-1CEST: expected "1970-01-01 01:00:00 +0100 (CET)", got "1970-01-01 
00:00:00 +0000 ()"
  JST-9: expected "1970-01-01 09:00:00 +0900 (JST)", got "1970-01-01 00:00:00 
+0000 ()"
  NST-13NDT: expected "1970-01-01 13:00:00 +1300 (NZDT)", got "1970-01-01 
00:00:00 +0000 ()"
  PST8PDT: expected "1985-11-04 16:53:21 -0800 (PST)", got "1985-11-05 00:53:21 
+0000 ()"
  MST7: expected "1985-11-04 17:53:21 -0700 (MST)", got "1985-11-05 00:53:21 
+0000 ()"
  CET-1CEST: expected "1985-11-05 01:53:21 +0100 (CET)", got "1985-11-05 
00:53:21 +0000 ()"
  JST-9: expected "1985-11-05 09:53:21 +0900 (JST)", got "1985-11-05 00:53:21 
+0000 ()"
  NST-13NDT: expected "1985-11-05 13:53:21 +1300 (NZDT)", got "1985-11-05 
00:53:21 +0000 ()"
  MST7: expected "2001-09-08 18:46:42 -0700 (MST)", got "2001-09-09 01:46:42 
+0000 ()"
  JST-9: expected "2001-09-09 10:46:42 +0900 (JST)", got "2001-09-09 01:46:42 
+0000 ()"

which indicates that in all cases, UTC has been used instead of the
timezone from the test.

The cause is that
  * the test uses the setenv(), unsetenv() replacements by Gnulib (since
    MSVC has only _putenv, no setenv, no unsetenv),
  * these replacement manipulate the contents of _environ, but have
    no effect on _wenviron (unlike _putenv, which modifies both),
  * and apparently on this machine, MSVC uses a Microsoft CRT that looks
    at _wgetenv (L"TZ"), not getenv ("TZ").

The fix is to change setenv and unsetenv to use _putenv(). The patches below
do this, and also improve comments, fix a possible out-of-memory crash,
and reduce code duplication.


2024-06-05  Bruno Haible  <bruno@clisp.org>

        setenv: On native Windows, don't modify _environ directly.
        * m4/setenv.m4 (gl_PREREQ_SETENV): Check for _putenv.
        * lib/setenv.c: On native Windows, include <windows.h> and define
        SetEnvironmentVariable.
        (setenv) [HAVE_DECL__PUTENV]: New implementation for platforms with
        _putenv.
        * modules/setenv (Depends-on): Add malloc-posix.

2024-06-05  Bruno Haible  <bruno@clisp.org>

        unsetenv: On native Windows, don't modify _environ directly.
        * m4/setenv.m4 (gl_PREREQ_UNSETENV): Check for _putenv.
        * lib/unsetenv.c (unsetenv): Add native Windows handling, from
        lib/putenv.c.
        * modules/unsetenv (Depends-on): Add free-posix, malloc-posix.
        * m4/putenv.m4 (gl_FUNC_PUTENV): Use AC_CHECK_DECLS_ONCE.
        * lib/putenv.c (_unsetenv): Moved to lib/unsetenv.c.
        (putenv): Invoke unsetenv instead of _unsetenv.
        * modules/putenv-gnu (Depends-on): Add unsetenv.

2024-06-05  Bruno Haible  <bruno@clisp.org>

        putenv: Don't crash upon out-of-memory.
        * lib/putenv.c (_unsetenv): Handle malloc failure.

2024-06-05  Bruno Haible  <bruno@clisp.org>

        putenv: Improve comments.
        * lib/putenv.c (_unsetenv, putenv): Improve comments regarding native
        Windows.

Attachment: 0001-putenv-Improve-comments.patch
Description: Text Data

Attachment: 0002-putenv-Don-t-crash-upon-out-of-memory.patch
Description: Text Data

Attachment: 0003-unsetenv-On-native-Windows-don-t-modify-_environ-dir.patch
Description: Text Data

Attachment: 0004-setenv-On-native-Windows-don-t-modify-_environ-direc.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]