[PATCH] include/uapi/linux/netlink.h -Wall -Wextra -Werror support fix

From: Doron Tsur
Date: Sun Dec 15 2013 - 12:31:44 EST


For code using:
#include <linux/netlink.h>
NLMSG_OK
-I<INSTALL_HDR_PATH>/include

Issue:
(nlh)->nlmsg_len is of type __u32, while len is of type int,
according to (len) >= (int)sizeof(struct nlmsghdr).
Hence (nlh)->nlmsg_len <= (len) sign-compare warning when using NLMSG_OK

Fix:
Add (int) casting:
- (nlh)->nlmsg_len <= (len))
+ (int)(nlh)->nlmsg_len <= (len))

Test log:
Compilation environments:
Ubuntu 13.10, x86_64, gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
Ubuntu 13.04, x86_64, gcc (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
[ -ftrack-macro-expansion=2 issue submitted:
gcc.gnu.org/bugzilla/show_bug.cgi?id=59495 ]

Kernel headers installed at:
make -C .../linux-stable headers_install \
INSTALL_HDR_PATH=/builds/kernel_tests_user_headers/

Pre-patch compilation:
gcc -Wall -Wextra -Werror
-I/builds/kernel_tests_pre_patch_user_headers/include
include-uapi-linux-netlink.h.test.c
In file included from include-uapi-linux-netlink.h.test.c:2:0:
include-uapi-linux-netlink.h.test.c: In function âmainâ:
/builds/kernel_tests_pre_patch_user_headers/include/linux/netlink.h:89:24:
error: comparison between signed and unsigned integer expressions
[-Werror=sign-compare]
(nlh)->nlmsg_len <= (len))
^
include-uapi-linux-netlink.h.test.c:26:5: note: in expansion of macro âNLMSG_OKâ
NLMSG_OK(&auxiliary_netlink_header, active_len));
^
cc1: all warnings being treated as errors

Post-patch:
$ gcc -Wall -Wextra -Werror
-I/builds/kernel_tests_user_headers/include/
include-uapi-linux-netlink.h.test.c

$ ./a.out
nlmsg_len= 1, len = 1 NLMSG_OK(nlh,len) test result: 0
nlmsg_len= 1, len = 3000 NLMSG_OK(nlh,len) test result: 0
nlmsg_len= 3000, len = 1 NLMSG_OK(nlh,len) test result: 0
nlmsg_len= 3000, len = 3000 NLMSG_OK(nlh,len) test result: 1

Tested-by: Doron Tsur <doront@xxxxxxxxxxxx>
Signed-off-by: Doron Tsur <doront@xxxxxxxxxxxx>
---
include/uapi/linux/netlink.h | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/include/uapi/linux/netlink.h b/include/uapi/linux/netlink.h
index 1a85940..1884d4e 100644
--- a/include/uapi/linux/netlink.h
+++ b/include/uapi/linux/netlink.h
@@ -86,7 +86,7 @@ struct nlmsghdr {
(struct nlmsghdr*)(((char*)(nlh)) + NLMSG_ALIGN((nlh)->nlmsg_len)))
#define NLMSG_OK(nlh,len) ((len) >= (int)sizeof(struct nlmsghdr) && \
(nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \
- (nlh)->nlmsg_len <= (len))
+ (int)(nlh)->nlmsg_len <= (len))
#define NLMSG_PAYLOAD(nlh,len) ((nlh)->nlmsg_len - NLMSG_SPACE((len)))

#define NLMSG_NOOP 0x1 /* Nothing. */
--
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/