Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 26 Oct 2017 18:32:04 +0000 (UTC)
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r325017 - head/sys/contrib/libnv
Message-ID:  <201710261832.v9QIW4RX097214@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jilles
Date: Thu Oct 26 18:32:04 2017
New Revision: 325017
URL: https://svnweb.freebsd.org/changeset/base/325017

Log:
  libnv: Fix strict-aliasing violation with cookie
  
  In rS323851, some casts were adjusted in calls to nvlist_next() and
  nvlist_get_pararr() in order to make scan-build happy. I think these changes
  just confused scan-build into not reporting the strict-aliasing violation.
  
  For example, nvlist_xdescriptors() is causing nvlist_next() to write to its
  local variable nvp of type nvpair_t * using the lvalue *cookiep of type
  void *, which is not allowed. Given the APIs of nvlist_next(),
  nvlist_get_parent() and nvlist_get_pararr(), one possible fix is to create a
  local void *cookie in nvlist_xdescriptors() and other places, and to convert
  the value to nvpair_t * when necessary. This patch implements that fix.
  
  Reviewed by:	oshogbo
  MFC after:	1 week
  Differential Revision:	https://reviews.freebsd.org/D12760

Modified:
  head/sys/contrib/libnv/nvlist.c

Modified: head/sys/contrib/libnv/nvlist.c
==============================================================================
--- head/sys/contrib/libnv/nvlist.c	Thu Oct 26 17:56:34 2017	(r325016)
+++ head/sys/contrib/libnv/nvlist.c	Thu Oct 26 18:32:04 2017	(r325017)
@@ -707,15 +707,17 @@ out:
 static int *
 nvlist_xdescriptors(const nvlist_t *nvl, int *descs)
 {
+	void *cookie;
 	nvpair_t *nvp;
 	int type;
 
 	NVLIST_ASSERT(nvl);
 	PJDLOG_ASSERT(nvl->nvl_error == 0);
 
-	nvp = NULL;
+	cookie = NULL;
 	do {
-		while (nvlist_next(nvl, &type, (void *)&nvp) != NULL) {
+		while (nvlist_next(nvl, &type, &cookie) != NULL) {
+			nvp = cookie;
 			switch (type) {
 			case NV_TYPE_DESCRIPTOR:
 				*descs = nvpair_get_descriptor(nvp);
@@ -737,7 +739,7 @@ nvlist_xdescriptors(const nvlist_t *nvl, int *descs)
 			    }
 			case NV_TYPE_NVLIST:
 				nvl = nvpair_get_nvlist(nvp);
-				nvp = NULL;
+				cookie = NULL;
 				break;
 			case NV_TYPE_NVLIST_ARRAY:
 			    {
@@ -749,12 +751,12 @@ nvlist_xdescriptors(const nvlist_t *nvl, int *descs)
 				PJDLOG_ASSERT(nitems > 0);
 
 				nvl = value[0];
-				nvp = NULL;
+				cookie = NULL;
 				break;
 			    }
 			}
 		}
-	} while ((nvl = nvlist_get_pararr(nvl, (void *)&nvp)) != NULL);
+	} while ((nvl = nvlist_get_pararr(nvl, &cookie)) != NULL);
 
 	return (descs);
 }
@@ -784,6 +786,7 @@ size_t
 nvlist_ndescriptors(const nvlist_t *nvl)
 {
 #ifndef _KERNEL
+	void *cookie;
 	nvpair_t *nvp;
 	size_t ndescs;
 	int type;
@@ -792,16 +795,17 @@ nvlist_ndescriptors(const nvlist_t *nvl)
 	PJDLOG_ASSERT(nvl->nvl_error == 0);
 
 	ndescs = 0;
-	nvp = NULL;
+	cookie = NULL;
 	do {
-		while (nvlist_next(nvl, &type, (void *)&nvp) != NULL) {
+		while (nvlist_next(nvl, &type, &cookie) != NULL) {
+			nvp = cookie;
 			switch (type) {
 			case NV_TYPE_DESCRIPTOR:
 				ndescs++;
 				break;
 			case NV_TYPE_NVLIST:
 				nvl = nvpair_get_nvlist(nvp);
-				nvp = NULL;
+				cookie = NULL;
 				break;
 			case NV_TYPE_NVLIST_ARRAY:
 			    {
@@ -813,7 +817,7 @@ nvlist_ndescriptors(const nvlist_t *nvl)
 				PJDLOG_ASSERT(nitems > 0);
 
 				nvl = value[0];
-				nvp = NULL;
+				cookie = NULL;
 				break;
 			    }
 			case NV_TYPE_DESCRIPTOR_ARRAY:
@@ -827,7 +831,7 @@ nvlist_ndescriptors(const nvlist_t *nvl)
 			    }
 			}
 		}
-	} while ((nvl = nvlist_get_pararr(nvl, (void *)&nvp)) != NULL);
+	} while ((nvl = nvlist_get_pararr(nvl, &cookie)) != NULL);
 
 	return (ndescs);
 #else



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201710261832.v9QIW4RX097214>