public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] tools/nolibc: Fix S_ISxxx macros
@ 2023-01-06 17:18 Warner Losh
  2023-01-08  9:34 ` Willy Tarreau
  0 siblings, 1 reply; 2+ messages in thread
From: Warner Losh @ 2023-01-06 17:18 UTC (permalink / raw)
  To: Willy Tarreau; +Cc: linux-kernel, Warner Losh

The mode field has the type encoded as an value in a field, not as a bit
mask. Mask the mode with S_IFMT instead of each type to test. Otherwise,
false positives are possible: eg S_ISDIR will return true for block
devices because S_IFDIR = 0040000 and S_IFBLK = 0060000 since mode is
masked with S_IFDIR instead of S_IFMT. These macros now match the
similar definitions in tools/include/uapi/linux/stat.h.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 tools/include/nolibc/types.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h
index 959997034e55..7770328dcef7 100644
--- a/tools/include/nolibc/types.h
+++ b/tools/include/nolibc/types.h
@@ -26,13 +26,13 @@
 #define S_IFSOCK       0140000
 #define S_IFMT         0170000
 
-#define S_ISDIR(mode)  (((mode) & S_IFDIR)  == S_IFDIR)
-#define S_ISCHR(mode)  (((mode) & S_IFCHR)  == S_IFCHR)
-#define S_ISBLK(mode)  (((mode) & S_IFBLK)  == S_IFBLK)
-#define S_ISREG(mode)  (((mode) & S_IFREG)  == S_IFREG)
-#define S_ISFIFO(mode) (((mode) & S_IFIFO)  == S_IFIFO)
-#define S_ISLNK(mode)  (((mode) & S_IFLNK)  == S_IFLNK)
-#define S_ISSOCK(mode) (((mode) & S_IFSOCK) == S_IFSOCK)
+#define S_ISDIR(mode)  (((mode) & S_IFMT) == S_IFDIR)
+#define S_ISCHR(mode)  (((mode) & S_IFMT) == S_IFCHR)
+#define S_ISBLK(mode)  (((mode) & S_IFMT) == S_IFBLK)
+#define S_ISREG(mode)  (((mode) & S_IFMT) == S_IFREG)
+#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
+#define S_ISLNK(mode)  (((mode) & S_IFMT) == S_IFLNK)
+#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
 
 /* dirent types */
 #define DT_UNKNOWN     0x0
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] tools/nolibc: Fix S_ISxxx macros
  2023-01-06 17:18 [PATCH v2] tools/nolibc: Fix S_ISxxx macros Warner Losh
@ 2023-01-08  9:34 ` Willy Tarreau
  0 siblings, 0 replies; 2+ messages in thread
From: Willy Tarreau @ 2023-01-08  9:34 UTC (permalink / raw)
  To: Warner Losh; +Cc: linux-kernel

Hello Warner,

On Fri, Jan 06, 2023 at 10:18:34AM -0700, Warner Losh wrote:
> The mode field has the type encoded as an value in a field, not as a bit
> mask. Mask the mode with S_IFMT instead of each type to test. Otherwise,
> false positives are possible: eg S_ISDIR will return true for block
> devices because S_IFDIR = 0040000 and S_IFBLK = 0060000 since mode is
> masked with S_IFDIR instead of S_IFMT. These macros now match the
> similar definitions in tools/include/uapi/linux/stat.h.

I'm impressed to see how I managed to make that gross a mistake!
Thanks for catching it and for the fix, its now queued.

Willy

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-01-08  9:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-06 17:18 [PATCH v2] tools/nolibc: Fix S_ISxxx macros Warner Losh
2023-01-08  9:34 ` Willy Tarreau

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox