Module Name:    src
Committed By:   riastradh
Date:           Sat Feb  8 13:18:48 UTC 2025

Modified Files:
        src/share/man/man3: CMSG_DATA.3

Log Message:
cmsg(3): Don't use malloc in example.

Since CMSG_SPACE (and CMSG_LEN) is required to expand to an integer
constant expression by POSIX, and it does in NetBSD as of 2012, there
is no reason to use malloc here -- we can just allocate the buffer on
the stack without tripping over variable-length arrays.

PR lib/59054: cmsg(3) uses malloc(3) unnecessarily


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/share/man/man3/CMSG_DATA.3

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/share/man/man3/CMSG_DATA.3
diff -u src/share/man/man3/CMSG_DATA.3:1.3 src/share/man/man3/CMSG_DATA.3:1.4
--- src/share/man/man3/CMSG_DATA.3:1.3	Sat Jan 24 17:17:01 2015
+++ src/share/man/man3/CMSG_DATA.3	Sat Feb  8 13:18:48 2025
@@ -1,4 +1,4 @@
-.\"	$NetBSD: CMSG_DATA.3,v 1.3 2015/01/24 17:17:01 christos Exp $
+.\"	$NetBSD: CMSG_DATA.3,v 1.4 2025/02/08 13:18:48 riastradh Exp $
 .\"	$OpenBSD: CMSG_DATA.3,v 1.5 2008/03/24 16:11:07 deraadt Exp $
 .\" Written by Jared Yanovich <jar...@openbsd.org>
 .\" Public domain, July 3, 2005
@@ -98,19 +98,13 @@ struct msghdr	 msg;
 struct cmsghdr	*cmsg;
 /* We use a union to make sure hdr is aligned */
 union {
-	struct cmsghdr hdr;
-	unsigned char	 buf[CMSG_SPACE(sizeof(int))];
-} *cmsgbuf;
+	struct cmsghdr	hdr;
+	unsigned char	buf[CMSG_SPACE(sizeof(int))];
+} cmsgbuf;
 
-/*
- * We allocate in the heap instead of the stack to avoid C99
- * variable stack allocation, which breaks gcc -fstack-protector.
- */
-if ((cmsgbuf = malloc(sizeof(*cmsgbuf))) == NULL)
-	err(1, "malloc");
 (void)memset(&msg, 0, sizeof(msg));
-msg.msg_control = cmsgbuf->buf;
-msg.msg_controllen = sizeof(cmsgbuf->buf);
+msg.msg_control = cmsgbuf.buf;
+msg.msg_controllen = sizeof(cmsgbuf.buf);
 
 cmsg = CMSG_FIRSTHDR(&msg);
 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
@@ -120,7 +114,6 @@ cmsg->cmsg_type = SCM_RIGHTS;
 
 if (sendmsg(s, &msg, 0) == -1)
 	err(1, "sendmsg");
-free(cmsgbuf);
 .Ed
 .Pp
 And an example that receives and decomposes the control message:
@@ -128,15 +121,13 @@ And an example that receives and decompo
 struct msghdr	 msg;
 struct cmsghdr	*cmsg;
 union {
-	struct cmsghdr hdr;
-	unsigned char	 buf[CMSG_SPACE(sizeof(int))];
-} *cmsgbuf;
+	struct cmsghdr	hdr;
+	unsigned char	buf[CMSG_SPACE(sizeof(int))];
+} cmsgbuf;
 
-if ((cmsgbuf = malloc(sizeof(*cmsgbuf))) == NULL)
-	err(1, "malloc");
 (void)memset(&msg, 0, sizeof(msg));
-msg.msg_control = cmsgbuf->buf;
-msg.msg_controllen = sizeof(cmsgbuf->buf);
+msg.msg_control = cmsgbuf.buf;
+msg.msg_controllen = sizeof(cmsgbuf.buf);
 
 if (recvmsg(s, &msg, 0) == -1)
 	err(1, "recvmsg");
@@ -151,7 +142,6 @@ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg !=
 		/* Do something with the descriptor. */
 	}
 }
-free(cmsgbuf);
 .Ed
 .Sh SEE ALSO
 .Xr recvmsg 2 ,

Reply via email to