Skip to contentSkip to conversation

ubifs: fix snprintf() checking

PATCH In review Dan Carpenter 2 participants 2 messages May 11, 2021 – May 12, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ubifs: fix snprintf() checking

The snprintf() function returns the number of characters (not counting the NUL terminator) that it would have printed if we had space.

This buffer has UBIFS_DFS_DIR_LEN characters plus one extra for the terminator. Printing UBIFS_DFS_DIR_LEN is okay but anything higher will result in truncation. Thus the comparison needs to be change from == to >.

These strings are compile time constants so this patch doesn't affect runtime.

Fixes ae380ce04731 ("UBIFS: lessen the size of debugging info data structure")
---
 drivers/mtd/ubi/debug.c | 2 +-
 fs/ubifs/debug.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/mtd/ubi/debug.c +1 -1
index ac2bdba8bb1a..3c0c8eca4d51 100644
@@ -511,7 +511,7 @@ int ubi_debugfs_init_dev(struct ubi_device *ubi)
 
 	n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
 		     ubi->ubi_num);
-	if (n == UBI_DFS_DIR_LEN) {
+	if (n > UBI_DFS_DIR_LEN) {
 		/* The array size is too small */
 		return -EINVAL;
 	}
fs/ubifs/debug.c +1 -1
index 1bbb9fe661b1..fc718f6178f2 100644
@@ -2824,7 +2824,7 @@ void dbg_debugfs_init_fs(struct ubifs_info *c)
 
 	n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
 		     c->vi.ubi_num, c->vi.vol_id);
-	if (n == UBIFS_DFS_DIR_LEN) {
+	if (n > UBIFS_DFS_DIR_LEN) {
 		/* The array size is too small */
 		return;
 	}
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw

Hello,

Am Tue, May 11, 2021 at 10:12:00AM +0300 schrieb Dan Carpenter:

Quoted 37 lines

The snprintf() function returns the number of characters (not counting the NUL terminator) that it would have printed if we had space.

This buffer has UBIFS_DFS_DIR_LEN characters plus one extra for the terminator. Printing UBIFS_DFS_DIR_LEN is okay but anything higher will result in truncation. Thus the comparison needs to be change from == to >.

These strings are compile time constants so this patch doesn't affect runtime.

Fixes ae380ce04731 ("UBIFS: lessen the size of debugging info data structure")
---
 drivers/mtd/ubi/debug.c | 2 +-
 fs/ubifs/debug.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/mtd/ubi/debug.c +1 -1
index ac2bdba8bb1a..3c0c8eca4d51 100644
@@ -511,7 +511,7 @@ int ubi_debugfs_init_dev(struct ubi_device *ubi)
 
 	n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
 		     ubi->ubi_num);
-	if (n == UBI_DFS_DIR_LEN) {
+	if (n > UBI_DFS_DIR_LEN) {
 		/* The array size is too small */
 		return -EINVAL;
 	}
fs/ubifs/debug.c +0 -0
index 1bbb9fe661b1..fc718f6178f2 100644

fs/ubifs/debug.c void dbg_debugfs_init_fs(struct ubifs_info *c) · line 2830

Quoted 8 lines
fs/ubifs/debug.c +1 -1
@@ -2824,7 +2824,7 @@ void dbg_debugfs_init_fs(struct ubifs_info *c)
 
 	n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
 		     c->vi.ubi_num, c->vi.vol_id);
-	if (n == UBIFS_DFS_DIR_LEN) {
+	if (n > UBIFS_DFS_DIR_LEN) {
 		/* The array size is too small */
 		return;
 	}

Greets
Alex