diff mbox series

tools, bpftool: Exit on error in function codegen

Message ID 20200611103341.21532-1-tklauser@distanz.ch
State Accepted
Delegated to: BPF Maintainers
Headers show
Series tools, bpftool: Exit on error in function codegen | expand

Commit Message

Tobias Klauser June 11, 2020, 10:33 a.m. UTC
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(-)

Comments

Andrii Nakryiko June 11, 2020, 6:02 p.m. UTC | #1
On Thu, Jun 11, 2020 at 3:33 AM Tobias Klauser <tklauser@distanz.ch> wrote:
>
> 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>
> ---

LGTM. Thanks!

Acked-by: Andrii Nakryiko <andriin@fb.com>

[...]
Daniel Borkmann June 11, 2020, 9:55 p.m. UTC | #2
On 6/11/20 8:02 PM, Andrii Nakryiko wrote:
> On Thu, Jun 11, 2020 at 3:33 AM Tobias Klauser <tklauser@distanz.ch> wrote:
>>
>> 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>
>> ---
> 
> LGTM. Thanks!
> 
> Acked-by: Andrii Nakryiko <andriin@fb.com>

Applied, thanks!
diff mbox series

Patch

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)