Skip to content

Commit 2c4779e

Browse files
tklauserborkmann
authored andcommitted
tools, bpftool: Exit on error in function codegen
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 unlikely 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 <[email protected]> Signed-off-by: Tobias Klauser <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent aa2cad0 commit 2c4779e

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

tools/bpf/bpftool/gen.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
200200
return err;
201201
}
202202

203-
static int codegen(const char *template, ...)
203+
static void codegen(const char *template, ...)
204204
{
205205
const char *src, *end;
206206
int skip_tabs = 0, n;
@@ -211,7 +211,7 @@ static int codegen(const char *template, ...)
211211
n = strlen(template);
212212
s = malloc(n + 1);
213213
if (!s)
214-
return -ENOMEM;
214+
exit(-1);
215215
src = template;
216216
dst = s;
217217

@@ -225,7 +225,7 @@ static int codegen(const char *template, ...)
225225
p_err("unrecognized character at pos %td in template '%s'",
226226
src - template - 1, template);
227227
free(s);
228-
return -EINVAL;
228+
exit(-1);
229229
}
230230
}
231231

@@ -236,7 +236,7 @@ static int codegen(const char *template, ...)
236236
p_err("not enough tabs at pos %td in template '%s'",
237237
src - template - 1, template);
238238
free(s);
239-
return -EINVAL;
239+
exit(-1);
240240
}
241241
}
242242
/* trim trailing whitespace */
@@ -257,7 +257,8 @@ static int codegen(const char *template, ...)
257257
va_end(args);
258258

259259
free(s);
260-
return n;
260+
if (n)
261+
exit(-1);
261262
}
262263

263264
static int do_skeleton(int argc, char **argv)

0 commit comments

Comments
 (0)