fix shifts possibly larger than type in major() macro
authorRich Felker <dalias@aerifal.cx>
Sat, 29 Jun 2013 17:57:13 +0000 (13:57 -0400)
committerRich Felker <dalias@aerifal.cx>
Sat, 29 Jun 2013 17:57:13 +0000 (13:57 -0400)
in theory this should not be an issue, since major() should only be
applied to type dev_t, which is 64-bit. however, it appears some
applications are not using dev_t but a smaller integer type (which
works on Linux because the kernel's dev_t is really only 32-bit). to
avoid the undefined behavior, do it as two shifts.

include/sys/sysmacros.h

index d6c6aef..07a3ef1 100644 (file)
@@ -2,7 +2,7 @@
 #define _SYS_SYSMACROS_H
 
 #define major(x) \
 #define _SYS_SYSMACROS_H
 
 #define major(x) \
-       ((unsigned)( (((x)>>32) & 0xfffff000) | (((x)>>8) & 0x00000fff) ))
+       ((unsigned)( (((x)>>31>>1) & 0xfffff000) | (((x)>>8) & 0x00000fff) ))
 #define minor(x) \
        ((unsigned)( (((x)>>12) & 0xffffff00) | ((x) & 0x000000ff) ))
 
 #define minor(x) \
        ((unsigned)( (((x)>>12) & 0xffffff00) | ((x) & 0x000000ff) ))