Initially, it may seem like this function might not be needed in any
form, since mingw-w64 lacks the main stpcpy function.

However, third party projects may contain their own implementation of
the stpcpy function. When GCC sees a declaration of the stpcpy function,
it assumes that it is legal to do optimizations on strcpy+strlen into
stpcpy.

When strcpy is wrapped with fortification wrappers, so that strcpy
ends up calling __builtin___strcpy_chk (which then calls __strcpy_chk),
GCC can also transform this into __builtin___stpcpy_chk, which can
generate a call to __stpcpy_chk.

GCC's libssp does provide an implementation of __stpcpy_chk, even if
the platform itself lacks stpcpy.

Therefore, mingw-w64-crt's implementation of the ssp routines also
does need an implementation of __stpcpy_chk, even if it is hard
to practically produce calls to it.

This should fix one issue discussed at
https://github.com/msys2/MINGW-packages/issues/5803#issuecomment-1276812143.

Signed-off-by: Martin Storsjö <mar...@martin.st>
---
 mingw-w64-crt/Makefile.am      |  2 +-
 mingw-w64-crt/ssp/stpcpy_chk.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 mingw-w64-crt/ssp/stpcpy_chk.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 053eab3cf..1dff68409 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -566,7 +566,7 @@ src_libmingwex=\
   \
   ssp/chk_fail.c         ssp/memcpy_chk.c           ssp/memmove_chk.c       
ssp/mempcpy_chk.c \
   ssp/memset_chk.c       ssp/stack_chk_fail.c       ssp/stack_chk_guard.c   
ssp/strcat_chk.c \
-  ssp/strcpy_chk.c       ssp/strncat_chk.c          ssp/strncpy_chk.c \
+  ssp/stpcpy_chk.c       ssp/strcpy_chk.c           ssp/strncat_chk.c       
ssp/strncpy_chk.c \
   \
   stdio/mingw_pformat.h    \
   stdio/scanf2-argcount-char.c stdio/scanf2-argcount-wchar.c \
diff --git a/mingw-w64-crt/ssp/stpcpy_chk.c b/mingw-w64-crt/ssp/stpcpy_chk.c
new file mode 100644
index 000000000..aaa387680
--- /dev/null
+++ b/mingw-w64-crt/ssp/stpcpy_chk.c
@@ -0,0 +1,19 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#include <string.h>
+
+void __cdecl __chk_fail(void) __attribute__((__noreturn__));
+
+char *__cdecl __stpcpy_chk(char *dst, const char *src, size_t bufsize);
+
+char *__cdecl __stpcpy_chk(char *dst, const char *src, size_t bufsize)
+{
+  size_t n = strlen(src);
+  if (n >= bufsize)
+    __chk_fail();
+  return memcpy(dst, src, n + 1) + n;
+}
-- 
2.25.1



_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to