Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

block/partitions/ldm: convert strncpy() to strscpy()

The strncpy() here can cause a non-terminated string, which older gcc
versions such as gcc-9 warn about:

In function 'ldm_parse_tocblock',
inlined from 'ldm_validate_tocblocks' at block/partitions/ldm.c:386:7,
inlined from 'ldm_partition' at block/partitions/ldm.c:1457:7:
block/partitions/ldm.c:134:2: error: 'strncpy' specified bound 16 equals destination size [-Werror=stringop-truncation]
134 | strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
block/partitions/ldm.c:145:2: error: 'strncpy' specified bound 16 equals destination size [-Werror=stringop-truncation]
145 | strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

New versions notice that the code is correct after all because of the
following termination, but replacing the strncpy() with strscpy_pad()
or strcpy() avoids the warning and simplifies the code at the same time.

Use the padding version here to keep the existing behavior, in case
the code relies on not including uninitialized data.

Link: https://lkml.kernel.org/r/20240409140059.3806717-4-arnd@kernel.org
Reviewed-by: Justin Stitt <justinstitt@google.com>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Alexey Starikovskiy <astarikovskiy@suse.de>
Cc: Bob Moore <robert.moore@intel.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Len Brown <lenb@kernel.org>
Cc: Lin Ming <ming.m.lin@intel.com>
Cc: Masahiro Yamada <masahiroy@kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nicolas Schier <nicolas@fjasle.eu>
Cc: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Cc: "Richard Russon (FlatCap)" <ldm@flatcap.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Arnd Bergmann and committed by
Andrew Morton
597bc741 3ef3a05b

+2 -4
+2 -4
block/partitions/ldm.c
··· 131 131 ldm_crit ("Cannot find TOCBLOCK, database may be corrupt."); 132 132 return false; 133 133 } 134 - strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name)); 135 - toc->bitmap1_name[sizeof (toc->bitmap1_name) - 1] = 0; 134 + strscpy_pad(toc->bitmap1_name, data + 0x24, sizeof(toc->bitmap1_name)); 136 135 toc->bitmap1_start = get_unaligned_be64(data + 0x2E); 137 136 toc->bitmap1_size = get_unaligned_be64(data + 0x36); 138 137 ··· 141 142 TOC_BITMAP1, toc->bitmap1_name); 142 143 return false; 143 144 } 144 - strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name)); 145 - toc->bitmap2_name[sizeof (toc->bitmap2_name) - 1] = 0; 145 + strscpy_pad(toc->bitmap2_name, data + 0x46, sizeof(toc->bitmap2_name)); 146 146 toc->bitmap2_start = get_unaligned_be64(data + 0x50); 147 147 toc->bitmap2_size = get_unaligned_be64(data + 0x58); 148 148 if (strncmp (toc->bitmap2_name, TOC_BITMAP2,