From patchwork Tue Oct 16 12:04:31 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Sheng-Hui X-Patchwork-Id: 1600501 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id B18F940ABA for ; Tue, 16 Oct 2012 12:04:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751987Ab2JPMEl (ORCPT ); Tue, 16 Oct 2012 08:04:41 -0400 Received: from mail-pa0-f46.google.com ([209.85.220.46]:37948 "EHLO mail-pa0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751094Ab2JPMEk (ORCPT ); Tue, 16 Oct 2012 08:04:40 -0400 Received: by mail-pa0-f46.google.com with SMTP id hz1so5846278pad.19 for ; Tue, 16 Oct 2012 05:04:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=UK3ZPhA4cOkPGBiz/ZhiVfhxTfjRQbywwOhrrMe7GG4=; b=B1uUvZEuckhASD1fUhZo8T4xs6tZsqYUZ/hh1nquduUHiErnIkTmliTvhiD/pP5gSQ x66FKdSc9wIQRBpiqFJvRetlVpLzle3zMXCAuRD/cf9QsjZ6Dy3HTSyuMI8uW4d9xVMk QEZczc4u0YK4pHEuogQt7vZTUVM655/dxtkvwb4DvPvn76aCXWIo40t5M2EqEepjhMnp 4Sg5wM/R+0SCCVK+yE1LsJMiM6WketnbWucsT1AfXfePCgSAFZss05jA/NKo44VHhmtn TeDoIv0BEf2blRE7ZltO0t67oGDpfGdcgoItdg/JpIoyA93x8m/4ry+FX2VGkI74Tj+1 Y3GQ== Received: by 10.68.138.229 with SMTP id qt5mr45423766pbb.122.1350389080348; Tue, 16 Oct 2012 05:04:40 -0700 (PDT) Received: from crossover.org ([122.70.30.150]) by mx.google.com with ESMTPS id qb2sm10700181pbb.15.2012.10.16.05.04.37 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 16 Oct 2012 05:04:39 -0700 (PDT) Message-ID: <507D4D4F.10807@gmail.com> Date: Tue, 16 Oct 2012 20:04:31 +0800 From: Wang Sheng-Hui User-Agent: Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 MIME-Version: 1.0 To: linux-btrfs@vger.kernel.org Subject: [PATCH] btrfs-progs: check non-digit character in the size value for mkfs options Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org 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 --- 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;