Skip to content

Avoid brittle wcscpy_s and wcscat_s defines on mingw - #198

Open
mstorsjo wants to merge 1 commit into
intel:mainfrom
mstorsjo:avoid-wcs-defines
Open

Avoid brittle wcscpy_s and wcscat_s defines on mingw#198
mstorsjo wants to merge 1 commit into
intel:mainfrom
mstorsjo:avoid-wcs-defines

Conversation

@mstorsjo

@mstorsjo mstorsjo commented Sep 1, 2026

Copy link
Copy Markdown

These wcscpy_s and wcscat_s fallback defines seem to be inteded for older versions of MSVC. When building in mingw mode, _MSC_VER isn't defined at all.

Previously this led to these defines being used, which was harmless at time time.

After mingw-w64/mingw-w64@9dff64a in mingw-w64, these fallback defines break using mingw-w64 headers; the expansion of wcscpy_s into two separate statements breaks use of wcscpy_s function in an expression context, leading to errors like this:

In file included from /home/martin/code/libvpl/libvpl/src/windows/mfx_dispatcher.cpp:14:
In file included from /home/martin/clang-nightly/x86_64-w64-mingw32/include/windows.h:114:
/home/martin/clang-nightly/x86_64-w64-mingw32/include/stralign.h:208:67: error: expected ')'
  208 |     if(WSTR_ALIGNED(Source) && WSTR_ALIGNED(Destination)) return (wcscpy_s((PWSTR)Destination,DestinationSize,(PCWSTR)Source)==0 ? Destination : NULL);
      |                                                                   ^
/home/martin/code/libvpl/libvpl/src/windows/mfx_dispatcher_defs.h:22:24: note: expanded from macro 'wcscpy_s'
   22 |         (void)(to_size);                \
      |                        ^
/home/martin/clang-nightly/x86_64-w64-mingw32/include/stralign.h:208:66: note: to match this '('
  208 |     if(WSTR_ALIGNED(Source) && WSTR_ALIGNED(Destination)) return (wcscpy_s((PWSTR)Destination,DestinationSize,(PCWSTR)Source)==0 ? Destination : NULL);
      |                                                                  ^
/home/martin/clang-nightly/x86_64-w64-mingw32/include/stralign.h:208:67: error: cannot initialize return object of type 'PUWSTR' (aka 'wchar_t *') with an rvalue of type 'void'
  208 |     if(WSTR_ALIGNED(Source) && WSTR_ALIGNED(Destination)) return (wcscpy_s((PWSTR)Destination,DestinationSize,(PCWSTR)Source)==0 ? Destination : NULL);
      |                                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/martin/code/libvpl/libvpl/src/windows/mfx_dispatcher_defs.h:22:9: note: expanded from macro 'wcscpy_s'
   22 |         (void)(to_size);                \
      |         ^~~~~~~~~~~~~~~
In file included from /home/martin/code/libvpl/libvpl/src/windows/mfx_dispatcher.cpp:14:
In file included from /home/martin/clang-nightly/x86_64-w64-mingw32/include/windows.h:114:
/home/martin/clang-nightly/x86_64-w64-mingw32/include/stralign.h:208:150: error: extraneous ')' before ';'
  208 |     if(WSTR_ALIGNED(Source) && WSTR_ALIGNED(Destination)) return (wcscpy_s((PWSTR)Destination,DestinationSize,(PCWSTR)Source)==0 ? Destination : NULL);
      |                                                                                                                                                      ^
3 errors generated.

Avoid this issue by simply limiting the fallback wcscpy_s and wcscat_s defines to older MSVC versions, which seems to be the original intent, avoiding defining them on mingw targets, that don't need them.

These wcscpy_s and wcscat_s fallback defines seem to be inteded for
older versions of MSVC. When building in mingw mode, _MSC_VER isn't
defined at all.

Previously this led to these defines being used, which was harmless
at time time.

After mingw-w64/mingw-w64@9dff64a
in mingw-w64, these fallback defines break using mingw-w64 headers;
the expansion of wcscpy_s into two separate statements breaks use of
wcscpy_s function in an expression context, leading to errors
like this:

    In file included from /home/martin/code/libvpl/libvpl/src/windows/mfx_dispatcher.cpp:14:
    In file included from /home/martin/clang-nightly/x86_64-w64-mingw32/include/windows.h:114:
    /home/martin/clang-nightly/x86_64-w64-mingw32/include/stralign.h:208:67: error: expected ')'
      208 |     if(WSTR_ALIGNED(Source) && WSTR_ALIGNED(Destination)) return (wcscpy_s((PWSTR)Destination,DestinationSize,(PCWSTR)Source)==0 ? Destination : NULL);
          |                                                                   ^
    /home/martin/code/libvpl/libvpl/src/windows/mfx_dispatcher_defs.h:22:24: note: expanded from macro 'wcscpy_s'
       22 |         (void)(to_size);                \
          |                        ^
    /home/martin/clang-nightly/x86_64-w64-mingw32/include/stralign.h:208:66: note: to match this '('
      208 |     if(WSTR_ALIGNED(Source) && WSTR_ALIGNED(Destination)) return (wcscpy_s((PWSTR)Destination,DestinationSize,(PCWSTR)Source)==0 ? Destination : NULL);
          |                                                                  ^
    /home/martin/clang-nightly/x86_64-w64-mingw32/include/stralign.h:208:67: error: cannot initialize return object of type 'PUWSTR' (aka 'wchar_t *') with an rvalue of type 'void'
      208 |     if(WSTR_ALIGNED(Source) && WSTR_ALIGNED(Destination)) return (wcscpy_s((PWSTR)Destination,DestinationSize,(PCWSTR)Source)==0 ? Destination : NULL);
          |                                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /home/martin/code/libvpl/libvpl/src/windows/mfx_dispatcher_defs.h:22:9: note: expanded from macro 'wcscpy_s'
       22 |         (void)(to_size);                \
          |         ^~~~~~~~~~~~~~~
    In file included from /home/martin/code/libvpl/libvpl/src/windows/mfx_dispatcher.cpp:14:
    In file included from /home/martin/clang-nightly/x86_64-w64-mingw32/include/windows.h:114:
    /home/martin/clang-nightly/x86_64-w64-mingw32/include/stralign.h:208:150: error: extraneous ')' before ';'
      208 |     if(WSTR_ALIGNED(Source) && WSTR_ALIGNED(Destination)) return (wcscpy_s((PWSTR)Destination,DestinationSize,(PCWSTR)Source)==0 ? Destination : NULL);
          |                                                                                                                                                      ^
    3 errors generated.

Avoid this issue by simply limiting the fallback wcscpy_s and
wcscat_s defines to older MSVC versions, which seems to be the
original intent, avoiding defining them on mingw targets, that don't
need them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant