After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 679617 - win32: fix g_get_environ()
win32: fix g_get_environ()
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: gio
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2012-07-09 10:38 UTC by Marc-Andre Lureau
Modified: 2012-07-16 10:50 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
win32: fix g_get_environ() (1.25 KB, patch)
2012-07-09 10:38 UTC, Marc-Andre Lureau
reviewed Details | Review
win32: fix tests/environment (1.64 KB, patch)
2012-07-09 10:38 UTC, Marc-Andre Lureau
needs-work Details | Review
win32: fix g_get_environ() (1.28 KB, patch)
2012-07-10 15:04 UTC, Marc-Andre Lureau
committed Details | Review
win32: g_getenv() should return "" if variable exists and empty (1.16 KB, patch)
2012-07-10 15:04 UTC, Marc-Andre Lureau
rejected Details | Review
win32: g_getenv() should return "" if variable exists and empty (1.06 KB, patch)
2012-07-10 15:09 UTC, Marc-Andre Lureau
committed Details | Review

Description Marc-Andre Lureau 2012-07-09 10:38:46 UTC
See patches comments.
Comment 1 Marc-Andre Lureau 2012-07-09 10:38:49 UTC
Created attachment 218323 [details] [review]
win32: fix g_get_environ()

The current code create the strv array incorrectly, it is too big and
leaves invalid holes. This may result in crashes when freeing the
returned value.
Comment 2 Marc-Andre Lureau 2012-07-09 10:38:53 UTC
Created attachment 218324 [details] [review]
win32: fix tests/environment

On Windows, getenv() returns NULL for empty variables.
Comment 3 Dan Winship 2012-07-10 13:47:59 UTC
Comment on attachment 218323 [details] [review]
win32: fix g_get_environ()

>+  for (n = 0, i = 0; strings[n]; n += wcslen (strings + n) + 1, i++);

Put the closing ";" on a separate line, to make it clear that the loop is intentionally empty. (Yeah, the existing code was bad style.) Better yet, reword to make the loop body non-empty.

Otherwise seems right.
Comment 4 Dan Winship 2012-07-10 13:59:58 UTC
Comment on attachment 218324 [details] [review]
win32: fix tests/environment

>+ * references to other environment variables, they are expanded. On
>+ * Windows, the return value is %NULL if the variable is empty.

It seems like this is probably an artifact of how the G_OS_WIN32 g_getenv() behaves; in the "len == 0" case, it needs to check if GetLastError() returns ERROR_ENVVAR_NOT_FOUND; if not, then it means the variable exists but is 0-length.
Comment 5 Marc-Andre Lureau 2012-07-10 15:04:33 UTC
Created attachment 218430 [details] [review]
win32: fix g_get_environ()

The current code create the strv array incorrectly, it is too big and
leaves invalid holes. This may result in crashes when freeing the
returned value.
Comment 6 Marc-Andre Lureau 2012-07-10 15:04:36 UTC
Created attachment 218431 [details] [review]
win32: g_getenv() should return "" if variable exists and empty

On Windows, GetEnvironmentVariable() returns 0 for empty variables.
Checking GetLastError() == ERROR_ENVVAR_NOT_FOUND helps make a
difference between a variable that does not exist or an empty one
which should return "".
Comment 7 Marc-Andre Lureau 2012-07-10 15:06:13 UTC
Review of attachment 218431 [details] [review]:

::: glib/genviron.c
@@ +469,3 @@
+      if (GetLastError () == ERROR_ENVVAR_NOT_FOUND)
+        return NULL;
+      value = "";

eh, that's bad, fixing
Comment 8 Marc-Andre Lureau 2012-07-10 15:09:31 UTC
Created attachment 218432 [details] [review]
win32: g_getenv() should return "" if variable exists and empty

On Windows, GetEnvironmentVariable() returns 0 for empty variables.
Checking GetLastError() == ERROR_ENVVAR_NOT_FOUND helps make a
difference between a variable that does not exist or an empty one
which should return "".
Comment 9 Dan Winship 2012-07-11 13:10:25 UTC
Comment on attachment 218432 [details] [review]
win32: g_getenv() should return "" if variable exists and empty

>+      quark = g_quark_from_static_string ("");
>+      return g_quark_to_string (quark);

you don't need to do that; you can just: return "";

ok to commit like that
Comment 10 Marc-Andre Lureau 2012-07-16 10:50:15 UTC
Attachment 218430 [details] pushed as 6007a4b - win32: fix g_get_environ()
Attachment 218432 [details] pushed as bfbfbec - win32: g_getenv() should return "" if variable exists and empty