[alsa-devel] [PATCH] ALSA: Use scnprintf() instead of snprintf()

Takashi Sakamoto o-takashi at sakamocchi.jp
Tue Feb 27 12:09:38 CET 2018


Hi,

On Feb 27 2018 15:12, Jaejoong Kim wrote:
>> We can see below explanation in 'Documentation/filesystems
>> /sysfs.txt'[1]:
>>
>> ```
>> - show() methods should return the number of bytes printed into the
>>    buffer. This is the return value of scnprintf().
>>
>> - show() must not use snprintf() when formatting the value to be
>>    returned to user space. If you can guarantee that an overflow
>>    will never happen you can use sprintf() otherwise you must use
>>    scnprintf().
>> ```
>>
>> Actually, 'snprintf()' returns written bytes excluding trailing _null_
>> byte for end output to strings and we need to use 'scnprintf()'.
> 
> Strictly speaking, that is half the truth.
> 
> snprintf() does not return the length actually written to the buffer,
> but rather to
> return the length that it was intended to be written to. So, in case
> of the formated
> buffer size is much bigger, 'snprintf()' will return formated buffer
> size not written bytes.
> 
> Please refer to the LWN about snprintf
> https://lwn.net/Articles/69419/
> 
> I do NOT think the original codes will happen if snprintf() return a
> number larger than PAGE_SIZE.
> Nevertheless, it is better to use scnprintf () for the show method
> because of the snprintf () behavior.

Ah... Exactly. I failed to get your intention, sorry... (I should have 
read your commit comment carefully...)


Thanks

Takashi Sakamoto


More information about the Alsa-devel mailing list