bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf] tools, bpftool: check return value of function codegen
@ 2020-06-10 13:08 Tobias Klauser
  2020-06-10 18:50 ` Andrii Nakryiko
  2020-06-11 10:33 ` [PATCH] tools, bpftool: Exit on error in " Tobias Klauser
  0 siblings, 2 replies; 6+ messages in thread
From: Tobias Klauser @ 2020-06-10 13:08 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann; +Cc: bpf

The codegen function might fail an return an error. Check its return
value in all call sites and handle it properly.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
---
 tools/bpf/bpftool/gen.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index ecbae47e66b8..b5fa3060dce3 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -325,7 +325,7 @@ static int do_skeleton(int argc, char **argv)
 	}
 
 	get_header_guard(header_guard, obj_name);
-	codegen("\
+	err = codegen("\
 		\n\
 		/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */   \n\
 									    \n\
@@ -342,6 +342,8 @@ static int do_skeleton(int argc, char **argv)
 		",
 		obj_name, header_guard
 	);
+	if (err)
+		goto out;
 
 	if (map_cnt) {
 		printf("\tstruct {\n");
@@ -376,7 +378,7 @@ static int do_skeleton(int argc, char **argv)
 			goto out;
 	}
 
-	codegen("\
+	err = codegen("\
 		\n\
 		};							    \n\
 									    \n\
@@ -453,8 +455,10 @@ static int do_skeleton(int argc, char **argv)
 		",
 		obj_name
 	);
+	if (err)
+		goto out;
 
-	codegen("\
+	err = codegen("\
 		\n\
 									    \n\
 		static inline int					    \n\
@@ -474,7 +478,7 @@ static int do_skeleton(int argc, char **argv)
 		obj_name
 	);
 	if (map_cnt) {
-		codegen("\
+		err = codegen("\
 			\n\
 									    \n\
 				/* maps */				    \n\
@@ -486,6 +490,9 @@ static int do_skeleton(int argc, char **argv)
 			",
 			map_cnt
 		);
+		if (err)
+			goto out;
+
 		i = 0;
 		bpf_object__for_each_map(map, obj) {
 			ident = get_map_ident(map);
@@ -493,13 +500,16 @@ static int do_skeleton(int argc, char **argv)
 			if (!ident)
 				continue;
 
-			codegen("\
+			err = codegen("\
 				\n\
 									    \n\
 					s->maps[%zu].name = \"%s\";	    \n\
 					s->maps[%zu].map = &obj->maps.%s;   \n\
 				",
 				i, bpf_map__name(map), i, ident);
+			if (err)
+				goto out;
+
 			/* memory-mapped internal maps */
 			if (bpf_map__is_internal(map) &&
 			    (bpf_map__def(map)->map_flags & BPF_F_MMAPABLE)) {
@@ -510,7 +520,7 @@ static int do_skeleton(int argc, char **argv)
 		}
 	}
 	if (prog_cnt) {
-		codegen("\
+		err = codegen("\
 			\n\
 									    \n\
 				/* programs */				    \n\
@@ -522,6 +532,9 @@ static int do_skeleton(int argc, char **argv)
 			",
 			prog_cnt
 		);
+		if (err)
+			goto out;
+
 		i = 0;
 		bpf_object__for_each_program(prog, obj) {
 			codegen("\
@@ -535,13 +548,15 @@ static int do_skeleton(int argc, char **argv)
 			i++;
 		}
 	}
-	codegen("\
+	err = codegen("\
 		\n\
 									    \n\
 			s->data_sz = %d;				    \n\
 			s->data = (void *)\"\\				    \n\
 		",
 		file_sz);
+	if (err)
+		goto out;
 
 	/* embed contents of BPF object file */
 	for (i = 0, len = 0; i < file_sz; i++) {
@@ -558,7 +573,7 @@ static int do_skeleton(int argc, char **argv)
 			printf("\\x%02x", (unsigned char)obj_data[i]);
 	}
 
-	codegen("\
+	err = codegen("\
 		\n\
 		\";							    \n\
 									    \n\
@@ -571,7 +586,6 @@ static int do_skeleton(int argc, char **argv)
 		#endif /* %s */						    \n\
 		",
 		header_guard);
-	err = 0;
 out:
 	bpf_object__close(obj);
 	if (obj_data)
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-06-11 21:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-10 13:08 [PATCH bpf] tools, bpftool: check return value of function codegen Tobias Klauser
2020-06-10 18:50 ` Andrii Nakryiko
2020-06-11  9:05   ` Tobias Klauser
2020-06-11 10:33 ` [PATCH] tools, bpftool: Exit on error in " Tobias Klauser
2020-06-11 18:02   ` Andrii Nakryiko
2020-06-11 21:55     ` Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).