Skip to content

Commit 597bc74

Browse files
arndbakpm00
authored andcommitted
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/[email protected] Reviewed-by: Justin Stitt <[email protected]> Signed-off-by: Arnd Bergmann <[email protected]> Cc: Alexey Starikovskiy <[email protected]> Cc: Bob Moore <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Len Brown <[email protected]> Cc: Lin Ming <[email protected]> Cc: Masahiro Yamada <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Nicolas Schier <[email protected]> Cc: Rafael J. Wysocki <[email protected]> Cc: "Richard Russon (FlatCap)" <[email protected]> Cc: Steven Rostedt <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 3ef3a05 commit 597bc74

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

block/partitions/ldm.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc)
131131
ldm_crit ("Cannot find TOCBLOCK, database may be corrupt.");
132132
return false;
133133
}
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));
136135
toc->bitmap1_start = get_unaligned_be64(data + 0x2E);
137136
toc->bitmap1_size = get_unaligned_be64(data + 0x36);
138137

@@ -142,8 +141,7 @@ static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc)
142141
TOC_BITMAP1, toc->bitmap1_name);
143142
return false;
144143
}
145-
strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name));
146-
toc->bitmap2_name[sizeof (toc->bitmap2_name) - 1] = 0;
144+
strscpy_pad(toc->bitmap2_name, data + 0x46, sizeof(toc->bitmap2_name));
147145
toc->bitmap2_start = get_unaligned_be64(data + 0x50);
148146
toc->bitmap2_size = get_unaligned_be64(data + 0x58);
149147
if (strncmp (toc->bitmap2_name, TOC_BITMAP2,

0 commit comments

Comments
 (0)