1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01:00
linux/drivers/net/ethernet/mellanox/mlxfw/mlxfw_mfa2_tlv.h
Gustavo A. R. Silva 30a87f150b net: mlxfw: Replace zero-length array with flexible-array member
The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to declare
variable-length types such as these ones is a flexible array member[1][2],
introduced in C99:

struct foo {
        int stuff;
        struct boo array[];
};

By making use of the mechanism above, we will get a compiler warning
in case the flexible array does not occur last in the structure, which
will help us prevent some kind of undefined behavior bugs from being
inadvertently introduced[3] to the codebase from now on.

Also, notice that, dynamic memory allocations won't be affected by
this change:

"Flexible array members have incomplete type, and so the sizeof operator
may not be applied. As a quirk of the original implementation of
zero-length arrays, sizeof evaluates to zero."[1]

This issue was found with the help of Coccinelle.

[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit 7649773293 ("cxgb3/l2t: Fix undefined behaviour")

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Reviewed-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2020-03-03 17:39:19 -08:00

67 lines
1.9 KiB
C

/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
/* Copyright (c) 2017-2019 Mellanox Technologies. All rights reserved */
#ifndef _MLXFW_MFA2_TLV_H
#define _MLXFW_MFA2_TLV_H
#include <linux/kernel.h>
#include "mlxfw_mfa2_file.h"
struct mlxfw_mfa2_tlv {
u8 version;
u8 type;
__be16 len;
u8 data[];
} __packed;
static inline const struct mlxfw_mfa2_tlv *
mlxfw_mfa2_tlv_get(const struct mlxfw_mfa2_file *mfa2_file, const void *ptr)
{
if (!mlxfw_mfa2_valid_ptr(mfa2_file, ptr) ||
!mlxfw_mfa2_valid_ptr(mfa2_file, ptr + sizeof(struct mlxfw_mfa2_tlv)))
return NULL;
return ptr;
}
static inline const void *
mlxfw_mfa2_tlv_payload_get(const struct mlxfw_mfa2_file *mfa2_file,
const struct mlxfw_mfa2_tlv *tlv, u8 payload_type,
size_t payload_size, bool varsize)
{
void *tlv_top;
tlv_top = (void *) tlv + be16_to_cpu(tlv->len) - 1;
if (!mlxfw_mfa2_valid_ptr(mfa2_file, tlv) ||
!mlxfw_mfa2_valid_ptr(mfa2_file, tlv_top))
return NULL;
if (tlv->type != payload_type)
return NULL;
if (varsize && (be16_to_cpu(tlv->len) < payload_size))
return NULL;
if (!varsize && (be16_to_cpu(tlv->len) != payload_size))
return NULL;
return tlv->data;
}
#define MLXFW_MFA2_TLV(name, payload_type, tlv_type) \
static inline const payload_type * \
mlxfw_mfa2_tlv_ ## name ## _get(const struct mlxfw_mfa2_file *mfa2_file, \
const struct mlxfw_mfa2_tlv *tlv) \
{ \
return mlxfw_mfa2_tlv_payload_get(mfa2_file, tlv, \
tlv_type, sizeof(payload_type), \
false); \
}
#define MLXFW_MFA2_TLV_VARSIZE(name, payload_type, tlv_type) \
static inline const payload_type * \
mlxfw_mfa2_tlv_ ## name ## _get(const struct mlxfw_mfa2_file *mfa2_file, \
const struct mlxfw_mfa2_tlv *tlv) \
{ \
return mlxfw_mfa2_tlv_payload_get(mfa2_file, tlv, \
tlv_type, sizeof(payload_type), \
true); \
}
#endif