From: Patrick Steinhardt <ps@pks.im>
To: Junio C Hamano <gitster@pobox.com>
Cc: Ghanshyam Thakkar <shyamthakkar001@gmail.com>,
Achu Luma <ach.lumap@gmail.com>,
git@vger.kernel.org, christian.couder@gmail.com,
Christian Couder <chriscool@tuxfamily.org>,
Johannes Schindelin <Johannes.Schindelin@gmx.de>
Subject: Re: [Outreachy][PATCH 2/2] Port helper/test-date.c to unit-tests/t-date.c
Date: Wed, 29 May 2024 07:49:49 +0200 [thread overview]
Message-ID: <ZlbB_T8DkgmPeWQp@tanuki> (raw)
In-Reply-To: <xmqq7cfd7ut0.fsf@gitster.g>
[-- Attachment #1: Type: text/plain, Size: 2741 bytes --]
On Tue, May 28, 2024 at 09:41:47AM -0700, Junio C Hamano wrote:
> Patrick Steinhardt <ps@pks.im> writes:
>
> > As I was debugging other Windows-specific issues in a VM already, Chris
> > asked me to also have a look at this issue. And indeed, most of the
> > tests fail deterministically. I also found a fix:
> > ...
> > - setenv("TZ", zone, 1);
> > + _putenv_s("TZ", zone);
> > tzset();
> > }
> >
> > I have no idea why that works though, and the fix is of course not
> > portable. But with this change, the timezones do get picked up by
> > `tzset()` and related date functions as expected.
>
> The header compat/mingw.h already talks about implementing its own
> replacement by making gitsetenv() call mingw_putenv().
>
> gitsetenv() emulates setenv() in terms of putenv(), and on Windows
> mingw_putenv() is what implements putenv(), so the difference you
> are observing is coming from the difference between mingw_putenv()
> and _putenv_s(), I would guess. As the former is isolated within
> compat/mingw.c, it would not involve any additional portability
> issues to redo the former in terms of the latter, I would imagine.
Ah, thanks for the pointer. And indeed, `mingw_putenv()` uses
`SetEnvironmentVariableW()` to provide the functionality. This is what
MSDN has to say [1]:
getenv and _putenv use the copy of the environment pointed to by the
global variable _environ to access the environment. getenv operates
only on the data structures accessible to the run-time library and
not on the environment "segment" created for the process by the
operating system. Therefore, programs that use the envp argument to
main or wmain may retrieve invalid information.
So calling `SetEnvironmentVariableW()` will not affect calls to
`getenv()`. See also issues like for example [2].
This works just fine for us because we also stub out `getenv()` to use
`GetEnvironmentVariableW()`, which is its counterpart. But everything
else in the C runtime that uses `getenv()` will not see the new values,
including our date-related functions. We don't ever set any of those
environment variables though, except for now in this new unit test.
Now the question is why we use `SetEnvironmentVariableW()` over
`_putenv_s`, and whether changing it would be safe. If the answer is a
strict "yes" then we could do that, but if it's a "maybe" then I'd
rather not want to change it just to make these unit tests work. In that
case, we might aim for a localized fix like the one I have posted.
Patrick
[1]: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getenv-wgetenv?view=msvc-170
[2]: https://github.com/curl/curl/issues/4774
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2024-05-29 5:49 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-05 16:25 [Outreachy][PATCH 1/2] date: refactor 64 bit prereq code into reusable functions Achu Luma
2024-02-05 16:25 ` [Outreachy][PATCH 2/2] Port helper/test-date.c to unit-tests/t-date.c Achu Luma
2024-03-28 12:35 ` Ghanshyam Thakkar
2024-03-28 16:35 ` Junio C Hamano
2024-05-28 13:20 ` Patrick Steinhardt
2024-05-28 16:41 ` Junio C Hamano
2024-05-29 5:49 ` Patrick Steinhardt [this message]
2024-06-27 6:49 ` Johannes Schindelin
2024-02-05 17:34 ` [Outreachy][PATCH 1/2] date: refactor 64 bit prereq code into reusable functions rsbecker
2024-02-06 8:39 ` Christian Couder
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZlbB_T8DkgmPeWQp@tanuki \
--to=ps@pks.im \
--cc=Johannes.Schindelin@gmx.de \
--cc=ach.lumap@gmail.com \
--cc=chriscool@tuxfamily.org \
--cc=christian.couder@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=shyamthakkar001@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).