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
This commit is contained in:
Jilles Tjoelker 2017-10-26 18:32:04 +00:00
parent 33d9904abc
commit 9a8ce256ed

View File

@ -707,15 +707,17 @@ nvlist_size(const nvlist_t *nvl)
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