#2098 MinGW startup code pollutes the namespace by calling opendir and readdir

WSL
pending
None
Bug
fixed
IINR_-_Include_In_Next_Release
True
2014-12-24
2013-10-09
Eli Zaretskii
No

MinGW runtime 4.x calls the dirent functions, opendir/readdir/closedir, in its startup code. This pollutes the namespace of, and breaks, application programs that have incompatible or entirely unrelated functions by that name, even if the application didn't include the dirent.h header. For example, a program that defines its own readdir will almost surely break the globbing of command-line arguments, and could even crash.

Please make the startup code call __opendir, __readdir, etc. instead, as symbols that begin with two underscores are "reserved", and cannot legitimately appear in an application.

Related

Issues: #2106
Issues: #2111
Issues: #2112
Issues: #2113

Discussion

<< < 1 2 (Page 2 of 2)
  • Keith Marshall
    Keith Marshall
    2013-10-18

    I'll not finalize the patch, until we reach consensus on this; once we have that, it'll be around 30 mins work, (when I have a suitable time slot), to complete and write up the ChangeLog.

    Meanwhile, with a near-final build, glob.o (which is the API causing the issue) now exhibits the following symbol references:

    $ mingw32-nm src/libcrt/misc/glob.o
    00000000 b .bss
    00000000 d .data
    00000000 r .rdata
    00000000 N .stab
    00000000 N .stabstr
    00000000 t .text
             U ___mingw_closedir
    00000a80 T ___mingw_glob
    00000a60 T ___mingw_globfree
             U ___mingw_opendir
             U ___mingw_readdir
             U __alloca
             U __errno
             U _dirname
             U _free
    00000000 d _glob_escape_char
    00000050 t _glob_in_set
    000002e0 t _glob_initialise
    00000440 t _glob_match
    000009c0 t _glob_registry
    00000000 t _glob_set_adjusted
    000003c0 t _glob_store_collated_entries
    00000330 t _glob_store_entry
    00000100 t _glob_strcmp
             U _malloc
             U _memcpy
             U _realloc
             U _strcoll
             U _strdup
             U _stricoll
             U _strlen
             U _tolower
    00000004 d glob_magic.0
    

    Note that the opendir, readdir, and closedir references have now moved into the __mingw_ "namespace", but there is a residual reference to dirname which has not. I'm thinking that perhaps it should, (along with basename, for symmetry, and maybe also getopt and friends). Should that be progressed under this ticket? Or, would a new one be better?

     
    • Earnie Boyd
      Earnie Boyd
      2013-10-18

      I agree that dirname should be included with the __mingw_ namespace. Maybe eventually we can move all of the mingwex functions to that namespace. It provides for more flexibility for the user.

       
  • Keith Marshall
    Keith Marshall
    2013-10-19

    I've now completed the patch, as attached, to move all dirent associated functions to the __mingw_ namespace; this assumes that dirent-2106.patch, as attached to [#2106], has been applied first.

    Also attached is an adaptation of the test case from [#2106]:

    #include <stdio.h>
    #include <dirent.h>
    
    int main(void) {
        typedef DIR *(*opendir_t)(const char *);
        typedef struct dirent *(*readdir_t)(DIR *);
        typedef int (*closedir_t)(DIR *);
    
        opendir_t my_opendir = opendir;
        readdir_t my_readdir = readdir;
        closedir_t my_closedir = closedir;
    
        struct dirent *entry;
        DIR *dp;
        int max = 7;
    
        fprintf(stderr,"time_t = %db\n", sizeof(time_t) * 8);
    
        dp = my_opendir("C:\\MinGW\\bin");
        if (dp == NULL) {
            perror("opendir");
            return -1;
        }
        while (--max && (entry = my_readdir(dp))) {
    
            fprintf( stderr,"%s: %I64u B\n", entry->d_name, entry->d_size );
        }
    
        my_closedir(dp);
        return 0;
    }
    

    Running this demonstrates correct operation, with indirect invocation via the trampoline entries generated from jmpstub.sx:

    $ gcc -o foo.exe issue-2098-test.c
    $ ./foo.exe
    time_t = 64b
    .: 0 B
    ..: 0 B
    addr2line.exe: 1732622 B
    ar.exe: 1758222 B
    as.exe: 2226190 B
    c++.exe: 11776 B
    

    I shall address the similar relocation of the dirname() and basename() functions, under ticket [#2112].

    While working on this, I've also noticed that attribution info, removed from the dirent.c and dirent.h files in the transition from mingwrt-3.x to wsl-4.x, has not been reinstated as previously requested. I've opened [#2111] to address this.

     

    Related

    Issues: #2106
    Issues: #2111
    Issues: #2112

  • Keith Marshall
    Keith Marshall
    2014-12-24

    This is now fixed, along with and as described on ticket [#2106]; the updated implementation will be included in the next release of mingwrt-3.x

     

    Related

    Issues: #2106

  • Keith Marshall
    Keith Marshall
    2014-12-24

    • status: assigned --> pending
    • Resolution: none --> fixed
    • Category: Unknown --> IINR_-_Include_In_Next_Release
     
<< < 1 2 (Page 2 of 2)