Skip to content

Commit

Permalink
Type conversion for align type promotion in P2ALIGN
Browse files Browse the repository at this point in the history
In P2ALIGN, the result would be incorrect when align is unsigned
integer and x is larger than max value of the type of align.
In that case, -(align) would be a positive integer, which means
high bits would be zero and finally stay zero after '&' when
align is converted to a larger integer type.

Signed-off-by: Qiuhao Chen <[email protected]>
  • Loading branch information
chenqiuhao1997 committed Feb 29, 2024
1 parent 8f2f6cd commit d8312e4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion include/os/freebsd/spl/sys/ccompile.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ typedef int enum_t;
#define readdir64 readdir
#define dirent64 dirent
#endif
#define P2ALIGN(x, align) ((x) & -(align))
#define P2ALIGN(x, align) \
((x) & -((sizeof (x) > sizeof (align))?(typeof(x))(align):(align)))
#define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1)
#define P2ROUNDUP(x, align) ((((x) - 1) | ((align) - 1)) + 1)
#define P2PHASE(x, align) ((x) & ((align) - 1))
Expand Down
3 changes: 2 additions & 1 deletion include/os/freebsd/spl/sys/sysmacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ extern unsigned char bcd_to_byte[256];
* eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
* eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
*/
#define P2ALIGN(x, align) ((x) & -(align))
#define P2ALIGN(x, align) \
((x) & -((sizeof (x) > sizeof (align))?(typeof(x))(align):(align)))

/*
* return x % (mod) align
Expand Down
3 changes: 2 additions & 1 deletion include/os/linux/spl/sys/sysmacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ makedev(unsigned int major, unsigned int minor)
/*
* Compatibility macros/typedefs needed for Solaris -> Linux port
*/
#define P2ALIGN(x, align) ((x) & -(align))
#define P2ALIGN(x, align) \
((x) & -((sizeof (x) > sizeof (align))?(typeof(x))(align):(align)))
#define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1)
#define P2ROUNDUP(x, align) ((((x) - 1) | ((align) - 1)) + 1)
#define P2PHASE(x, align) ((x) & ((align) - 1))
Expand Down
3 changes: 2 additions & 1 deletion lib/libspl/include/os/linux/sys/sysmacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
/*
* Compatibility macros/typedefs needed for Solaris -> Linux port
*/
#define P2ALIGN(x, align) ((x) & -(align))
#define P2ALIGN(x, align) \
((x) & -((sizeof (x) > sizeof (align))?(typeof(x))(align):(align)))
#define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1)
#define P2ROUNDUP(x, align) ((((x) - 1) | ((align) - 1)) + 1)
#define P2BOUNDARY(off, len, align) \
Expand Down

0 comments on commit d8312e4

Please sign in to comment.