lib_xsk_extend: Avoid crashing in XSK_BTF_READ_xxx macros

The macros XSK_BTF_READ_xxx doesn't handle or report on errors
if the input or field were wrong, which is not uncommon for macros.

The bad behavior is that the macro continue to dereference the
pointer even-when xsk_btf__read_xxx() functions returns an error.
This often leads to crashing the application.

Change macro to only dereference when no errors were reported.

Side-note: The compiler warnings will detect if API user didn't
init the 'dest' value with a default value.  As the effect of
the change is that the 'dest' value will not be touch, and thus
contain the value before the macro was used.

Example:
  warning: ‘mark’ may be used uninitialized in this function

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
This commit is contained in:
Jesper Dangaard Brouer
2021-11-12 10:01:58 +01:00
parent 1337b2dbaa
commit 8db4d3b33c
2 changed files with 6 additions and 5 deletions

View File

@ -470,8 +470,9 @@ static int print_meta_info_time_api2(uint8_t *pkt)
static void print_meta_info_mark(uint8_t *pkt, struct xdp_hints_mark *meta)
{
struct xsk_btf_info *xbi = meta->xbi;
__u32 mark;
__u32 mark = 0;
/* The 'mark' value is not updated in case of errors */
XSK_BTF_READ_INTO(mark, &meta->mark, xbi, pkt);
if (debug_meta)
printf("meta-mark mark:%u\n", mark);

View File

@ -43,12 +43,12 @@ LIBBPF_API bool xsk_btf__field_member(const char *field, struct xsk_btf_info *xb
/* Notice: that field must NOT be a C-string as macro will stringify it */
#define XSK_BTF_READ_FIELD_INTO(dest, field, xbi, addr) ({ \
typeof(dest) *_d; \
xsk_btf__read_field((void **)&_d, sizeof(dest), #field, xbi, addr); \
dest = *_d; })
int _err=xsk_btf__read_field((void **)&_d, sizeof(dest), #field, xbi, addr); \
if (!_err) dest = *_d; })
#define XSK_BTF_READ_INTO(dest, member, xbi, addr) ({ \
typeof(dest) *_d; \
xsk_btf__read((void **)&_d, sizeof(dest), member, xbi, addr); \
dest = *_d; })
int _err=xsk_btf__read((void **)&_d, sizeof(dest), member, xbi, addr); \
if (!_err) dest = *_d; })
#endif /* __LIB_XSK_EXTEND_H */