Skip to content

Commit

Permalink
nvpair: Use flexible array member for nvpair name strings
Browse files Browse the repository at this point in the history
Coverity reported possible out-of-bounds reads from doing `((char
*)(nvp) + sizeof (nvpair_t))` to get the nvpair name string. These were
initially marked as false positives, but since we are now using C99
flexible array members elsewhere, we could use them here too as cleanup
to make the code easier to understand.

Reviewed-by: Tino Reichardt <milky-zfs@mcmilk.de>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Reported-by: Coverity (CID-977165)
Reported-by: Coverity (CID-1524109)
Reported-by: Coverity (CID-1524642)
Closes openzfs#14612
  • Loading branch information
ryao authored and lundman committed Mar 17, 2023
1 parent f9d9a36 commit b78b834
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/sys/nvpair.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ typedef struct nvpair {
int16_t nvp_reserve; /* not used */
int32_t nvp_value_elem; /* number of elements for array types */
data_type_t nvp_type; /* type of value */
/* name string */
char nvp_name[]; /* name string */
/* aligned ptr array for string arrays */
/* aligned array of data for value */
} nvpair_t;
Expand Down Expand Up @@ -109,7 +109,7 @@ typedef struct nvlist {
#define NV_ALIGN4(x) (((x) + 3) & ~3)

#define NVP_SIZE(nvp) ((nvp)->nvp_size)
#define NVP_NAME(nvp) ((char *)(nvp) + sizeof (nvpair_t))
#define NVP_NAME(nvp) ((nvp)->nvp_name)
#define NVP_TYPE(nvp) ((nvp)->nvp_type)
#define NVP_NELEM(nvp) ((nvp)->nvp_value_elem)
#define NVP_VALUE(nvp) ((char *)(nvp) + NV_ALIGN(sizeof (nvpair_t) \
Expand Down

0 comments on commit b78b834

Please sign in to comment.