Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 531b014e authored by Jakub Kicinski's avatar Jakub Kicinski Committed by Daniel Borkmann
Browse files

tools: bpf: make use of reallocarray



reallocarray() is a safer variant of realloc which checks for
multiplication overflow in case of array allocation.  Since it's
not available in Glibc < 2.26 import kernel's overflow.h and
add a static inline implementation when needed.  Use feature
detection to probe for existence of reallocarray.

Signed-off-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: default avatarJiong Wang <jiong.wang@netronome.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 8d13406c
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ INSTALL ?= install
RM ?= rm -f

FEATURE_USER = .bpftool
FEATURE_TESTS = libbfd disassembler-four-args
FEATURE_TESTS = libbfd disassembler-four-args reallocarray
FEATURE_DISPLAY = libbfd disassembler-four-args

check_feat := 1
@@ -75,6 +75,10 @@ ifeq ($(feature-disassembler-four-args), 1)
CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
endif

ifeq ($(feature-reallocarray), 0)
CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
endif

include $(wildcard $(OUTPUT)*.d)

all: $(OUTPUT)bpftool
+1 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@
#include <linux/compiler.h>
#include <linux/kernel.h>
#include <linux/hashtable.h>
#include <tools/libc_compat.h>

#include "json_writer.h"

+3 −3
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
 * POSSIBILITY OF SUCH DAMAGE.
 */

#define _GNU_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -66,8 +67,7 @@ void kernel_syms_load(struct dump_data *dd)
	while (!feof(fp)) {
		if (!fgets(buff, sizeof(buff), fp))
			break;
		tmp = realloc(dd->sym_mapping,
			      (dd->sym_count + 1) *
		tmp = reallocarray(dd->sym_mapping, dd->sym_count + 1,
				   sizeof(*dd->sym_mapping));
		if (!tmp) {
out:
+4 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ FILES= \
         test-libaudit.bin                      \
         test-libbfd.bin                        \
         test-disassembler-four-args.bin        \
         test-reallocarray.bin			\
         test-liberty.bin                       \
         test-liberty-z.bin                     \
         test-cplus-demangle.bin                \
@@ -204,6 +205,9 @@ $(OUTPUT)test-libbfd.bin:
$(OUTPUT)test-disassembler-four-args.bin:
	$(BUILD) -DPACKAGE='"perf"' -lbfd -lopcodes

$(OUTPUT)test-reallocarray.bin:
	$(BUILD)

$(OUTPUT)test-liberty.bin:
	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty

+8 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <stdlib.h>

int main(void)
{
	return !!reallocarray(NULL, 1, 1);
}
Loading