>Synopsis:      /usr/include/sys/param.h breaks code
>Category:      system
>Environment:
        System      : OpenBSD 5.0
        Details     : OpenBSD 5.0-stable (GENERIC.MP) #0: Thu Feb 16 01:38:28 
EST 2012
                         
root@aemilia.chuck:/usr/src/sys/arch/i386/compile/GENERIC.MP

        Architecture: OpenBSD.i386
        Machine     : i386
>Description:
        /usr/include/sys/param.h contains a "convenience macro" named
        nitems, which causes user code using nitems in an innocent
        context to fail mysteriously.  The bug was spotted during
        compilation of c++ code that uses the FLTK port.

        The macro is named "nitems" and is found in /usr/include/sys/param.h
        at line 196: 

        191 /* Macros for calculating the offset of a field */
        192 #if !defined(offsetof) && defined(_KERNEL)
        193 #define offsetof(s, e) ((size_t)&((s *)0)->e)
        194 #endif
        195
        196 #define nitems(_a)      (sizeof((_a)) / sizeof((_a)[0]))
        197

>How-To-Repeat:

        it was found in a c++ method,  I imagine you can write
        sample code to exercise the bug.

        Any use of nitems(...) that one does not want expanded
        as per the macro will demonstrate the bug.

        snippet that ought to bug:

        #include <sys/param.h>

        extern int nitems(void);

        /* expands to  extern int (sizeof((void))/sizeof((void)[0]); */

        .... later ....

                foo = nitems();

        /* expands to  foo = (sizeof(())/sizeof(()[0]); */

>Fix:
        surround the nitems macro with #if define(_KERNEL) and
        let any non-kernel code that has come to depend on it
        choke.

No dmesg needed.

Sent this to gnats... no response.  Gnats check-your-PR webpage doesn't
work. 

Sent to bugs@, not subscribed, please CC if there is some argument.

Dave 

Reply via email to