Re: [PATCH 4/5] mm: shmem: Convert shmem_enabled_show to use sysfs_emit_at

From: Joe Perches
Date: Mon Nov 02 2020 - 12:01:36 EST


On Mon, 2020-11-02 at 14:40 +0000, Matthew Wilcox wrote:
> For someone who's used to C "strings", it's pretty common to do
> something like:
>
> buf += sprintf(buf, "foo ");
> buf += sprintf(buf, "bar ");

It's not equivalent code.

What was actually necessary was using scnprintf which tests the
number of bytes available in the buffer.

The actual equivalent code was something like:

int used = 0;
used += scnprintf(buf + used, PAGE_SIZE - used, "foo ");
used += scnprintf(buf + used, PAGE_SIZE - used, "bar ");

> sysfs_emit_at instead wants me to do:
>
> len += sprintf(buf + len, "foo ");
> len += sprintf(buf + len, "bar ");
>
> I don't see how the code I wrote defeats the check. It checks that the
> buffer never crosses a PAGE_SIZE boundary, which is equivalently safe.

And it'd be required to store the original buf value passed to be
able to return the actual number of bytes emitted when multiple
calls are used.

return sprintf(buf) - obuf;

This also does and can not verify that buf is originally page aligned.