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 <chenqiuhao1997@gmail.com>
  • Loading branch information
chenqiuhao1997 committed Feb 28, 2024
1 parent 8f2f6cd commit eca2d33
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/os/freebsd/spl/sys/ccompile.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ 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)))

Check failure on line 141 in include/os/freebsd/spl/sys/ccompile.h

View workflow job for this annotation

GitHub Actions / checkstyle

line > 80 characters

Check failure on line 141 in include/os/freebsd/spl/sys/ccompile.h

View workflow job for this annotation

GitHub Actions / checkstyle

missing space around relational operator

Check failure on line 141 in include/os/freebsd/spl/sys/ccompile.h

View workflow job for this annotation

GitHub Actions / checkstyle

missing space between keyword and paren
#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
2 changes: 1 addition & 1 deletion include/os/freebsd/spl/sys/sysmacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ 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)))

Check failure on line 194 in include/os/freebsd/spl/sys/sysmacros.h

View workflow job for this annotation

GitHub Actions / checkstyle

line > 80 characters

Check failure on line 194 in include/os/freebsd/spl/sys/sysmacros.h

View workflow job for this annotation

GitHub Actions / checkstyle

missing space around relational operator

Check failure on line 194 in include/os/freebsd/spl/sys/sysmacros.h

View workflow job for this annotation

GitHub Actions / checkstyle

missing space between keyword and paren

/*
* return x % (mod) align
Expand Down
2 changes: 1 addition & 1 deletion include/os/linux/spl/sys/sysmacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ 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)))

Check failure on line 162 in include/os/linux/spl/sys/sysmacros.h

View workflow job for this annotation

GitHub Actions / checkstyle

line > 80 characters

Check failure on line 162 in include/os/linux/spl/sys/sysmacros.h

View workflow job for this annotation

GitHub Actions / checkstyle

missing space around relational operator

Check failure on line 162 in include/os/linux/spl/sys/sysmacros.h

View workflow job for this annotation

GitHub Actions / checkstyle

missing space between keyword and paren
#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
2 changes: 1 addition & 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,7 @@
/*
* 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)))

Check failure on line 55 in lib/libspl/include/os/linux/sys/sysmacros.h

View workflow job for this annotation

GitHub Actions / checkstyle

line > 80 characters
#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 eca2d33

Please sign in to comment.