All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: check non-digit character in the size value for mkfs options
@ 2012-10-16 12:04 Wang Sheng-Hui
  0 siblings, 0 replies; only message in thread
From: Wang Sheng-Hui @ 2012-10-16 12:04 UTC (permalink / raw)
  To: linux-btrfs

When we run mkfs.btrfs, we can specify the size value for leafsize, etc.
Current the limit is 65536, and the lower limit is 4096. Also, the size
should be 4096 aligned. When we specify such value, parse_size just check
the tailing non-digit character, but doesn't check other characters.

For example, run "mkfs.btrfs -l 40960b btrfs.img" will get nodesize 40960,
leafsize 40960 and sectorsize 4096, which is expected. But if we typo
4096bb, "mkfs.btrfs -l 4096bb btrfs.img", the result is nodesize 4096,
leafsize 4096 and sectorsize 4096, which maybe not what we want and what
we really want to type is 40960b.

Add check in parse_size to deal with the non-tailing non-digit characters.

Signed-off-by: Wang Sheng-Hui <shhuiw@gmail.com>
---
 mkfs.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/mkfs.c b/mkfs.c
index 2cc6051..11e64d2 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -60,6 +60,7 @@ static u64 parse_size(char *s)
 	char c;
 	u64 mult = 1;
 	u64 ret;
+	int i;

 	s = strdup(s);

@@ -80,6 +81,16 @@ static u64 parse_size(char *s)
 		}
 		s[len - 1] = '\0';
 	}
+
+	len = strlen(s);
+	for (i = 0; i < len; i++) {
+		if (!isdigit(s[i])) {
+			fprintf(stderr, "Illegal size value contains "
+				"non-digit character %c\n", s[i]);
+			exit(1);
+		}
+	}
+
 	ret = atol(s) * mult;
 	free(s);
 	return ret;
-- 
1.7.5.4

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2012-10-16 12:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-16 12:04 [PATCH] btrfs-progs: check non-digit character in the size value for mkfs options Wang Sheng-Hui

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.