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 */
}
_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "[email protected]"