bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tobias Klauser <tklauser@distanz.ch>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf@vger.kernel.org
Subject: [PATCH] tools, bpftool: Exit on error in function codegen
Date: Thu, 11 Jun 2020 12:33:41 +0200	[thread overview]
Message-ID: <20200611103341.21532-1-tklauser@distanz.ch> (raw)
In-Reply-To: <20200610130807.21497-1-tklauser@distanz.ch>

Currently, the codegen function might fail and return an error. But its
callers continue without checking its return value. Since codegen can
fail only in the ounlikely case of the system running out of memory or
the static template being malformed, just exit(-1) directly from codegen
and make it void-returning.

Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
---
Replaces https://lore.kernel.org/bpf/20200610130807.21497-1-tklauser@distanz.ch/
and to be applied on top of
https://lore.kernel.org/bpf/20200610130804.21423-1-tklauser@distanz.ch/

 tools/bpf/bpftool/gen.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index ecbae47e66b8..7443879e87af 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -200,7 +200,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
 	return err;
 }
 
-static int codegen(const char *template, ...)
+static void codegen(const char *template, ...)
 {
 	const char *src, *end;
 	int skip_tabs = 0, n;
@@ -211,7 +211,7 @@ static int codegen(const char *template, ...)
 	n = strlen(template);
 	s = malloc(n + 1);
 	if (!s)
-		return -ENOMEM;
+		exit(-1);
 	src = template;
 	dst = s;
 
@@ -225,7 +225,7 @@ static int codegen(const char *template, ...)
 			p_err("unrecognized character at pos %td in template '%s'",
 			      src - template - 1, template);
 			free(s);
-			return -EINVAL;
+			exit(-1);
 		}
 	}
 
@@ -236,7 +236,7 @@ static int codegen(const char *template, ...)
 				p_err("not enough tabs at pos %td in template '%s'",
 				      src - template - 1, template);
 				free(s);
-				return -EINVAL;
+				exit(-1);
 			}
 		}
 		/* trim trailing whitespace */
@@ -257,7 +257,8 @@ static int codegen(const char *template, ...)
 	va_end(args);
 
 	free(s);
-	return n;
+	if (n)
+		exit(-1);
 }
 
 static int do_skeleton(int argc, char **argv)
-- 
2.27.0


  parent reply	other threads:[~2020-06-11 10:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Tobias Klauser [this message]
2020-06-11 18:02   ` [PATCH] tools, bpftool: Exit on error in " Andrii Nakryiko
2020-06-11 21:55     ` Daniel Borkmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200611103341.21532-1-tklauser@distanz.ch \
    --to=tklauser@distanz.ch \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).