On Tuesday 13 January 2026 13:52:55 LIU Hao wrote:
> 在 2025-12-29 02:00, Pali Rohár 写道:
> > If the GetEnvironmentVariable was called with NULL buffer then the return
> > value is buffer size including its terminating null character.
> >
> > This differs from the case when GetEnvironmentVariable succeeds, then it
> > returns size without the terminating null character.
> >
> > CRT getenv_s function always returns size with terminating null character.
> > ---
> > mingw-w64-libraries/winstorecompat/src/getenv_s.c | 7 +++++++
> > 1 file changed, 7 insertions(+)
> >
> > diff --git a/mingw-w64-libraries/winstorecompat/src/getenv_s.c
> > b/mingw-w64-libraries/winstorecompat/src/getenv_s.c
> > index ed7c52523279..5988082fab32 100644
> > --- a/mingw-w64-libraries/winstorecompat/src/getenv_s.c
> > +++ b/mingw-w64-libraries/winstorecompat/src/getenv_s.c
> > @@ -63,6 +63,13 @@ errno_t __cdecl getenv_s(size_t *pReturnValue, char
> > *dstBuf, rsize_t dstSize, co
> > if (dstBuf)
> > dstBuf[0] = '\0';
> > return ERANGE;
> > + } else if (dstSize == 0) {
> > + /* If the GetEnvironmentVariable was called with NULL buffer then
> > + * the return value is same as above case: buffer size including
> > its
> > + * terminating null character.
> > + */
> > + *pReturnValue = ret;
> > + return 0;
>
> This comment says 'NULL buffer', but `dstBuf` is a parameter so it's unclear
> that whether it's null or not. `GetEnvironmentVariableA()` accepts a length
> of zero with either a null or non-null pointer, which is always classified
> as a buffer that 'is not large enough to hold the data'.
>
> So, other than the comment, the change looks good to me. I will push this
> one after the CI passes, with 'NULL buffer' replaced by 'zero-length
> buffer'.
Ok, that is fine.
I must admit that GetEnvironmentVariable() API with +/-1 in buffer sizes is
insane.
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public