Skip to content

Commit d974053

Browse files
Xu Kuohaianakryiko
authored andcommitted
libbpf: Avoid allocating reg_name with sscanf in parse_usdt_arg()
The reg_name in parse_usdt_arg() is used to hold register name, which is short enough to be held in a 16-byte array, so we could define reg_name as char reg_name[16] to avoid dynamically allocating reg_name with sscanf. Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com> Signed-off-by: Xu Kuohai <xukuohai@huawei.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Stanislav Fomichev <sdf@google.com> Link: https://lore.kernel.org/bpf/20221018145538.2046842-1-xukuohai@huaweicloud.com
1 parent eb814cf commit d974053

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

tools/lib/bpf/usdt.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,26 +1225,24 @@ static int calc_pt_regs_off(const char *reg_name)
12251225

12261226
static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg)
12271227
{
1228-
char *reg_name = NULL;
1228+
char reg_name[16];
12291229
int arg_sz, len, reg_off;
12301230
long off;
12311231

1232-
if (sscanf(arg_str, " %d @ %ld ( %%%m[^)] ) %n", &arg_sz, &off, &reg_name, &len) == 3) {
1232+
if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n", &arg_sz, &off, reg_name, &len) == 3) {
12331233
/* Memory dereference case, e.g., -4@-20(%rbp) */
12341234
arg->arg_type = USDT_ARG_REG_DEREF;
12351235
arg->val_off = off;
12361236
reg_off = calc_pt_regs_off(reg_name);
1237-
free(reg_name);
12381237
if (reg_off < 0)
12391238
return reg_off;
12401239
arg->reg_off = reg_off;
1241-
} else if (sscanf(arg_str, " %d @ %%%ms %n", &arg_sz, &reg_name, &len) == 2) {
1240+
} else if (sscanf(arg_str, " %d @ %%%15s %n", &arg_sz, reg_name, &len) == 2) {
12421241
/* Register read case, e.g., -4@%eax */
12431242
arg->arg_type = USDT_ARG_REG;
12441243
arg->val_off = 0;
12451244

12461245
reg_off = calc_pt_regs_off(reg_name);
1247-
free(reg_name);
12481246
if (reg_off < 0)
12491247
return reg_off;
12501248
arg->reg_off = reg_off;
@@ -1456,16 +1454,15 @@ static int calc_pt_regs_off(const char *reg_name)
14561454

14571455
static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg)
14581456
{
1459-
char *reg_name = NULL;
1457+
char reg_name[16];
14601458
int arg_sz, len, reg_off;
14611459
long off;
14621460

1463-
if (sscanf(arg_str, " %d @ %ld ( %m[a-z0-9] ) %n", &arg_sz, &off, &reg_name, &len) == 3) {
1461+
if (sscanf(arg_str, " %d @ %ld ( %15[a-z0-9] ) %n", &arg_sz, &off, reg_name, &len) == 3) {
14641462
/* Memory dereference case, e.g., -8@-88(s0) */
14651463
arg->arg_type = USDT_ARG_REG_DEREF;
14661464
arg->val_off = off;
14671465
reg_off = calc_pt_regs_off(reg_name);
1468-
free(reg_name);
14691466
if (reg_off < 0)
14701467
return reg_off;
14711468
arg->reg_off = reg_off;
@@ -1474,12 +1471,11 @@ static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec
14741471
arg->arg_type = USDT_ARG_CONST;
14751472
arg->val_off = off;
14761473
arg->reg_off = 0;
1477-
} else if (sscanf(arg_str, " %d @ %m[a-z0-9] %n", &arg_sz, &reg_name, &len) == 2) {
1474+
} else if (sscanf(arg_str, " %d @ %15[a-z0-9] %n", &arg_sz, reg_name, &len) == 2) {
14781475
/* Register read case, e.g., -8@a1 */
14791476
arg->arg_type = USDT_ARG_REG;
14801477
arg->val_off = 0;
14811478
reg_off = calc_pt_regs_off(reg_name);
1482-
free(reg_name);
14831479
if (reg_off < 0)
14841480
return reg_off;
14851481
arg->reg_off = reg_off;

0 commit comments

Comments
 (0)