Skip to content

Commit

Permalink
win32.c: don't call wcscpy() with in == out
Browse files Browse the repository at this point in the history
Since both the input and output parameters are restrict qualified,
this would be invalid, and it is possibe for PerlDir_mapW() to return
its parameter.  This warned on gcc:

win32.c: In function 'win32_link':
win32.c:3712:40: warning: passing argument 1 to 'restrict'-qualified parameter aliases with argument 2 [-Wrestrict]
 3712 |         ((aTHXa(PERL_GET_THX)), wcscpy(wOldName, PerlDir_mapW(wOldName)),
      |                                        ^~~~~~~~
  • Loading branch information
tonycoz committed Apr 14, 2024
1 parent 224c53d commit a33729f
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion win32/win32.c
Expand Up @@ -3700,6 +3700,17 @@ win32_pclose(PerlIO *pf)
#endif /* USE_RTL_POPEN */
}

/* wcscpy() arguments are restrict qualified, but if they're equal
we don't need to do the copy
*/
static inline WCHAR *
cond_wcsncpy(WCHAR *out, const WCHAR *in, size_t n) {
assert(wcslen(in) < n);
if (out != in)
wcsncpy(out, in, n);
return out;
}

DllExport int
win32_link(const char *oldname, const char *newname)
{
Expand All @@ -3709,7 +3720,8 @@ win32_link(const char *oldname, const char *newname)

if (MultiByteToWideChar(CP_ACP, 0, oldname, -1, wOldName, MAX_PATH+1) &&
MultiByteToWideChar(CP_ACP, 0, newname, -1, wNewName, MAX_PATH+1) &&
((aTHXa(PERL_GET_THX)), wcscpy(wOldName, PerlDir_mapW(wOldName)),
((aTHXa(PERL_GET_THX)),
cond_wcsncpy(wOldName, PerlDir_mapW(wOldName), C_ARRAY_LENGTH(wOldName)),
CreateHardLinkW(PerlDir_mapW(wNewName), wOldName, NULL)))
{
return 0;
Expand Down

0 comments on commit a33729f

Please sign in to comment.