When a subset of ssp functions were implemented in 7478c20848f60e6238547db63d1a5bc450d468cc, only functions that were referenced with _FORTIFY_SOURCE were added. This set of functions accidentally missed __gets_chk, which we do reference if fortification is enabled.
Thus, this fixes an accidental omission. While this function does implicitly reference stderr and the fgets function, the fgets function symbol is provided by all CRT alternatives. stderr expands to a call to __acrt_iob_func, and that is provided by all CRT alternatives, either by the DLL itself or as statically linked glue code. Therefore, this implementation should be safe to use with both msvcr*.dll and UCRT. Signed-off-by: Martin Storsjö <[email protected]> --- mingw-w64-crt/Makefile.am | 3 ++- mingw-w64-crt/ssp/gets_chk.c | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 mingw-w64-crt/ssp/gets_chk.c diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index 64b14d068..4547cee62 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -604,7 +604,8 @@ src_libmingwex=\ misc/wmemchr.c misc/wmemcmp.c misc/wmemcpy.c misc/wmemmove.c misc/wmempcpy.c \ misc/wmemset.c misc/ftw.c misc/ftw64.c misc/mingw-access.c \ \ - ssp/chk_fail.c ssp/memcpy_chk.c ssp/memmove_chk.c ssp/mempcpy_chk.c \ + ssp/chk_fail.c ssp/gets_chk.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/stpcpy_chk.c ssp/strcpy_chk.c ssp/strncat_chk.c ssp/strncpy_chk.c \ \ diff --git a/mingw-w64-crt/ssp/gets_chk.c b/mingw-w64-crt/ssp/gets_chk.c new file mode 100644 index 000000000..9c85083f9 --- /dev/null +++ b/mingw-w64-crt/ssp/gets_chk.c @@ -0,0 +1,42 @@ +/** + * 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> +#include <stdio.h> +#include <stdlib.h> + +void __cdecl __chk_fail(void) __attribute__((__noreturn__)); + +char *__cdecl __gets_chk(char *dst, size_t bufsize); + +char *__cdecl __gets_chk(char *dst, size_t bufsize) +{ + char *buf; + size_t n; + if (bufsize >= (size_t) INT_MAX) + return gets(dst); + + buf = malloc(bufsize + 1); + if (!buf) + return gets(dst); + + if (!fgets(buf, (int)(bufsize + 1), stdin)) { + free(buf); + return NULL; + } + + n = strlen(buf); + if (n > 0 && buf[n - 1] == '\n') + n--; + + if (n >= bufsize) + __chk_fail(); + + memcpy(dst, buf, n); + dst[n] = '\0'; + free(buf); + return dst; +} -- 2.34.1 _______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
