All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ext4: fix -Wstringop-truncation warnings
@ 2020-11-12  9:33 Kang Wenlin
  2020-12-16  4:20 ` Theodore Y. Ts'o
  0 siblings, 1 reply; 3+ messages in thread
From: Kang Wenlin @ 2020-11-12  9:33 UTC (permalink / raw)
  To: tytso, adilger.kernel; +Cc: linux-ext4, linux-kernel

From: Wenlin Kang <wenlin.kang@windriver.com>

The strncpy() function may create a unterminated string,
use strscpy_pad() instead.

This fixes the following warning:

fs/ext4/super.c: In function '__save_error_info':
fs/ext4/super.c:349:2: warning: 'strncpy' specified bound 32 equals destination size [-Wstringop-truncation]
  strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/ext4/super.c:353:3: warning: 'strncpy' specified bound 32 equals destination size [-Wstringop-truncation]
   strncpy(es->s_first_error_func, func,
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    sizeof(es->s_first_error_func));
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Wenlin Kang <wenlin.kang@windriver.com>
---
 fs/ext4/super.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index c3b864588a0b..3ade6e1f7a4d 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -446,7 +446,7 @@ static void __save_error_info(struct super_block *sb, int error,
 		return;
 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
 	ext4_update_tstamp(es, s_last_error_time);
-	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
+	strscpy_pad(es->s_last_error_func, func, sizeof(es->s_last_error_func));
 	es->s_last_error_line = cpu_to_le32(line);
 	es->s_last_error_ino = cpu_to_le32(ino);
 	es->s_last_error_block = cpu_to_le64(block);
@@ -507,7 +507,7 @@ static void __save_error_info(struct super_block *sb, int error,
 	if (!es->s_first_error_time) {
 		es->s_first_error_time = es->s_last_error_time;
 		es->s_first_error_time_hi = es->s_last_error_time_hi;
-		strncpy(es->s_first_error_func, func,
+		strscpy_pad(es->s_first_error_func, func,
 			sizeof(es->s_first_error_func));
 		es->s_first_error_line = cpu_to_le32(line);
 		es->s_first_error_ino = es->s_last_error_ino;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] ext4: fix -Wstringop-truncation warnings
  2020-11-12  9:33 [PATCH] ext4: fix -Wstringop-truncation warnings Kang Wenlin
@ 2020-12-16  4:20 ` Theodore Y. Ts'o
  2020-12-17  7:00   ` Wenlin Kang
  0 siblings, 1 reply; 3+ messages in thread
From: Theodore Y. Ts'o @ 2020-12-16  4:20 UTC (permalink / raw)
  To: Kang Wenlin; +Cc: adilger.kernel, linux-ext4, linux-kernel

On Thu, Nov 12, 2020 at 05:33:24PM +0800, Kang Wenlin wrote:
> From: Wenlin Kang <wenlin.kang@windriver.com>
> 
> The strncpy() function may create a unterminated string,
> use strscpy_pad() instead.
> 
> This fixes the following warning:
> 
> fs/ext4/super.c: In function '__save_error_info':
> fs/ext4/super.c:349:2: warning: 'strncpy' specified bound 32 equals destination size [-Wstringop-truncation]
>   strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
>   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> fs/ext4/super.c:353:3: warning: 'strncpy' specified bound 32 equals destination size [-Wstringop-truncation]
>    strncpy(es->s_first_error_func, func,
>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>     sizeof(es->s_first_error_func));
>     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

What compiler are you using?  s_last_error_func is defined to not
necessarily be NUL terminated.  So strscpy_pad() is not a proper
replacement for strncpy() in this use case.

From Documentation/process/deprecated:

   If a caller is using non-NUL-terminated strings, strncpy() can
   still be used, but destinations should be marked with the `__nonstring
   <https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html>`_
   attribute to avoid future compiler warnings.

s_{first,last}_error_func is properly annotated with __nonstring in
fs/ext4/ext4.h.

						- Ted

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ext4: fix -Wstringop-truncation warnings
  2020-12-16  4:20 ` Theodore Y. Ts'o
@ 2020-12-17  7:00   ` Wenlin Kang
  0 siblings, 0 replies; 3+ messages in thread
From: Wenlin Kang @ 2020-12-17  7:00 UTC (permalink / raw)
  To: Theodore Y. Ts'o; +Cc: adilger.kernel, linux-ext4, linux-kernel

On 2020/12/16 下午12:20, Theodore Y. Ts'o wrote:
> [Please note this e-mail is from an EXTERNAL e-mail address]
>
> On Thu, Nov 12, 2020 at 05:33:24PM +0800, Kang Wenlin wrote:
>> From: Wenlin Kang <wenlin.kang@windriver.com>
>>
>> The strncpy() function may create a unterminated string,
>> use strscpy_pad() instead.
>>
>> This fixes the following warning:
>>
>> fs/ext4/super.c: In function '__save_error_info':
>> fs/ext4/super.c:349:2: warning: 'strncpy' specified bound 32 equals destination size [-Wstringop-truncation]
>>    strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
>>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> fs/ext4/super.c:353:3: warning: 'strncpy' specified bound 32 equals destination size [-Wstringop-truncation]
>>     strncpy(es->s_first_error_func, func,
>>     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>      sizeof(es->s_first_error_func));
>>      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> What compiler are you using?  s_last_error_func is defined to not
> necessarily be NUL terminated.  So strscpy_pad() is not a proper
> replacement for strncpy() in this use case.


My compiler is gcc 8.2.0,  this is found in v4.18, and I see mainline 
codes is
using the same code too,  so sent this patch. But according to your 
instructions,
I just re-check the code, with "__nonstring" attribute, it seems it has 
fixed.
Thank for your explain.


>
>  From Documentation/process/deprecated:
>
>     If a caller is using non-NUL-terminated strings, strncpy() can
>     still be used, but destinations should be marked with the `__nonstring
>     <https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html>`_
>     attribute to avoid future compiler warnings.
>
> s_{first,last}_error_func is properly annotated with __nonstring in
> fs/ext4/ext4.h.
>
>                                                  - Ted


-- 
Thanks,
Wenlin Kang


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-12-17  6:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-12  9:33 [PATCH] ext4: fix -Wstringop-truncation warnings Kang Wenlin
2020-12-16  4:20 ` Theodore Y. Ts'o
2020-12-17  7:00   ` Wenlin Kang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.