[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <878r7o5dht.fsf@nvidia.com>
Date: Fri, 27 Oct 2023 15:12:46 +0200
From: Petr Machata <me@...chata.org>
To: David Ahern <dsahern@...il.com>
Cc: Jiri Pirko <jiri@...nulli.us>, netdev@...r.kernel.org,
stephen@...workplumber.org, daniel.machon@...rochip.com
Subject: Re: [patch iproute2-next v3 3/6] devlink: extend
pr_out_nested_handle() to print object
David Ahern <dsahern@...il.com> writes:
> On 10/24/23 4:04 AM, Jiri Pirko wrote:
>> @@ -2861,6 +2842,38 @@ static void pr_out_selftests_handle_end(struct dl *dl)
>> __pr_out_newline();
>> }
>>
>> +static void __pr_out_nested_handle(struct dl *dl, struct nlattr *nla_nested_dl,
>> + bool is_object)
>> +{
>> + struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
>> + int err;
>> +
>> + err = mnl_attr_parse_nested(nla_nested_dl, attr_cb, tb);
>> + if (err != MNL_CB_OK)
>> + return;
>> +
>> + if (!tb[DEVLINK_ATTR_BUS_NAME] ||
>> + !tb[DEVLINK_ATTR_DEV_NAME])
>> + return;
>> +
>> + if (!is_object) {
>> + char buf[64];
>> +
>> + sprintf(buf, "%s/%s", mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]),
>> + mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]));
>
> buf[64] - 1 for null terminator - 16 for IFNAMSIZ leaves 47. I do not
> see limits on bus name length, so how can you guarantee it is always <
> 47 characters?
>
> Make this snprintf, check the return and make sure buf is null terminated.
I was wondering whether somehing like this might make sense in the
iproute2 library:
#define alloca_sprintf(FMT, ...) ({ \
int xasprintf_n = snprintf(NULL, 0, (FMT), __VA_ARGS__); \
char *xasprintf_buf = alloca(xasprintf_n); \
sprintf(xasprintf_buf, (FMT), __VA_ARGS__); \
xasprintf_buf; \
})
void foo() {
const char *buf = alloca_sprintf("%x %y %z", etc.);
printf(... buf ...);
}
I'm not really happy with it -- because of alloca vs. array, and because
of the double evaluation. But all those SPRINT_BUF's peppered everywhere
make me uneasy every time I read or write them.
Or maybe roll something custom asprintf-like that can reuse and/or
realloc a passed-in buffer?
The sprintf story is pretty bad in iproute2 right now, IMHO.
Powered by blists - more mailing lists