mirror of
https://github.com/xdp-project/bpf-examples.git
synced 2024-05-06 15:54:53 +00:00
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:
@ -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)
|
static void print_meta_info_mark(uint8_t *pkt, struct xdp_hints_mark *meta)
|
||||||
{
|
{
|
||||||
struct xsk_btf_info *xbi = meta->xbi;
|
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);
|
XSK_BTF_READ_INTO(mark, &meta->mark, xbi, pkt);
|
||||||
if (debug_meta)
|
if (debug_meta)
|
||||||
printf("meta-mark mark:%u\n", mark);
|
printf("meta-mark mark:%u\n", mark);
|
||||||
|
@ -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 */
|
/* Notice: that field must NOT be a C-string as macro will stringify it */
|
||||||
#define XSK_BTF_READ_FIELD_INTO(dest, field, xbi, addr) ({ \
|
#define XSK_BTF_READ_FIELD_INTO(dest, field, xbi, addr) ({ \
|
||||||
typeof(dest) *_d; \
|
typeof(dest) *_d; \
|
||||||
xsk_btf__read_field((void **)&_d, sizeof(dest), #field, xbi, addr); \
|
int _err=xsk_btf__read_field((void **)&_d, sizeof(dest), #field, xbi, addr); \
|
||||||
dest = *_d; })
|
if (!_err) dest = *_d; })
|
||||||
|
|
||||||
#define XSK_BTF_READ_INTO(dest, member, xbi, addr) ({ \
|
#define XSK_BTF_READ_INTO(dest, member, xbi, addr) ({ \
|
||||||
typeof(dest) *_d; \
|
typeof(dest) *_d; \
|
||||||
xsk_btf__read((void **)&_d, sizeof(dest), member, xbi, addr); \
|
int _err=xsk_btf__read((void **)&_d, sizeof(dest), member, xbi, addr); \
|
||||||
dest = *_d; })
|
if (!_err) dest = *_d; })
|
||||||
|
|
||||||
#endif /* __LIB_XSK_EXTEND_H */
|
#endif /* __LIB_XSK_EXTEND_H */
|
||||||
|
Reference in New Issue
Block a user