[dpdk-dev] [PATCH] net/ring: fix unchecked return value
Kevin Laatz
kevin.laatz at intel.com
Wed Sep 23 12:04:34 CEST 2020
On 23/09/2020 10:43, David Marchand wrote:
> On Wed, Sep 23, 2020 at 11:39 AM Bruce Richardson
> <bruce.richardson at intel.com> wrote:
>> I think a more standard way of checking for trailing chars is to use %n
>> which stores the number of chars processed. Then check that against
>> strlen.
>>
>> For example something like:
>>
>> if (sscanf(value, "%p%n", args, n) != 1 || n != strlen(value)) {
>> /* do error handling */
>> }
>>
> The man is a bit scary about %n:
>
> The C standard says: "Execution of a %n directive does not increment
> the assignment count returned at the completion of execution" but the
> Corrigendum seems to contradict this. Probably it is wise not to make
> any assumptions on the effect of %n conversions on the return value.
I still think the check is required.
As you rightly point out, the value will be truncated once sscanf deems
a character incorrect. This will happen regardless of if we check the
return or not. What the check catches, however, is the case where the
incorrect character comes at the beginning, e.g. Z0x1234 - which would
truncated the entire arg.
$ ./sscanf Z0x1234
'%p' KO for Z0x1234
'%p%c' KO for Z0x1234
---
Kevin
More information about the dev
mailing list