lkml.org 
[lkml]   [2020]   [Nov]   [2]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH 4/5] mm: shmem: Convert shmem_enabled_show to use sysfs_emit_at
From
Date
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.


\
 
 \ /
  Last update: 2020-11-02 18:01    [W:0.053 / U:5.728 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site