linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Wang Sheng-Hui <shhuiw@gmail.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH] btrfs-progs: check non-digit character in the size value for mkfs options
Date: Tue, 16 Oct 2012 20:04:31 +0800	[thread overview]
Message-ID: <507D4D4F.10807@gmail.com> (raw)

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

                 reply	other threads:[~2012-10-16 12:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=507D4D4F.10807@gmail.com \
    --to=shhuiw@gmail.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).