Re: [PATCH v3 0/4] Make sscanf() stricter

From: Petr Mladek
Date: Tue Jun 20 2023 - 10:58:43 EST


On Tue 2023-06-20 16:52:42, Andy Shevchenko wrote:
> On Tue, Jun 20, 2023 at 03:34:09PM +0200, Petr Mladek wrote:
> > On Thu 2023-06-15 14:23:59, Andy Shevchenko wrote:
> > > On Thu, Jun 15, 2023 at 08:06:46AM +0000, David Laight wrote:
>
> ...
>
> > + %pj or another %p modifiers would be hard to understand either.
> >
> > Yes, we have %pe but I think that only few people really use it.
> > And it is kind of self-explanatory because it is typically
> > used together with ERR_PTR() and with variables called
> > "err" or "ret".
>
> j, besides the luck of no (yet) use in the kernel's printf(), is
> described for printf(3)
>
> j A following integer conversion corresponds to an intmax_t or uintmax_t
> argument, or a following n conversion corresponds to a pointer to an
> intmax_t argument.

I see, I have missed this coincidence. And we would really need to use %pj.
%jd requires intmax_t variable. Otherwise, the compiler produces:

kernel/lib/test.c:10:17: error: format ‘%jd’ expects argument of type ‘intmax_t *’, but argument 3 has type ‘int *’ [-Werror=format=]
sscanf(str, "%jd hello.", &tmp);

Hmm, %pj might even make some sense for sscanf() which requires pointers anyway.
But still, we would lose the compiler check of the size of the passed
buffer.

This is actually my concern with many other %p modifiers. The compiler
is not able to check that we pass the right pointer. I know that this
might happen even with wrong buffer passed to %s or so. But number
types is another category.


> So, I think among all proposals, this one is the best (while all of them may
> sound not good).

I still prefer the custom handler when it is not too complex.

Or if there are many users, we could create sscanf_strict() or so.

Best Regards,
Petr