Functions __getmainargs() and __wgetmainargs() in system msvcrt.dll library
since Windows XP and in Visual C++ libraries since msvcr70.dll return
negative value on error and expect that caller handle it and terminate
process. Visual C++ 2002+ startup code calls _amsg_exit(_RT_SPACEARG) on
failure. Do same in mingw-w64 startup code.

Note that mingw-w64 contains compatibility layer around __getmainargs() and
__wgetmainargs() functions for msvcrt.dll and older VC++ DLL libraries in
mingw-w64 import libraries to ensure that these functions returns 0 on
success. This allows to have uniform mingw-w64 crtexe.c startup code, which
can always expects that __getmainargs() and __wgetmainargs() returns the
correct value.

Variable argret is used only in the pre_cpp_init() function, so declare it
on the function stack instead of having it static for the whole file.

With this change, compiled applications linked with msvcrt.dll or new
library correctly reports runtime failure on allocations errors from
__getmainargs() and __wgetmainargs() calls.
---
 mingw-w64-crt/crt/crtexe.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index 319cace6f50a..ec7fa74dc37b 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -58,7 +58,6 @@ extern void __main(void);
 static _TCHAR **argv;
 static _TCHAR **envp;
 
-static int argret;
 static int mainret=0;
 static int managedapp;
 static int has_cctor = 0;
@@ -125,6 +124,7 @@ static void __cdecl
 pre_cpp_init (void)
 {
   _startupinfo startinfo;
+  int argret;
 
   startinfo.newmode = _newmode;
 
@@ -133,6 +133,8 @@ pre_cpp_init (void)
 #else
   argret = __getmainargs(&argc,&argv,&envp,_dowildcard,&startinfo);
 #endif
+  if (argret < 0)
+    _amsg_exit(8); /* _RT_SPACEARG */
 }
 
 static int __tmainCRTStartup (void);
-- 
2.20.1



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

Reply via email to