Lasse Collin <[email protected]> wrote:

> On 2026-08-17 Kirill Makurin wrote:
>> Documentation for `CreateThread` from Platform SDK for Windows Server
>> 2003[1] says that the last parameter must be non-NULL on Windows
>> 95/98/Me.
>>
>> I'm not sure if it is worth to add this attribute just for the sake
>> of ancient Windows versions.
>
> It's not about what is worth it, it's about correctness. When NULL might
> be a valid argument, the nonnull attribute *must not* be added because
> adding it could create bugs. The attribute has two effects:
>
> (1) Compiler may optimize with the assumption that a NULL argument
>     results in undefined behavior. For example, gcc -O2 can optimize
>     bar() to always return 0. Try at <https://godbolt.org/z/axxva6dqf>.
>
>         #include <stddef.h>
>
>         void foo(char *buf) __attribute__((nonnull(1)));
>
>         int bar(char *buf)
>         {
>             int r = 0;
>             if (buf == NULL)
>                 r = -1;
>             foo(buf);
>             return r;
>         }
>
> (2) Enable diagnostics for -Wnonnull and -fsanitize=undefined.
>
> With the nonnull attribute, one cannot get (2) without (1). Clang has
> _Nonnull which does only (2), but GCC doesn't support it.

I forgot that these attributes also affect compiler optimizations, in addition 
to enabling additional diagnostics. You're making a good argument that adding 
this attribute may result in undefined behavior.

>> In case of CRT functions
>> `_beginthread[ex]`, I think we are better to provide a wrapper which
>> could use a stack variable if this argument is NULL.
>
> In my opinion, if a program needs to run on very old Windows, then it's
> better to fix the program instead of adding this kind of wrapper to
> mingw-w64.

Yes, I agree. Unfortunately, this kind of information is usually lacking from 
recent Microsoft docs.

According to information in mingw-w64-doc/howto-build/crt-libraries.txt, the 
latest CRT which can run Win9x systems is msvcr80.dll. In SEH-related changes a 
few months ago, Pali added wrappers for `_beginthread[ex]` for 32-bit 
msvcr90.dll and older. With this in mind, all we need to do is to extend those 
wrappers to handle case when the last argument is NULL.

- Kirill Makurin

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to