[PATCH] Cygwin: build_env: fix off-by-one bug when re-adding PATH

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Cygwin: build_env: fix off-by-one bug when re-adding PATH

Michael Haubenwallner-2
Adding default winvar 'PATH=C:\cygwin64\binZ' to an environment that is
already allocated for 'SYSTEMROOT=ZWINDIR=Z', we need to count that
trailing (Z)ero as well.  Otherwise we trigger this assertion failure:

$ /bin/env -i SYSTEMROOT= WINDIR= /bin/env
assertion "(s - envblock) <= tl" failed: file "/home/corinna/src/cygwin/cygwin-3.0.7/cygwin-3.0.7-1.x86_64/src/newlib-cygwin/winsup/cygwin/environ.cc", line 1302, function: char** build_env(const char* const*, WCHAR*&, int&, bool, HANDLE)
Aborted (core dumped)
---
 winsup/cygwin/environ.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/winsup/cygwin/environ.cc b/winsup/cygwin/environ.cc
index 124842734..8fa01b2d5 100644
--- a/winsup/cygwin/environ.cc
+++ b/winsup/cygwin/environ.cc
@@ -1295,7 +1295,7 @@ build_env (const char * const *envp, PWCHAR &envblock, int &envc,
  during execve. */
       if (!saw_PATH)
  {
-  new_tl += cygheap->installation_dir.Length / sizeof (WCHAR) + 5;
+  new_tl += cygheap->installation_dir.Length / sizeof (WCHAR) + 5 + 1;
   if (new_tl > tl)
     tl = raise_envblock (new_tl, envblock, s);
   s = wcpcpy (wcpcpy (s, L"PATH="),
--
2.21.0

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Cygwin: build_env: fix off-by-one bug when re-adding PATH

Corinna Vinschen-2
On Aug  7 10:51, Michael Haubenwallner wrote:

> Adding default winvar 'PATH=C:\cygwin64\binZ' to an environment that is
> already allocated for 'SYSTEMROOT=ZWINDIR=Z', we need to count that
> trailing (Z)ero as well.  Otherwise we trigger this assertion failure:
>
> $ /bin/env -i SYSTEMROOT= WINDIR= /bin/env
> assertion "(s - envblock) <= tl" failed: file "/home/corinna/src/cygwin/cygwin-3.0.7/cygwin-3.0.7-1.x86_64/src/newlib-cygwin/winsup/cygwin/environ.cc", line 1302, function: char** build_env(const char* const*, WCHAR*&, int&, bool, HANDLE)
> Aborted (core dumped)
> ---
>  winsup/cygwin/environ.cc | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/winsup/cygwin/environ.cc b/winsup/cygwin/environ.cc
> index 124842734..8fa01b2d5 100644
> --- a/winsup/cygwin/environ.cc
> +++ b/winsup/cygwin/environ.cc
> @@ -1295,7 +1295,7 @@ build_env (const char * const *envp, PWCHAR &envblock, int &envc,
>   during execve. */
>        if (!saw_PATH)
>   {
> -  new_tl += cygheap->installation_dir.Length / sizeof (WCHAR) + 5;
> +  new_tl += cygheap->installation_dir.Length / sizeof (WCHAR) + 5 + 1;
>    if (new_tl > tl)
>      tl = raise_envblock (new_tl, envblock, s);
>    s = wcpcpy (wcpcpy (s, L"PATH="),
> --
> 2.21.0
Pushed.


Thanks,
Corinna

--
Corinna Vinschen
Cygwin Maintainer

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Cygwin: build_env: fix off-by-one bug when re-adding PATH

Brian Inglis
In reply to this post by Michael Haubenwallner-2
On 2019-08-07 02:51, Michael Haubenwallner wrote:

> Adding default winvar 'PATH=C:\cygwin64\binZ' to an environment that is
> already allocated for 'SYSTEMROOT=ZWINDIR=Z', we need to count that
> trailing (Z)ero as well.  Otherwise we trigger this assertion failure:
>
> $ /bin/env -i SYSTEMROOT= WINDIR= /bin/env
> assertion "(s - envblock) <= tl" failed: file "/home/corinna/src/cygwin/cygwin-3.0.7/cygwin-3.0.7-1.x86_64/src/newlib-cygwin/winsup/cygwin/environ.cc", line 1302, function: char** build_env(const char* const*, WCHAR*&, int&, bool, HANDLE)
> Aborted (core dumped)
> ---
>  winsup/cygwin/environ.cc | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/winsup/cygwin/environ.cc b/winsup/cygwin/environ.cc
> index 124842734..8fa01b2d5 100644
> --- a/winsup/cygwin/environ.cc
> +++ b/winsup/cygwin/environ.cc
> @@ -1295,7 +1295,7 @@ build_env (const char * const *envp, PWCHAR &envblock, int &envc,
>   during execve. */
>        if (!saw_PATH)
>   {
> -  new_tl += cygheap->installation_dir.Length / sizeof (WCHAR) + 5;
> +  new_tl += cygheap->installation_dir.Length / sizeof (WCHAR) + 5 + 1;
>    if (new_tl > tl)
>      tl = raise_envblock (new_tl, envblock, s);
>    s = wcpcpy (wcpcpy (s, L"PATH="),
>

Don't forget also to allow for the single null char '\0' final entry to
terminate the env var block, and ensure that is cleared following any addition:

        SYSTEMDRIVE=C:\0
        SYSTEMROOT=C:\WINDOWS\0
        WINDIR=C:\WINDOWS\0
        PATH=C:\cygwin64\bin\0
        \0

Nothing like running off the end thru memory looking for another ...=...\0! ;^>

--
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.