-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unix.environment on Windows: use _wenviron #1369
Conversation
otherlibs/win32unix/envir.c
Outdated
@@ -21,23 +21,26 @@ | |||
#include <caml/osdeps.h> | |||
|
|||
#include <Windows.h> | |||
#include <Stdlib.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stdlib (lowercase) is more standard (also for windows.h, btw) in our codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, an quick check in the museum shows that Microsoft use Windows.h and stdlib.h (or, of course, STDLIB.H if you're very, very old...)
However, the MinGW headers use windows.h so it's anyone's guess really... let's hope no one is crazy enough to try running the kernel in case sensitive mode 😄
otherlibs/win32unix/envir.c
Outdated
v = caml_copy_string_of_utf16(p); | ||
Store_field(result, i ++, v); | ||
if (_wenviron != NULL) { | ||
nbr = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cannot one use caml_alloc_array as caml_copy_string_array does (passing caml_copy_string_of_utf16 instead)?
Could you create a test that would fail before this PR is merged? |
The point of the PR is that it should be possible - except that today I can't seem to reproduce the failure locally, mmmph... Will investigate more, see if I can figure out what is going on. In any case, it seems weird to use the native Win32 API for just one of the calls instead of all three. |
At least for the ANSI version, my understanding of MPR4499 is that putenv indeed sends the new value to the "real "environment" (the one manipulated through GetEnvironmentVariable and co). If this is also the case for the W version of putenv, everything would be ok (although I understand it is not very nice to mix the two kinds of functions). It would actually be a bit "better" since this would allow to see changes performed through SetEnvironmentVariable (e.g. from .NET). But yes, it would actually be confusing to see changes that are invisible to getenv... |
Ugh, it's one of many ugly sections of the Microsoft C runtime! It feels somewhat icky because _environ and _wenviron are deprecated (however, MSVC also provides no enumeration functions, as far as I'm aware, so go figure!). OCaml is supposed to sit atop a C runtime, even if it's an often irritating one, so I agree that correcting this one function is the way to go. |
Do I need to add an entry to Changes? |
Perhaps mention it together with the entry for #1200. |
986ad5f
to
6b4326f
Compare
Ah yes, of course! |
otherlibs/win32unix/envir.c
Outdated
wchar_t * envp, * p; | ||
int size, i; | ||
CAMLparam0 (); | ||
CAMLlocal2 (v, result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi!
Are the spaces after the function names introduced by design?
It's on line 28, 29, 36.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment; and not, not really. Will fix it and give AppVeyor an excuse to wake up again.
674328c
to
7822d54
Compare
6b4326f
to
652c6f5
Compare
@alainfrisch suggested a possibly simpler implementation. Please tell why it would not work. |
Yes, sorry about that, I forgot to post an answer here. The reason was that
(we need a version with Anyone see a way around this? |
Of course I can just cast the argument to use it (in fact I already used this once: https://github.com/ocaml/ocaml/blob/trunk/byterun/sys.c#L362). Shall I do that? |
652c6f5
to
5af73a7
Compare
OK, absent any explicit decision I went ahead and added a commit using |
Is it possible/practical to add a regression test for the bug you are fixing? |
Yes, it is! I added a test ( |
Is someone willing to review and approve the PR? ( @alainfrisch, @dra27 ? ) |
feda74a
to
37ae174
Compare
let foo = "FOO" | ||
|
||
let () = | ||
set_environment_variable foo "BAR"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good to have this test (disabled), but perhaps indicates somehow that it is related to MPR#4499, not the current PR.
Also, it might be good to use non-ASCII characters in the string.
LGTM (modulo a minor comment about the disabled test). Waiting for the CI green lights. |
37ae174
to
1d9079a
Compare
Rebased and amended as suggested:
|
- Add lib-unix/win-env/test_env.ml, a test that fails in trunk and passes in this PR. - Add lib-unix/win-env/test_env2.ml: a test for MPR#4499 which is closely related to this PR. Currently disabled as it fails. - Added a new test in win-unicode/mltest.ml to test non-ASCII characters returned by Unix.environment (Unix.{getenv,putenv} were already being tested).
1d9079a
to
37e7fe1
Compare
( @alainfrisch does your comment count as approval? Let's follow the process as it was suggested. ) |
@damiendoligez @gasche I'd like to cherry-pick this to 4.06. Do you agree? |
Yes, I think it's good to have it in the first release with #1200. I would encourage you to include it in 4.06. |
* Unix.environment on Windows: use _wenviron * Add Changes entry * Update testsuite - Add lib-unix/win-env/test_env.ml, a test that fails in trunk and passes in this PR. - Add lib-unix/win-env/test_env2.ml: a test for MPR#4499 which is closely related to this PR. Currently disabled as it fails. - Added a new test in win-unicode/mltest.ml to test non-ASCII characters returned by Unix.environment (Unix.{getenv,putenv} were already being tested).
Cherry-picked to 4.06 as 990845b. |
One more rebase fix following #1200.
The existing
Unix.environment
intrunk
uses the native Win32 APIGetEnvironmentStrings
which works OK except that its result can sometimes get out of sync with the result of the "POSIX" API_wgetenv
used forUnix.{get,put}env
(see MPR#4499 for details).The mixup ocurred because at some point we had switched to using the native Win32 API in all three functions but later decided to leave that change for later.
This patch reverts
Unix.environment
to read from the global variable_wenviron
, similar to the analogous implementation for Unix.