From patchwork Wed May 29 09:11:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Duy Nguyen X-Patchwork-Id: 10966563 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 557F8112C for ; Wed, 29 May 2019 09:11:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 44AE8287F5 for ; Wed, 29 May 2019 09:11:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 34895288E0; Wed, 29 May 2019 09:11:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C068D287F5 for ; Wed, 29 May 2019 09:11:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726087AbfE2JLe (ORCPT ); Wed, 29 May 2019 05:11:34 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:46174 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725911AbfE2JLe (ORCPT ); Wed, 29 May 2019 05:11:34 -0400 Received: by mail-pg1-f194.google.com with SMTP id v9so979299pgr.13 for ; Wed, 29 May 2019 02:11:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TuqttM0T8MfV2wTbbPFhnQTuyohnpencg4SHXzoWl5o=; b=L33o1LOpquT4QGhx2bdgJevFsDdfjwFfeux3ao+YUOUpXeHk4x2U8mTi/R4dY1XCAl QhxOK2r79pWW8CgYb1WgEt0iQ4NbqaK9fBYmHxxn8NiJ8+sxXaX9GCzj+eb5laddWk/Z RxeIvWU7E382AtQACaF+6uxaLs4jqFe9uO7NA9P1W7uiVy+vx0IKlaiOV6k9kPgKO9XM /VyyinpRxybq9bvGce/qObIlozzzfnJCjD3vCObOn0Nyrt8pBScgrtRnA0gkJZFHoLe9 YhEy9MlPuMT0U5UB5H4ncdBS6N7AZ/MQRscrMtqhq/rfYnSmLidKrJ4Uf+NLajFI3hNQ P1yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TuqttM0T8MfV2wTbbPFhnQTuyohnpencg4SHXzoWl5o=; b=CqqwCgA3JNSVgKrlG10/buTE2hDRx1lhaSH8UZ+2EDfS9qrZg2/Y7PWsSfyThIRcIs j/mWeQEWgxz1OdSDwMKhJRl1265/0mSUNVVtgPNYwrblimAGRgL4wMrWL5FiAc9SA9WK 1oeVj/oym8Y4/YzKCpk+2Vzb1fF+GuyEbOp1QB+DbZvvg2odKyJCtexUxmn3hNfwZ5+A 7CVKErUoANkqt2XgYVd1bMvuU5SSmIHlWJCq+NdGGviEVYTK25yOJYXg1npC/UTtWPt1 QG30pZ4e96h+LK4T7D9k5s5qavlI4DFb/epgzKK9A8xeI3k41idVn/mMY203DSbm97va S0WQ== X-Gm-Message-State: APjAAAU3NdnKDD1dUbC7582Nx7dF7+1ieF/EXYIt06fMnkU0e+FW6r3Q e8880Me8V/d4haJ6nlHVJu8= X-Google-Smtp-Source: APXvYqz1hA7GsDW4NEGo1n5RfBbel/CVelIvHV+lRikfBywjoDSKPhNcN6h9sYN+bLXwOc4EUv6D/g== X-Received: by 2002:a63:a08:: with SMTP id 8mr136805144pgk.46.1559121093738; Wed, 29 May 2019 02:11:33 -0700 (PDT) Received: from ash ([115.72.28.243]) by smtp.gmail.com with ESMTPSA id t15sm6099494pjb.6.2019.05.29.02.11.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 May 2019 02:11:33 -0700 (PDT) Received: by ash (sSMTP sendmail emulation); Wed, 29 May 2019 16:11:28 +0700 From: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= To: pclouds@gmail.com Cc: avarab@gmail.com, bturner@atlassian.com, git@vger.kernel.org, gitster@pobox.com, tmz@pobox.com Subject: [PATCH v2 1/3] diff-parseopt: correct variable types that are used by parseopt Date: Wed, 29 May 2019 16:11:14 +0700 Message-Id: <20190529091116.21898-2-pclouds@gmail.com> X-Mailer: git-send-email 2.22.0.rc0.322.g2b0371e29a In-Reply-To: <20190529091116.21898-1-pclouds@gmail.com> References: <20190524092442.701-1-pclouds@gmail.com> <20190529091116.21898-1-pclouds@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Most number-related OPT_ macros store the value in an 'int' variable. Many of the variables in 'struct diff_options' have a different type, but during the conversion to using parse_options() I failed to notice and correct. The problem was reported on s360x which is a big-endian architechture. The variable to store '-w' option in this case is xdl_opts, 'long' type, 8 bytes. But since parse_options() assumes 'int' (4 bytes), it will store bits in the wrong part of xdl_opts. The problem was found on little-endian platforms because parse_options() will accidentally store at the right part of xdl_opts. There aren't much to say about the type change (except that 'int' for xdl_opts should still be big enough, since Windows' long is the same size as 'int' and nobody has complained so far). Some safety checks may be implemented in the future to prevent class of bugs. Reported-by: Todd Zullinger Signed-off-by: Nguyễn Thái Ngọc Duy --- diff.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff.h b/diff.h index b20cbcc091..d5e44baa96 100644 --- a/diff.h +++ b/diff.h @@ -169,7 +169,7 @@ struct diff_options { const char *prefix; int prefix_length; const char *stat_sep; - long xdl_opts; + int xdl_opts; /* see Documentation/diff-options.txt */ char **anchors; From patchwork Wed May 29 09:11:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Duy Nguyen X-Patchwork-Id: 10966565 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4F3FF112C for ; Wed, 29 May 2019 09:11:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 422B028511 for ; Wed, 29 May 2019 09:11:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 36A6D28841; Wed, 29 May 2019 09:11:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B167E28511 for ; Wed, 29 May 2019 09:11:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726173AbfE2JLk (ORCPT ); Wed, 29 May 2019 05:11:40 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:44882 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725911AbfE2JLj (ORCPT ); Wed, 29 May 2019 05:11:39 -0400 Received: by mail-pl1-f194.google.com with SMTP id c5so785985pll.11 for ; Wed, 29 May 2019 02:11:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZkU0Sy3kGo2Tpxjx47UMjQZq7d/ezG/8c+Qx3KTnaGM=; b=kND/JYiR45BGiPxvq3UZBF8W4sdKFoCSJxjolZX/wBkerpXieDoT+14V6ROozBFXW5 P9uluWqWc3Mic/pyhhf+O+f4I4Y0V7ImTZOkY8IhVDvtMz76Uvdj3xyvDPoyWa/WJEMy foxxFXl+rrULvsRSvt3wSu724XMtT4Pb7tqZsx94VBBRYvKrpv85Oqv2Yvo3aqHw/1Go I5bYJ1ZLrMdM2Zh8XcEb4gvpjnTNVNr9oxN8PRU6T3H0g0jwvENV0Z6A1JbC61rJ8eHV upSdekVfJY4ad0zN8rIhydgHFk+spjyubARvAz3q42JHoePWtGwacMEKQnA8SzCWfFCp cAog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZkU0Sy3kGo2Tpxjx47UMjQZq7d/ezG/8c+Qx3KTnaGM=; b=ZL1Iedk8J+i+vzPhFILftUZuXvqqoMHMH7vrgh8gMkgDbW44XQ5d4iNhdRLXH2csh3 NLXQr0bWLGRgG2mBC5K8SIeh4HFj+R3RKwwRVCe9UmBK5DSsXwqPPM1jusiRUL0wupOp +APNTYatWYhuGJ44KXf9A8/oEKFCJ7hq3jVexH5LepZoQKkH8XyYudLDLVglV/xERS3z H82hAp5PdBxy065jHTkeVFlgJlcMouEwAxAs2tX94cQ3hYIS/iUaUGeu/v0c86mF03r/ jZqmCRPl7muzyHzvXhVV2qoNTUkB12ZMDkSBiUqw4ulOnIijcsOBQun7igk8E+Lx0ezI OM5g== X-Gm-Message-State: APjAAAWMRiIxWAuZTh0OHN4EQXuTMqGMC+4DiQMF6AwDZRTV1KATigsn iCoychLNhWHB30ByzDmsKV8= X-Google-Smtp-Source: APXvYqyPmmjIOm0eHzwc2VPt6CRx8c/ZzD7LbeLJzTt1xwD0f7DPVTSuD7nJn8/B8AmyqSzBZwqJbg== X-Received: by 2002:a17:902:9697:: with SMTP id n23mr35204811plp.283.1559121098789; Wed, 29 May 2019 02:11:38 -0700 (PDT) Received: from ash ([115.72.28.243]) by smtp.gmail.com with ESMTPSA id e14sm16793838pff.60.2019.05.29.02.11.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 May 2019 02:11:38 -0700 (PDT) Received: by ash (sSMTP sendmail emulation); Wed, 29 May 2019 16:11:34 +0700 From: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= To: pclouds@gmail.com Cc: avarab@gmail.com, bturner@atlassian.com, git@vger.kernel.org, gitster@pobox.com, tmz@pobox.com Subject: [PATCH v2 2/3] diff-parseopt: restore -U (no argument) behavior Date: Wed, 29 May 2019 16:11:15 +0700 Message-Id: <20190529091116.21898-3-pclouds@gmail.com> X-Mailer: git-send-email 2.22.0.rc0.322.g2b0371e29a In-Reply-To: <20190529091116.21898-1-pclouds@gmail.com> References: <20190524092442.701-1-pclouds@gmail.com> <20190529091116.21898-1-pclouds@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Before d473e2e0e8 (diff.c: convert -U|--unified, 2019-01-27), -U and --unified are implemented with a custom parser opt_arg() in diff.c. I didn't check this code carefully and not realize that it's the equivalent of PARSE_OPT_NONEG | PARSE_OPT_OPTARG. In other words, if -U is specified without any argument, the option should be accepted, and the default value should be used. Without PARSE_OPT_OPTARG, parse_options() will reject this case and cause a regression. Reported-by: Bryan Turner Signed-off-by: Nguyễn Thái Ngọc Duy --- diff.c | 10 ++++--- t/t4013-diff-various.sh | 2 ++ t/t4013/diff.diff_-U1_initial..side (new) | 29 ++++++++++++++++++++ t/t4013/diff.diff_-U2_initial..side (new) | 31 ++++++++++++++++++++++ t/t4013/diff.diff_-U_initial..side (new) | 32 +++++++++++++++++++++++ 5 files changed, 100 insertions(+), 4 deletions(-) diff --git a/diff.c b/diff.c index 4d3cf83a27..80ddc11671 100644 --- a/diff.c +++ b/diff.c @@ -5211,9 +5211,11 @@ static int diff_opt_unified(const struct option *opt, BUG_ON_OPT_NEG(unset); - options->context = strtol(arg, &s, 10); - if (*s) - return error(_("%s expects a numerical value"), "--unified"); + if (arg) { + options->context = strtol(arg, &s, 10); + if (*s) + return error(_("%s expects a numerical value"), "--unified"); + } enable_patch_output(&options->output_format); return 0; @@ -5272,7 +5274,7 @@ static void prep_parse_options(struct diff_options *options) DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT), OPT_CALLBACK_F('U', "unified", options, N_(""), N_("generate diffs with lines context"), - PARSE_OPT_NONEG, diff_opt_unified), + PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified), OPT_BOOL('W', "function-context", &options->flags.funccontext, N_("generate diffs with lines context")), OPT_BIT_F(0, "raw", &options->output_format, diff --git a/t/t4013-diff-various.sh b/t/t4013-diff-various.sh index 9f8f0e84ad..a9054d2db1 100755 --- a/t/t4013-diff-various.sh +++ b/t/t4013-diff-various.sh @@ -338,6 +338,8 @@ format-patch --inline --stdout initial..master^^ format-patch --stdout --cover-letter -n initial..master^ diff --abbrev initial..side +diff -U initial..side +diff -U1 initial..side diff -r initial..side diff --stat initial..side diff -r --stat initial..side diff --git a/t/t4013/diff.diff_-U1_initial..side b/t/t4013/diff.diff_-U1_initial..side new file mode 100644 index 0000000000..b69f8f048a --- /dev/null +++ b/t/t4013/diff.diff_-U1_initial..side @@ -0,0 +1,29 @@ +$ git diff -U1 initial..side +diff --git a/dir/sub b/dir/sub +index 35d242b..7289e35 100644 +--- a/dir/sub ++++ b/dir/sub +@@ -2 +2,3 @@ A + B ++1 ++2 +diff --git a/file0 b/file0 +index 01e79c3..f4615da 100644 +--- a/file0 ++++ b/file0 +@@ -3 +3,4 @@ + 3 ++A ++B ++C +diff --git a/file3 b/file3 +new file mode 100644 +index 0000000..7289e35 +--- /dev/null ++++ b/file3 +@@ -0,0 +1,4 @@ ++A ++B ++1 ++2 +$ diff --git a/t/t4013/diff.diff_-U2_initial..side b/t/t4013/diff.diff_-U2_initial..side new file mode 100644 index 0000000000..8ffe04f203 --- /dev/null +++ b/t/t4013/diff.diff_-U2_initial..side @@ -0,0 +1,31 @@ +$ git diff -U2 initial..side +diff --git a/dir/sub b/dir/sub +index 35d242b..7289e35 100644 +--- a/dir/sub ++++ b/dir/sub +@@ -1,2 +1,4 @@ + A + B ++1 ++2 +diff --git a/file0 b/file0 +index 01e79c3..f4615da 100644 +--- a/file0 ++++ b/file0 +@@ -2,2 +2,5 @@ + 2 + 3 ++A ++B ++C +diff --git a/file3 b/file3 +new file mode 100644 +index 0000000..7289e35 +--- /dev/null ++++ b/file3 +@@ -0,0 +1,4 @@ ++A ++B ++1 ++2 +$ diff --git a/t/t4013/diff.diff_-U_initial..side b/t/t4013/diff.diff_-U_initial..side new file mode 100644 index 0000000000..c66c0dd5c6 --- /dev/null +++ b/t/t4013/diff.diff_-U_initial..side @@ -0,0 +1,32 @@ +$ git diff -U initial..side +diff --git a/dir/sub b/dir/sub +index 35d242b..7289e35 100644 +--- a/dir/sub ++++ b/dir/sub +@@ -1,2 +1,4 @@ + A + B ++1 ++2 +diff --git a/file0 b/file0 +index 01e79c3..f4615da 100644 +--- a/file0 ++++ b/file0 +@@ -1,3 +1,6 @@ + 1 + 2 + 3 ++A ++B ++C +diff --git a/file3 b/file3 +new file mode 100644 +index 0000000..7289e35 +--- /dev/null ++++ b/file3 +@@ -0,0 +1,4 @@ ++A ++B ++1 ++2 +$ From patchwork Wed May 29 09:11:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Duy Nguyen X-Patchwork-Id: 10966567 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DD12C15E6 for ; Wed, 29 May 2019 09:11:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D0A5428841 for ; Wed, 29 May 2019 09:11:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C502520881; Wed, 29 May 2019 09:11:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6905F28841 for ; Wed, 29 May 2019 09:11:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726311AbfE2JLo (ORCPT ); Wed, 29 May 2019 05:11:44 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:43620 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725911AbfE2JLo (ORCPT ); Wed, 29 May 2019 05:11:44 -0400 Received: by mail-pl1-f196.google.com with SMTP id gn7so788799plb.10 for ; Wed, 29 May 2019 02:11:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=C7hX91qz3vqJ6SvsPoUxKHsJMUdb/xZv58Ut8nEe1vU=; b=Z+GB+nF4WYxRmr9IZoaszFvuZ/0257ruAly+p9ngB15SY7bqYtxVKBKjcvBhCnynrM MrU3VFQMVydQj4PAkNXFWNv09sQe7uYgGRlF+fF4XQSrU6hS63kUDSVeKbw0sJCpd+P7 b8RzXkR6dyJHdR4wR9sprEw2Jd/9fbyhhbi4CbPMjGyjbJLjUMLNAENh9Qkezg2kRvQ+ /aLRSH3jmRi08u18ZFDYKnlddexiXcbrjmnucZzpXK+LZzCYeRCwXRGx3LHWNpCFH78t 9xJMTYd9JItbU0jTV6F/0kHWPv30R0BRfAtHWVf5ED0UYA4uX3MAj2GlnyJ3Og38gb6T zPEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=C7hX91qz3vqJ6SvsPoUxKHsJMUdb/xZv58Ut8nEe1vU=; b=VgH6Zw9euTgSdny3PgTDYwO+jUo4poR+tcxR1rT850jFDe94T1hflZ2kE1ChoZzl5i b1HoO86q31ODHqsCPwMp738Gac2+xGqk733DRoeyY2LvVCO8ladXtUIIETDUYuNWVWZU wJYwxr1O0L92WX3NbAfdaTNUlsNc1k7AHhK8ftsGoHH9i7CxyYjYFxJYhywv1iWLgJ9Y MDes5StVns8rRJ27vQGjCBKnOTLj+Y/c9Pvmks7yjT8Xknq/nvehUjSwxfq3Wa+lnxNS njtBCZmPWOYdjF950hMvN8yOuYoHSLcPgNmig+JbwK/CoUXfkSmbOTKrrPywCz5/Oi2t WgZA== X-Gm-Message-State: APjAAAUD6CAevrr1MrNBoPn/tZS3R+/138pLcbh+UPEeAUnftZ8NCiNI ph3Y9u1Rkwa01cBIJjAXkno= X-Google-Smtp-Source: APXvYqw/tzRzVelBA8IM3zCvv/jT+PV2WEQoFD/z7+VfPSV14LG/mL4RAbgr0/2HFa2qgfjywKxU2A== X-Received: by 2002:a17:902:8d96:: with SMTP id v22mr65051145plo.282.1559121103691; Wed, 29 May 2019 02:11:43 -0700 (PDT) Received: from ash ([115.72.28.243]) by smtp.gmail.com with ESMTPSA id e16sm22497632pfj.77.2019.05.29.02.11.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 May 2019 02:11:43 -0700 (PDT) Received: by ash (sSMTP sendmail emulation); Wed, 29 May 2019 16:11:39 +0700 From: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= To: pclouds@gmail.com Cc: avarab@gmail.com, bturner@atlassian.com, git@vger.kernel.org, gitster@pobox.com, tmz@pobox.com Subject: [PATCH v2 3/3] parse-options: check empty value in OPT_INTEGER and OPT_ABBREV Date: Wed, 29 May 2019 16:11:16 +0700 Message-Id: <20190529091116.21898-4-pclouds@gmail.com> X-Mailer: git-send-email 2.22.0.rc0.322.g2b0371e29a In-Reply-To: <20190529091116.21898-1-pclouds@gmail.com> References: <20190524092442.701-1-pclouds@gmail.com> <20190529091116.21898-1-pclouds@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When parsing the argument for OPT_INTEGER and OPT_ABBREV, we check if we can parse the entire argument to a number with "if (*s)". There is one missing check: if "arg" is empty to begin with, we fail to notice. This could happen with long option by writing like git diff --inter-hunk-context= blah blah Before 16ed6c97cc (diff-parseopt: convert --inter-hunk-context, 2019-03-24), --inter-hunk-context is handled by a custom parser opt_arg() and does detect this correctly. This restores the bahvior for --inter-hunk-context and make sure all other integer options are handled the same (sane) way. For OPT_ABBREV this is new behavior. But it makes it consistent with the rest. PS. OPT_MAGNITUDE has similar code but git_parse_ulong() does detect empty "arg". So it's good to go. Signed-off-by: Nguyễn Thái Ngọc Duy --- parse-options-cb.c | 3 +++ parse-options.c | 3 +++ 2 files changed, 6 insertions(+) diff --git a/parse-options-cb.c b/parse-options-cb.c index 4b95d04a37..a3de795c58 100644 --- a/parse-options-cb.c +++ b/parse-options-cb.c @@ -16,6 +16,9 @@ int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset) if (!arg) { v = unset ? 0 : DEFAULT_ABBREV; } else { + if (!*arg) + return error(_("option `%s' expects a numerical value"), + opt->long_name); v = strtol(arg, (char **)&arg, 10); if (*arg) return error(_("option `%s' expects a numerical value"), diff --git a/parse-options.c b/parse-options.c index 987e27cb91..87b26a1d92 100644 --- a/parse-options.c +++ b/parse-options.c @@ -195,6 +195,9 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p, } if (get_arg(p, opt, flags, &arg)) return -1; + if (!*arg) + return error(_("%s expects a numerical value"), + optname(opt, flags)); *(int *)opt->value = strtol(arg, (char **)&s, 10); if (*s) return error(_("%s expects a numerical value"),