Menu

#2307 Macro-related error in ddk\ntddstor.h

WSL
closed
None
Bug
fixed
IINR_-_Include_In_Next_Release
False
2016-09-05
2016-07-20
Anton
No

Environment:

Host OS: Windows X Pro SP 3

GCC:
Using built-in specs.
COLLECT_GCC=C:\MinGW\bin\gcc.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.9.3/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-4.9.3/configure --build=x86_64-pc-linux-gnu --host=mingw32 --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --enable-libstdcxx-debug --with-tune=generic --enable-nls
Thread model: win32
gcc version 4.9.3 (GCC) 

Binutils linker: GNU ld (GNU Binutils) 2.25.1

MinGW: 3022000L

Build Environment:
MINGW32_NT-5.1 ANT-519666801B8 1.0.19(0.48/3/2) 2016-07-13 17:45 i686 Msys

The macro __IOCTL_STORAGE_ defined in \ddk\ntddstor.h is not expanded properly.

To reproduce the issude, try to compile the following program with gcc:

#define _XOPEN_SOURCE 700

#include <stdio.h>
#include <winioctl.h>

int main()
{ printf( "IOCTL_STORAGE_EJECT_MEDIA = %1$08d (0x%1$08X)\n",
      IOCTL_STORAGE_EJECT_MEDIA
    );
  return 0;
}

Compilation fails with the following error:

test.c: In function 'main':
test.c:8:7: error: macro "__IOCTL_STORAGE_" requires 3 arguments, but only 2 given
       IOCTL_STORAGE_EJECT_MEDIA
       ^
In file included from c:\mingw\include\ddk\ntdddisk.h:49:0,
                 from c:\mingw\include\winioctl.h:55,
                 from test.c:4:
test.c:8:7: error: '__IOCTL_STORAGE_' undeclared (first use in this function)
       IOCTL_STORAGE_EJECT_MEDIA
       ^
test.c:8:7: note: each undeclared identifier is reported only once for each function it appears in

Related

Issues: #2309

Discussion

  • Keith Marshall

    Keith Marshall - 2016-07-21
    • status: unread --> assigned
    • assigned_to: Keith Marshall
     
  • Keith Marshall

    Keith Marshall - 2016-07-21

    Thanks for the report; this is specific to w32api-3.18.

    The issue is caused by the C preprocessor's lazy expansion of macro argument's. It is rather more pervasive than just the IOCTL_STORAGE_EJECT_MEDIA example cited, but, continuing with that as an example, we see the expansion of that macro as:

    __IOCTL_STORAGE_(0x0202,__FILE_RD_BUFFERED)
    

    with __IOCTL_STORAGE_ defined as:

    #define __IOCTL_STORAGE_(FN,M,A)  CTL_CODE(IOCTL_STORAGE_BASE,(FN),(M),(A))
    

    and __FILE_RD_BUFFERED defined (in <ddk/winddk.h>) as:

    #define __FILE_RD_BUFFERED  METHOD_BUFFERED, FILE_READ_ACCESS
    

    Now, if the C preprocessor expanded macros logically, (i.e. inside parentheses first), we would see IOCTL_STORAGE_EJECT_MEDIA expanding initially to:

    __IOCTL_STORAGE_(0x0202,METHOD_BUFFERED,FILE_READ_ACCESS)
    

    which would be correct. Unfortunately, it doesn't happen that way: expansion occurs from outside to in, and the incorrect number of macro arguments is flagged, without realization that expansion of the inner macro actually would provide the additional argument which is expected.

    This afflicts all macros currently defined in terms of __FILE_RD_BUFFERED, (and similarly, __FILE_RW_BUFFERED and __FILE_AM_BUFFERED). A possible resolution will be to redefine each of these three macros, to cause, (in the example case), __IOCTL_STORAGE_ to be expanded within the expansion of __FILE_RD_BUFFERED, (and similarly, for all other usage, and for each of the other two offending macros), e.g.:

    #define __FILE_RD_BUFFERED(F,N)  F(N,METHOD_BUFFERED,FILE_READ_ACCESS)
    .
    .
    .
    #define IOCTL_STORAGE_EJECT_MEDIA  __FILE_RD_BUFFERED(__IOCTL_STORAGE_,0x0202)
    

    I'll work toward such a resolution, and release w32api-3.18.1 ASAP.

     
  • Keith Marshall

    Keith Marshall - 2016-09-05
    • status: assigned --> closed
    • Resolution: none --> fixed
    • Category: Unknown --> IINR_-_Include_In_Next_Release
     
  • Keith Marshall

    Keith Marshall - 2016-09-05

    This is fixed in w32api-3.18.1. Although I've flagged it as "include in next release", this is already available in FRS, and may be installed using mingw-get.

     
MongoDB Logo MongoDB