From patchwork Tue Aug 6 18:49:04 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zach Brown X-Patchwork-Id: 2839543 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id A81859F479 for ; Tue, 6 Aug 2013 18:49:15 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 919272024D for ; Tue, 6 Aug 2013 18:49:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3AE8D2024A for ; Tue, 6 Aug 2013 18:49:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755763Ab3HFStJ (ORCPT ); Tue, 6 Aug 2013 14:49:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:14264 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755695Ab3HFStH (ORCPT ); Tue, 6 Aug 2013 14:49:07 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r76In4Iu020711 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 6 Aug 2013 14:49:04 -0400 Received: from localhost (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r76In4uQ005480 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Tue, 6 Aug 2013 14:49:04 -0400 Date: Tue, 6 Aug 2013 11:49:04 -0700 From: Zach Brown To: Miao Xie Cc: Eric Sandeen , linux-btrfs Subject: Re: [PATCH] btrfs-progs: don't overrun "answer" array in cmds-chunk.c Message-ID: <20130806184904.GN12314@lenny.home.zabbo.net> References: <52006509.1060207@redhat.com> <20130806035757.GL12314@lenny.home.zabbo.net> <5200905A.4080607@cn.fujitsu.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <5200905A.4080607@cn.fujitsu.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP > > If you're in here, want to reimplement this thing in a few lines of > > scanf(%s) and strcasecmp()? I can give it a go if you don't want to. > > I think it is better that moving it to utils.c because the other commands > may use it in the future. I disagree. Let's stick to only writing the code that we need. Implementing and testing code that meets future needs that we make up isn't a good use of our time. If someone has to tweak this in the future, so be it. They'll actually be in a position to implement and test their needs. Anyway, here's how I'd do a trivial y/n prompt. - z From 2c46c2b81061f1c55de07a80d9d177a7df7b33cb Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Tue, 6 Aug 2013 11:30:21 -0700 Subject: [PATCH] btrfs-progs: simplify ask_user() Eric noticed the trivial stack overflow bug in ask_user(). I went to see the context for that fix and found that ask_user() was a bit much. This fixes the overflow bug that Eric found, endless spinning on scanf() errors, removes dead code, and leaves us with a trivial helper. Signed-off-by: Zach Brown --- cmds-chunk.c | 65 +++++++++++++----------------------------------------------- 1 file changed, 14 insertions(+), 51 deletions(-) diff --git a/cmds-chunk.c b/cmds-chunk.c index 03314de..35a5c69 100644 --- a/cmds-chunk.c +++ b/cmds-chunk.c @@ -1307,58 +1307,22 @@ fail_close_fd: return ret; } -static int ask_user(char *question, int defval) +/* + * This reads a line from the stdin and only returns non-zero if the + * first whitespace delimited token is a case insensitive match with yes + * or y. + */ +static int ask_user(char *question) { - char answer[5]; - char *defstr; - int i; - - if (defval == 1) - defstr = "[Y/n]"; - else if (defval == 0) - defstr = "[y/N]"; - else if (defval == -1) - defstr = "[y/n]"; - else - BUG_ON(1); -again: - printf("%s%s? ", question, defstr); - - i = 0; - while (i < 4 && scanf("%c", &answer[i])) { - if (answer[i] == '\n') { - answer[i] = '\0'; - break; - } else if (answer[i] == ' '){ - answer[i] = '\0'; - if (i == 0) - continue; - else - break; - } else if (answer[i] >= 'A' && answer[i] <= 'Z') { - answer[i] += 'a' - 'A'; - } - i++; - } - answer[5] = '\0'; - __fpurge(stdin); - - if (strlen(answer) == 0) { - if (defval != -1) - return defval; - else - goto again; - } + char buf[30] = {0,}; + char *saveptr = NULL; + char *answer; - if (!strcmp(answer, "yes") || - !strcmp(answer, "y")) - return 1; - - if (!strcmp(answer, "no") || - !strcmp(answer, "n")) - return 0; + printf("%s [y/N]: ", question); - goto again; + return fgets(buf, sizeof(buf) - 1, stdin) && + (answer = strtok_r(buf, " \t\n\r", &saveptr)) && + (!strcasecmp(answer, "yes") || !strcasecmp(answer, "y")); } static int btrfs_get_device_extents(u64 chunk_object, @@ -1752,8 +1716,7 @@ static int btrfs_recover_chunk_tree(char *path, int verbose, int yes) } if (!rc.yes) { - ret = ask_user("We are going to rebuild the chunk tree on disk, it might destroy the old metadata on the disk, Are you sure", - 0); + ret = ask_user("We are going to rebuild the chunk tree on disk, it might destroy the old metadata on the disk, Are you sure?"); if (!ret) { ret = BTRFS_CHUNK_TREE_REBUILD_ABORTED; goto fail_close_ctree;