#40696: MSVCRT.DLL's _pgmptr variable is uninitialized by MinGW start-up code Open Date: 2020-08-31 23:32 Last Update: 2020-09-01 20:31 URL for this Ticket: https://osdn.net//projects/mingw/ticket/40696 RSS feed for this Ticket: https://osdn.net/ticket/ticket_rss.php?group_id=3917&tid=40696 --------------------------------------------------------------------- Last Changes/Comment on this Ticket: 2020-09-01 20:31 Updated by: keith Comment: The following (trivial) program illustrates the issue: 1. #include <stdio.h> 2. #include <stdlib.h> 3. 4. #ifdef _CRT_GLOB 5. int _CRT_glob = _CRT_GLOB; 6. #endif 7. 8. int main() 9. { printf( "_pgmptr = '%s'\n", _pgmptr ); 10. return 0; 11. } If I compile this, with default settings for _CRT_glob, link with current mingwrt, and run it (under MSYS) on a Win7, or WinXP VM, I see: $ mingw32-gcc t-pgmptr.c -o pgmptr.exe $ ./pgmptr.exe _pgmptr = '(null)' OTOH, if I compile it with -D_CRT_GLOB=1, or with -D_CRT_GLOB=0, either of which causes the mingwrt start-up code to call Microsoft's __getmainargs(), I see (as expected): $ mingw32-gcc -D_CRT_GLOB=1 t-pgmptr.c -o pgmptr.exe $ ./pgmptr.exe _pgmptr = 'e:\sandbox\pgmptr.exe' Curiously, I didn't notice the anomalous default behaviour previously, (testing on GNU/Linux, under Wine), because Wine's implementation of MSVCRT.DLL appears to initialize _pgmptr without __getmainargs() having been called. --------------------------------------------------------------------- Ticket Status: Reporter: keith Owner: keith Type: Issues Status: Open [Owner assigned] Priority: 5 - Medium MileStone: (None) Component: WSL Severity: 5 - Medium Resolution: None --------------------------------------------------------------------- Ticket details: According to Microsoft's GetModulePathNameA() function documentation: The global variable _pgmptr is automatically initialized to the full path of the executable file, and can be used to retrieve the full path name of an executable file. It appears that this global variable initialization occurs, only if the run-time start-up code calls Microsoft's __getmainargs() function, but that exhibits defective globbing of wild-card patterns, and MinGW has not used it, as a default start-up hook, since the release of mingwrt-3.21, in December 2014. Consequently, references to _pgmptr, in code linked against mingwrt-3.21 (and later), are likely to dereference a NULL pointer. -- Ticket information of MinGW - Minimalist GNU for Windows project MinGW - Minimalist GNU for Windows Project is hosted on OSDN Project URL: https://osdn.net/projects/mingw/ OSDN: https://osdn.net URL for this Ticket: https://osdn.net/projects/mingw/ticket/40696 RSS feed for this Ticket: https://osdn.net/ticket/ticket_rss.php?group_id=3917&tid=40696