Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 4 Jun 2018 18:47:14 +0000 (UTC)
From:      Conrad Meyer <cem@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r334624 - head/sys/libkern
Message-ID:  <201806041847.w54IlEnb095421@repo.freebsd.org>

Next in thread | Raw E-Mail | Index | Archive | Help
Author: cem
Date: Mon Jun  4 18:47:14 2018
New Revision: 334624
URL: https://svnweb.freebsd.org/changeset/base/334624

Log:
  str(r)chr: Replace union abuse with __DECONST
  
  Writing one union member and reading another is technically illegal C,
  although we do it in many places in the tree.  Use the __DECONST macro
  instead, which is (technically) a valid C construct.
  
  Trivial style(9) cleanups to touched lines while here.
  
  Sponsored by:	Dell EMC Isilon

Modified:
  head/sys/libkern/strchr.c
  head/sys/libkern/strrchr.c

Modified: head/sys/libkern/strchr.c
==============================================================================
--- head/sys/libkern/strchr.c	Mon Jun  4 17:49:34 2018	(r334623)
+++ head/sys/libkern/strchr.c	Mon Jun  4 18:47:14 2018	(r334624)
@@ -36,19 +36,16 @@ __FBSDID("$FreeBSD$");
 #include <sys/libkern.h>
 
 char *
-strchr(const char *p, int ch)
+strchr(const char *cp, int ch)
 {
-	union {
-		const char *cp;
-		char *p;
-	} u;
+	char *p;
 
-	u.cp = p;
-	for (;; ++u.p) {
-		if (*u.p == ch)
-			return(u.p);
-		if (*u.p == '\0')
-			return(NULL);
+	p = __DECONST(char *, cp);
+	for (;; ++p) {
+		if (*p == ch)
+			return (p);
+		if (*p == '\0')
+			return (NULL);
 	}
 	/* NOTREACHED */
 }

Modified: head/sys/libkern/strrchr.c
==============================================================================
--- head/sys/libkern/strrchr.c	Mon Jun  4 17:49:34 2018	(r334623)
+++ head/sys/libkern/strrchr.c	Mon Jun  4 18:47:14 2018	(r334624)
@@ -36,20 +36,16 @@ __FBSDID("$FreeBSD$");
 #include <sys/libkern.h>
 
 char *
-strrchr(const char *p, int ch)
+strrchr(const char *cp, int ch)
 {
-	union {
-		const char *cp;
-		char *p;
-	} u;
-	char *save;
+	char *p, *save;
 
-	u.cp = p;
-	for (save = NULL;; ++u.p) {
-		if (*u.p == ch)
-			save = u.p;
-		if (*u.p == '\0')
-			return(save);
+	p = __DECONST(char *, cp);
+	for (save = NULL;; ++p) {
+		if (*p == ch)
+			save = p;
+		if (*p == '\0')
+			return (save);
 	}
 	/* NOTREACHED */
 }



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?201806041847.w54IlEnb095421>