bug-gnulib
[Top][All Lists]
Advanced

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

Re: Modification of environment variables on Windows


From: Bruno Haible
Subject: Re: Modification of environment variables on Windows
Date: Sat, 27 Apr 2024 16:11:43 +0200

Hi Markus,

> We recently updated gnulib in GNU Octave to a newer revision 
> (d4ec02b3cc70cddaaa5183cc5a45814e0afb2292). (Kudos on the impressive speedup 
> to the bootstrap process.)
> 
> Since then, we are seeing warnings like the following when building for MinGW:
> 
> ../../libgnu/tzset.c: In function 'rpl_tzset':
> ../../libgnu/tzset.c:68:24: warning: initialization of 'char *' from 
> incompatible pointer type 'char **' [-Wincompatible-pointer-types]
>    68 |         for (char *s = env; *s != NULL; s++)
>       |                        ^~~
> ../../libgnu/tzset.c:68:32: warning: comparison between pointer and integer
>    68 |         for (char *s = env; *s != NULL; s++)
>       |                                ^~
> ../../libgnu/tzset.c:72:28: warning: initialization of 'wchar_t *' {aka 
> 'short unsigned int *'} from incompatible pointer type 'wchar_t **' {aka 
> 'short unsigned int **'} [-Wincompatible-pointer-types]
>    72 |         for (wchar_t *ws = wenv; *ws != NULL; ws++)
>       |                            ^~~~
> ../../libgnu/tzset.c:72:38: warning: comparison between pointer and integer
>    72 |         for (wchar_t *ws = wenv; *ws != NULL; ws++)
>                                              ^~
> 
> IIUC, these warnings might be legitimate.

Thanks for the report. What a blunder, indeed!

> The attached patch avoids those warnings.

Thanks, but it does not do the right thing: *s[1] accesses the first character
of the string after s. What was meant was to access the second character of
the string at s; this needs to be written as (*s)[1].

I'm committing this patch instead.


2024-04-27  Bruno Haible  <bruno@clisp.org>

        ctime, localtime, tzset, wcsftime: Fix env access (regr. 2024-02-09).
        Reported by Markus Mützel <markus.muetzel@gmx.de> in
        <https://lists.gnu.org/archive/html/bug-gnulib/2024-04/msg00457.html>.
        * lib/ctime.c (rpl_ctime): Fix logic of environment traversal.
        * lib/localtime.c (rpl_localtime): Likewise.
        * lib/tzset.c (rpl_tzset): Likewise.
        * lib/wcsftime.c (rpl_wcsftime): Likewise.

diff --git a/lib/ctime.c b/lib/ctime.c
index 8c54ef463c..744b153260 100644
--- a/lib/ctime.c
+++ b/lib/ctime.c
@@ -63,13 +63,19 @@ rpl_ctime (const time_t *tp)
       char **env = _environ;
       wchar_t **wenv = _wenviron;
       if (env != NULL)
-        for (char *s = env; *s != NULL; s++)
-          if (s[0] == 'T' && s[1] == 'Z' && s[2] == '=')
-            s[0] = '$';
+        for (char **ep = env; *ep != NULL; ep++)
+          {
+            char *s = *ep;
+            if (s[0] == 'T' && s[1] == 'Z' && s[2] == '=')
+              s[0] = '$';
+          }
       if (wenv != NULL)
-        for (wchar_t *ws = wenv; *ws != NULL; ws++)
-          if (ws[0] == L'T' && ws[1] == L'Z' && ws[2] == L'=')
-            ws[0] = L'$';
+        for (wchar_t **wep = wenv; *wep != NULL; wep++)
+          {
+            wchar_t *ws = *wep;
+            if (ws[0] == L'T' && ws[1] == L'Z' && ws[2] == L'=')
+              ws[0] = L'$';
+          }
     }
 #endif
 
diff --git a/lib/localtime.c b/lib/localtime.c
index f0e91ac647..df0278e5c2 100644
--- a/lib/localtime.c
+++ b/lib/localtime.c
@@ -63,13 +63,19 @@ rpl_localtime (const time_t *tp)
       char **env = _environ;
       wchar_t **wenv = _wenviron;
       if (env != NULL)
-        for (char *s = env; *s != NULL; s++)
-          if (s[0] == 'T' && s[1] == 'Z' && s[2] == '=')
-            s[0] = '$';
+        for (char **ep = env; *ep != NULL; ep++)
+          {
+            char *s = *ep;
+            if (s[0] == 'T' && s[1] == 'Z' && s[2] == '=')
+              s[0] = '$';
+          }
       if (wenv != NULL)
-        for (wchar_t *ws = wenv; *ws != NULL; ws++)
-          if (ws[0] == L'T' && ws[1] == L'Z' && ws[2] == L'=')
-            ws[0] = L'$';
+        for (wchar_t **wep = wenv; *wep != NULL; wep++)
+          {
+            wchar_t *ws = *wep;
+            if (ws[0] == L'T' && ws[1] == L'Z' && ws[2] == L'=')
+              ws[0] = L'$';
+          }
     }
 #endif
 
diff --git a/lib/tzset.c b/lib/tzset.c
index f307f0c3d1..93dc52e284 100644
--- a/lib/tzset.c
+++ b/lib/tzset.c
@@ -65,13 +65,19 @@ rpl_tzset (void)
       char **env = _environ;
       wchar_t **wenv = _wenviron;
       if (env != NULL)
-        for (char *s = env; *s != NULL; s++)
-          if (s[0] == 'T' && s[1] == 'Z' && s[2] == '=')
-            s[0] = '$';
+        for (char **ep = env; *ep != NULL; ep++)
+          {
+            char *s = *ep;
+            if (s[0] == 'T' && s[1] == 'Z' && s[2] == '=')
+              s[0] = '$';
+          }
       if (wenv != NULL)
-        for (wchar_t *ws = wenv; *ws != NULL; ws++)
-          if (ws[0] == L'T' && ws[1] == L'Z' && ws[2] == L'=')
-            ws[0] = L'$';
+        for (wchar_t **wep = wenv; *wep != NULL; wep++)
+          {
+            wchar_t *ws = *wep;
+            if (ws[0] == L'T' && ws[1] == L'Z' && ws[2] == L'=')
+              ws[0] = L'$';
+          }
     }
 
   /* On native Windows, tzset() is deprecated.  Use _tzset() instead.  See
diff --git a/lib/wcsftime.c b/lib/wcsftime.c
index d8b471ab57..b1b3243271 100644
--- a/lib/wcsftime.c
+++ b/lib/wcsftime.c
@@ -61,13 +61,19 @@ rpl_wcsftime (wchar_t *buf, size_t bufsize, const wchar_t 
*format, const struct
       char **env = _environ;
       wchar_t **wenv = _wenviron;
       if (env != NULL)
-        for (char *s = env; *s != NULL; s++)
-          if (s[0] == 'T' && s[1] == 'Z' && s[2] == '=')
-            s[0] = '$';
+        for (char **ep = env; *ep != NULL; ep++)
+          {
+            char *s = *ep;
+            if (s[0] == 'T' && s[1] == 'Z' && s[2] == '=')
+              s[0] = '$';
+          }
       if (wenv != NULL)
-        for (wchar_t *ws = wenv; *ws != NULL; ws++)
-          if (ws[0] == L'T' && ws[1] == L'Z' && ws[2] == L'=')
-            ws[0] = L'$';
+        for (wchar_t **wep = wenv; *wep != NULL; wep++)
+          {
+            wchar_t *ws = *wep;
+            if (ws[0] == L'T' && ws[1] == L'Z' && ws[2] == L'=')
+              ws[0] = L'$';
+          }
     }
 #endif
 






reply via email to

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