Skip to contentSkip to conversation

sock_diag.7: Fix recvmsg() usage in the example

PATCH In review Pali Rohár 2 participants 2 messages Jan 16, 2021 – Jan 17, 2021 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sock_diag.7: Fix recvmsg() usage in the example

The msg_name field for recvmsg() call points to a caller-allocated buffer nladdr that is used to return the source address of the (netlink) socket.

As recvmsg() does not read this buffer and fills it for a caller, do not initialize it and instead check its value in the example.

---
 man7/sock_diag.7 | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
man7/sock_diag.7 +6 -3
index 91ccf3ac5..191e6ac49 100644
@@ -753,9 +753,7 @@ static int
 receive_responses(int fd)
 {
     long buf[8192 / sizeof(long)];
-    struct sockaddr_nl nladdr = {
-        .nl_family = AF_NETLINK
-    };
+    struct sockaddr_nl nladdr;
     struct iovec iov = {
         .iov_base = buf,
         .iov_len = sizeof(buf)
@@ -782,6 +780,11 @@ receive_responses(int fd)
         if (ret == 0)
             return 0;
 
+        if (nladdr.nl_family != AF_NETLINK) {
+            fputs("!AF_NETLINK\en", stderr);
+            return \-1;
+        }
+
         const struct nlmsghdr *h = (struct nlmsghdr *) buf;
 
         if (!NLMSG_OK(h, ret)) {
Signature · 1 line
2.20.1
#1 lore raw
Quoted 7 lines · Pali Rohár

The msg_name field for recvmsg() call points to a caller-allocated buffer nladdr that is used to return the source address of the (netlink) socket.

As recvmsg() does not read this buffer and fills it for a caller, do not initialize it and instead check its value in the example.

Hi Pali,

Patch applied.

Thanks,

Alex

Quoted 32 lines
---
 man7/sock_diag.7 | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
man7/sock_diag.7 +6 -3
index 91ccf3ac5..191e6ac49 100644
@@ -753,9 +753,7 @@ static int
 receive_responses(int fd)
 {
     long buf[8192 / sizeof(long)];
-    struct sockaddr_nl nladdr = {
-        .nl_family = AF_NETLINK
-    };
+    struct sockaddr_nl nladdr;
     struct iovec iov = {
         .iov_base = buf,
         .iov_len = sizeof(buf)
@@ -782,6 +780,11 @@ receive_responses(int fd)
         if (ret == 0)
             return 0;
 
+        if (nladdr.nl_family != AF_NETLINK) {
+            fputs("!AF_NETLINK\en", stderr);
+            return \-1;
+        }
+
         const struct nlmsghdr *h = (struct nlmsghdr *) buf;
 
         if (!NLMSG_OK(h, ret)) {