Subject: Re: CVS commit: src/sys/compat/linux/common
To: Martin Husemann <martin@NetBSD.org>
From: David Young <dyoung@pobox.com>
List: source-changes
Date: 02/24/2005 02:34:44
--TRYliJ5NKNqkz5bu
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Thu, Feb 24, 2005 at 08:15:53AM +0000, Martin Husemann wrote:
> 
> Module Name:	src
> Committed By:	martin
> Date:		Thu Feb 24 08:15:53 UTC 2005
> 
> Modified Files:
> 	src/sys/compat/linux/common: linux_socket.c
> 
> Log Message:
> Fix number of elements calculation when converting socket options.
> Fixes bugs 4 and 5 reported by Ted Unangst on tech-kern.

I like this patch better, what do you think?

[Really, a common idiom like NELTS() belongs in sys/cdefs.h.  Perhaps it
should be called __arraycount().]

Dave

-- 
David Young             OJC Technologies
dyoung@ojctech.com      Urbana, IL * (217) 278-3933

--TRYliJ5NKNqkz5bu
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="linux-socket.c-patch"

Index: sys/compat/linux/common/linux_socket.c
===================================================================
RCS file: /cvsroot/src/sys/compat/linux/common/linux_socket.c,v
retrieving revision 1.53
diff -u -r1.53 linux_socket.c
--- sys/compat/linux/common/linux_socket.c	12 Sep 2004 15:32:55 -0000	1.53
+++ sys/compat/linux/common/linux_socket.c	24 Feb 2005 08:32:00 -0000
@@ -182,25 +182,30 @@
 	-1,		/* pseudo_AF_HDRCMPLT */
 };
 
-static const int bsd_to_linux_msg_flags_[] = {
-	MSG_OOB,		LINUX_MSG_OOB,
-	MSG_PEEK,		LINUX_MSG_PEEK,
-	MSG_DONTROUTE,		LINUX_MSG_DONTROUTE,
-	MSG_EOR,		LINUX_MSG_EOR,
-	MSG_TRUNC,		LINUX_MSG_TRUNC,
-	MSG_CTRUNC,		LINUX_MSG_CTRUNC,
-	MSG_WAITALL,		LINUX_MSG_WAITALL,
-	MSG_DONTWAIT,		LINUX_MSG_DONTWAIT,
-	MSG_BCAST,		0,		/* not supported, clear */
-	MSG_MCAST,		0,		/* not supported, clear */
-	-1, /* not supp */	LINUX_MSG_PROBE,
-	-1, /* not supp */	LINUX_MSG_FIN,
-	-1, /* not supp */	LINUX_MSG_SYN,
-	-1, /* not supp */	LINUX_MSG_CONFIRM,
-	-1, /* not supp */	LINUX_MSG_RST,
-	-1, /* not supp */	LINUX_MSG_ERRQUEUE,
-	-1, /* not supp */	LINUX_MSG_NOSIGNAL,
-	-1, /* not supp */	LINUX_MSG_MORE,
+#define	NELTS(__x)	(sizeof(__x) / sizeof(__x[0]))
+
+static const struct {
+	int bfl;
+	int lfl;
+} bsd_to_linux_msg_flags_[] = {
+	{MSG_OOB,		LINUX_MSG_OOB},
+	{MSG_PEEK,		LINUX_MSG_PEEK},
+	{MSG_DONTROUTE,		LINUX_MSG_DONTROUTE},
+	{MSG_EOR,		LINUX_MSG_EOR},
+	{MSG_TRUNC,		LINUX_MSG_TRUNC},
+	{MSG_CTRUNC,		LINUX_MSG_CTRUNC},
+	{MSG_WAITALL,		LINUX_MSG_WAITALL},
+	{MSG_DONTWAIT,		LINUX_MSG_DONTWAIT},
+	{MSG_BCAST,		0},		/* not supported, clear */
+	{MSG_MCAST,		0},		/* not supported, clear */
+	{-1, /* not supp */	LINUX_MSG_PROBE},
+	{-1, /* not supp */	LINUX_MSG_FIN},
+	{-1, /* not supp */	LINUX_MSG_SYN},
+	{-1, /* not supp */	LINUX_MSG_CONFIRM},
+	{-1, /* not supp */	LINUX_MSG_RST},
+	{-1, /* not supp */	LINUX_MSG_ERRQUEUE},
+	{-1, /* not supp */	LINUX_MSG_NOSIGNAL},
+	{-1, /* not supp */	LINUX_MSG_MORE},
 };
 
 /*
@@ -239,9 +244,9 @@
 	if (lflag == 0)
 		return (0);
 
-	for(i=0; i < sizeof(bsd_to_linux_msg_flags_)/2; i += 2) {
-		bfl = bsd_to_linux_msg_flags_[i];
-		lfl = bsd_to_linux_msg_flags_[i+1];
+	for(i = 0; i < NELTS(bsd_to_linux_msg_flags_); i++) {
+		bfl = bsd_to_linux_msg_flags_[i].bfl;
+		lfl = bsd_to_linux_msg_flags_[i].lfl;
 
 		if (lfl == 0)
 			continue;
@@ -267,9 +272,9 @@
 	if (bflag == 0)
 		return (0);
 
-	for(i=0; i < sizeof(bsd_to_linux_msg_flags_)/2; i += 2) {
-		bfl = bsd_to_linux_msg_flags_[i];
-		lfl = bsd_to_linux_msg_flags_[i+1];
+	for(i = 0; i < NELTS(bsd_to_linux_msg_flags_); i++) {
+		bfl = bsd_to_linux_msg_flags_[i].bfl;
+		lfl = bsd_to_linux_msg_flags_[i].lfl;
 
 		if (bfl <= 0)
 			continue;

--TRYliJ5NKNqkz5bu--