From patchwork Tue Oct 23 17:51:53 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 1631701 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 3A027DF283 for ; Tue, 23 Oct 2012 17:52:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757373Ab2JWRvn (ORCPT ); Tue, 23 Oct 2012 13:51:43 -0400 Received: from mail-ee0-f46.google.com ([74.125.83.46]:64806 "EHLO mail-ee0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757353Ab2JWRvm (ORCPT ); Tue, 23 Oct 2012 13:51:42 -0400 Received: by mail-ee0-f46.google.com with SMTP id b15so1518982eek.19 for ; Tue, 23 Oct 2012 10:51:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=MKun3/8AXwFTQKacu5l0fFHu5dpBKX65WEJPSl/Varo=; b=j+W0sloulCF6vPTfWEw0/awR2r7Be2nzWhv+1BqTwihX/uIh1+EKQt30TDuXdJlNdc uNGPCbb2WEyGkvE2TglYuHwawOt1ceJQhF6yLp5z3IU/lvDF+zGfVls5XNv7YoJFFcCk PaBoW96g/G8GLSvfyWw1QDQXirwqywTPH2JG7k6wvkfjQLMm4kQ75efaQU7v1fTb4nI/ P18pBUY2jdVE1DCaPtvK3Ov28kxH7tM5RnZNu9tdE3+h+6220ws6ZAHs5Zi7yQiRiPM1 KThvegvJ0UUHRrVkErlJ5IogJ8jYK4XQQ939VRRHTVCywyVLEFWkLmV9helo+/PCmG0z mQ6w== Received: by 10.14.221.194 with SMTP id r42mr18040061eep.25.1351014701844; Tue, 23 Oct 2012 10:51:41 -0700 (PDT) Received: from venice..bhome (host103-133-static.242-95-b.business.telecomitalia.it. [95.242.133.103]) by mx.google.com with ESMTPS id o47sm21949351eem.11.2012.10.23.10.51.40 (version=SSLv3 cipher=OTHER); Tue, 23 Oct 2012 10:51:41 -0700 (PDT) From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Stefan Behrens , Wang Sheng-Hui , Goffredo Baroncelli Subject: [PATCH 3/5] parse_size(): check for invalid suffix Date: Tue, 23 Oct 2012 19:51:53 +0200 Message-Id: <1351014715-5727-4-git-send-email-kreijack@inwind.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1351014715-5727-1-git-send-email-kreijack@inwind.com> References: <1351014715-5727-1-git-send-email-kreijack@inwind.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Goffredo Baroncelli Check that the suffix for the parse_size() input is of only one character. --- utils.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/utils.c b/utils.c index 732c782..e61cdea 100644 --- a/utils.c +++ b/utils.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -1222,12 +1223,18 @@ scan_again: u64 parse_size(char *s) { - int len = strlen(s); + int i; char c; u64 mult = 1; - if (!isdigit(s[len - 1])) { - c = tolower(s[len - 1]); + for( i=0 ; s[i] && isdigit(s[i]) ; i++ ) ; + if( !i ){ + fprintf(stderr, "ERROR: size value is empty\n"); + exit(50); + } + + if ( s[i] ) { + c = tolower(s[i]); switch (c) { case 'g': mult *= 1024; @@ -1238,11 +1245,17 @@ u64 parse_size(char *s) case 'b': break; default: - fprintf(stderr, "Unknown size descriptor %c\n", c); + fprintf(stderr, "ERROR: Unknown size descriptor " + "'%c'\n", c); exit(1); } - s[len - 1] = '\0'; } - return strtoull(s, NULL, 0) * mult; + if( s[i] && s[i+1]) { + fprintf(stderr, "ERROR: Illegal suffix contains " + "character '%c' in wrong position\n", + s[i+1]); + exit(51); + } + return strtoull(s, NULL, 10) * mult; }