From patchwork Sat May 21 17:14:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12857981 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D95AEC433EF for ; Sat, 21 May 2022 17:14:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245074AbiEURO6 (ORCPT ); Sat, 21 May 2022 13:14:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232331AbiEURO4 (ORCPT ); Sat, 21 May 2022 13:14:56 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60094666A9 for ; Sat, 21 May 2022 10:14:54 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id m20so4134032wrb.13 for ; Sat, 21 May 2022 10:14:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hudp7bTUxP85RGVSSWbuHZUCZqVFSw30OWSEBcmw4DY=; b=jgrhz3xEjIitaB6M8Q89Klo5RkD6HeLc0fZDUDG9ruGp1vMMSqsp2EDl1JZL/S0u8k p32HZQaVGvUu/uWi+MkW4riTQ69V4SIa80REEIzJbERm1K1lBjXdddqKHYYwYeZdxnzB hCdJjJvKRy+UR+UpM0j5D5KwDjaRTjxwiB2pPFq0mCffJD4J5kT8GLBE2nruA8G7Z6Dj +iugvkVP6tQ6y1uR/9q3a/yukCmO4NVFSSH5Qgj3wrb68GsQmjapzzGmYJF+buIVboIN tmXz7Et4w6M7lRltclf3aTHD0J44L+OMKaLDeFo+szsW84OObaKyC3LTgdFmxNZhol4g oCJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hudp7bTUxP85RGVSSWbuHZUCZqVFSw30OWSEBcmw4DY=; b=iYVE9pEsciaKrfm0izP+mh2i4UUC7BG3v5PLKXz37Re3XWm/lE9SsV8HeYscs7dlW9 N+3MBxqIABUmVKw+8Gp1Fnb1gjH5Fzk1bnMfBKud6HSjDtEKx3qstfnvAKumE+JbKcxf RVyYPbNRBEkMEZB6ILNdmHvjRnadNTspGoNjdRZ7CeLScdqPg+UaZgD6oAjiSQFgkao0 iKGOGqGi7082YKOuP2SwKIrunbjdmkEWQDoWN0JxaYv9cgoCzuTibkpOklJyDk1BAzbx s4LNfzZwjX61c2Obn5ycdMFZMn66y1UNg+RU0Rz06efIMFPln4dwsu0Fnc/ACSSkWlgK 2eeA== X-Gm-Message-State: AOAM531WPhUAmhaNZTnrjzADnI9KWzepAiN05kpgPPdRQeDgWdJEIhnd fQxcMBwv8HJ7YUyFJYsH/basSdJ4lIkrrg== X-Google-Smtp-Source: ABdhPJx1zt7hXnX1U7dTbU1crALL6XYyyIsIjAmrQ2XXGlGLk0pJ3IEs3MgO4iQUvVNdND9Uoc6M5Q== X-Received: by 2002:a5d:588a:0:b0:20c:5ddf:2491 with SMTP id n10-20020a5d588a000000b0020c5ddf2491mr12540970wrf.197.1653153292477; Sat, 21 May 2022 10:14:52 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id b15-20020a7bc24f000000b0039736892653sm4964685wmj.27.2022.05.21.10.14.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 May 2022 10:14:51 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Bagas Sanjaya , Abhradeep Chakraborty , =?utf-8?b?w4Z2?= =?utf-8?b?YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 1/5] usage.c: add a non-fatal bug() function to go with BUG() Date: Sat, 21 May 2022 19:14:38 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.960.g7a4e2fc85c9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a bug() function to use in cases where we'd like to indicate a runtime BUG(), but would like to deref the BUG() call because we're possibly accumulating more bug() callers to exhaustively indicate what went wrong. We already have this sort of facility in various parts of the codebase, just in the form of ad-hoc re-inventions of the functionality that this new API provides. E.g. this will be used to replace optbug() in parse-options.c, and the 'error("BUG:[...]' we do in a loop in builtin/receive-pack.c. Unlike the code this replaces we'll log to trace2 with this new bug() function (as with other usage.c functions, including BUG()), we'll also be able to avoid calls to xstrfmt() in some cases, as the bug() function itself accepts variadic sprintf()-like arguments. Any caller to bug() should follow up such calls with BUG_if_bug(), which will BUG() out (i.e. abort()) if there were any preceding calls to bug(). As the tests and documentation here show we'll catch missing BUG_if_bug() invocations in our exit() wrapper. I'd previously proposed this as part of another series[1], in that use-case we ended thinking a BUG() would be better (and eventually 96e41f58fe1 (fsck: report invalid object type-path combinations, 2021-10-01) ended up with neither). Here though we'll end up converting various existing code that was already doing what we're doing better with this new API. 1. https://lore.kernel.org/git/YGRXomWwRYPdXZi3@coredump.intra.peff.net/ Signed-off-by: Ævar Arnfjörð Bjarmason --- .../technical/api-error-handling.txt | 17 +++++- Documentation/technical/api-trace2.txt | 4 +- git-compat-util.h | 12 +++++ t/helper/test-trace2.c | 21 +++++++- t/t0210-trace2-normal.sh | 52 +++++++++++++++++++ trace2.c | 6 +++ usage.c | 30 +++++++++-- 7 files changed, 132 insertions(+), 10 deletions(-) diff --git a/Documentation/technical/api-error-handling.txt b/Documentation/technical/api-error-handling.txt index 8be4f4d0d6a..f4dffea4af0 100644 --- a/Documentation/technical/api-error-handling.txt +++ b/Documentation/technical/api-error-handling.txt @@ -1,12 +1,27 @@ Error reporting in git ====================== -`BUG`, `die`, `usage`, `error`, and `warning` report errors of +`BUG`, `bug`, `die`, `usage`, `error`, and `warning` report errors of various kinds. - `BUG` is for failed internal assertions that should never happen, i.e. a bug in git itself. +- `bug` (lower-case, not `BUG`) is supposed to be used like `BUG` but + prints a "BUG" message instead of calling `abort()`. We then expect + `BUG_if_bug()` to be called to `abort()` if there were any calls to + `bug()`. If there weren't any a call to `BUG_if_bug()` is a NOOP. ++ +This is for the convenience of APIs who'd like to potentially report +more than one "bug", such as the optbug() validation in +parse-options.c. ++ +We call `BUG_if_bug()` ourselves at `exit()` time (via a wrapper, not +`atexit()`), which guarantees that we'll catch cases where we forgot +to invoke `BUG_if_bug()` after calls to `bug()`. Thus calling +`BUG_if_bug()` isn't strictly necessary, but ensures that we die as +soon as possible. + - `die` is for fatal application errors. It prints a message to the user and exits with status 128. diff --git a/Documentation/technical/api-trace2.txt b/Documentation/technical/api-trace2.txt index f4a8a690878..77a150b30ee 100644 --- a/Documentation/technical/api-trace2.txt +++ b/Documentation/technical/api-trace2.txt @@ -465,8 +465,8 @@ completed.) ------------ `"error"`:: - This event is emitted when one of the `BUG()`, `error()`, `die()`, - `warning()`, or `usage()` functions are called. + This event is emitted when one of the `BUG()`, `bug()`, `error()`, + `die()`, `warning()`, or `usage()` functions are called. + ------------ { diff --git a/git-compat-util.h b/git-compat-util.h index 58fd813bd01..23b36053af4 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1269,9 +1269,21 @@ static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size, /* usage.c: only to be used for testing BUG() implementation (see test-tool) */ extern int BUG_exit_code; +/* usage.c: if bug() is called we should have a BUG_if_bug() afterwards */ +extern int bug_called_must_BUG; + __attribute__((format (printf, 3, 4))) NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...); #define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__) +__attribute__((format (printf, 3, 4))) +void bug_fl(const char *file, int line, const char *fmt, ...); +#define bug(...) bug_fl(__FILE__, __LINE__, __VA_ARGS__) +#define BUG_if_bug() do { \ + if (bug_called_must_BUG) { \ + bug_called_must_BUG = 0; \ + BUG_fl(__FILE__, __LINE__, "see bug() output above"); \ + } \ +} while (0) #ifdef __APPLE__ #define FSYNC_METHOD_DEFAULT FSYNC_METHOD_WRITEOUT_ONLY diff --git a/t/helper/test-trace2.c b/t/helper/test-trace2.c index 59b124bb5f1..9d2ad744840 100644 --- a/t/helper/test-trace2.c +++ b/t/helper/test-trace2.c @@ -198,7 +198,7 @@ static int ut_006data(int argc, const char **argv) return 0; } -static int ut_007bug(int argc, const char **argv) +static int ut_007BUG(int argc, const char **argv) { /* * Exercise BUG() to ensure that the message is printed to trace2. @@ -206,6 +206,21 @@ static int ut_007bug(int argc, const char **argv) BUG("the bug message"); } +static int ut_008bug(int argc, const char **argv) +{ + bug("a bug message"); + bug("another bug message"); + BUG_if_bug(); + return 0; +} + +static int ut_009bug_BUG(int argc, const char **argv) +{ + bug("a bug message"); + bug("another bug message"); + return 0; +} + /* * Usage: * test-tool trace2 @@ -222,7 +237,9 @@ static struct unit_test ut_table[] = { { ut_004child, "004child", "[]" }, { ut_005exec, "005exec", "" }, { ut_006data, "006data", "[ ]+" }, - { ut_007bug, "007bug", "" }, + { ut_007BUG, "007bug", "" }, + { ut_008bug, "008bug", "" }, + { ut_009bug_BUG, "009bug_BUG","" }, }; /* clang-format on */ diff --git a/t/t0210-trace2-normal.sh b/t/t0210-trace2-normal.sh index 37c359bd5a2..7c0e0017ad3 100755 --- a/t/t0210-trace2-normal.sh +++ b/t/t0210-trace2-normal.sh @@ -168,6 +168,58 @@ test_expect_success 'BUG messages are written to trace2' ' test_cmp expect actual ' +test_expect_success 'bug messages with BUG_if_bug() are written to trace2' ' + test_when_finished "rm trace.normal actual expect" && + test_expect_code 99 env GIT_TRACE2="$(pwd)/trace.normal" \ + test-tool trace2 008bug 2>err && + cat >expect <<-\EOF && + a bug message + another bug message + see bug() output above + EOF + sed "s/^.*: //" actual && + test_cmp expect actual && + + perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" actual && + cat >expect <<-EOF && + version $V + start _EXE_ trace2 008bug + cmd_name trace2 (trace2) + error a bug message + error another bug message + error see bug() output above + exit elapsed:_TIME_ code:99 + atexit elapsed:_TIME_ code:99 + EOF + test_cmp expect actual +' + +test_expect_success 'bug messages without BUG_if_bug() are written to trace2' ' + test_when_finished "rm trace.normal actual expect" && + test_expect_code 99 env GIT_TRACE2="$(pwd)/trace.normal" \ + test-tool trace2 009bug_BUG 2>err && + cat >expect <<-\EOF && + a bug message + another bug message + had bug() output above, in addition missed BUG_if_bug() call + EOF + sed "s/^.*: //" actual && + test_cmp expect actual && + + perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" actual && + cat >expect <<-EOF && + version $V + start _EXE_ trace2 009bug_BUG + cmd_name trace2 (trace2) + error a bug message + error another bug message + error had bug() output above, in addition missed BUG_if_bug() call + exit elapsed:_TIME_ code:99 + atexit elapsed:_TIME_ code:99 + EOF + test_cmp expect actual +' + sane_unset GIT_TRACE2_BRIEF # Now test without environment variables and get all Trace2 settings diff --git a/trace2.c b/trace2.c index e01cf77f1a8..d49d5d5a082 100644 --- a/trace2.c +++ b/trace2.c @@ -211,6 +211,12 @@ int trace2_cmd_exit_fl(const char *file, int line, int code) code &= 0xff; + if (bug_called_must_BUG) { + /* BUG_vfl() calls exit(), which calls us again */ + bug_called_must_BUG = 0; + BUG("had bug() output above, in addition missed BUG_if_bug() call"); + } + if (!trace2_enabled) return code; diff --git a/usage.c b/usage.c index b738dd178b3..c37b94f1a40 100644 --- a/usage.c +++ b/usage.c @@ -290,18 +290,24 @@ void warning(const char *warn, ...) /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */ int BUG_exit_code; -static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params) +static void BUG_vfl_common(const char *file, int line, const char *fmt, + va_list params) { char prefix[256]; - va_list params_copy; - static int in_bug; - - va_copy(params_copy, params); /* truncation via snprintf is OK here */ snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line); vreportf(prefix, fmt, params); +} + +static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params) +{ + va_list params_copy; + static int in_bug; + + va_copy(params_copy, params); + BUG_vfl_common(file, line, fmt, params); if (in_bug) abort(); @@ -322,6 +328,20 @@ NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...) va_end(ap); } +int bug_called_must_BUG; +void bug_fl(const char *file, int line, const char *fmt, ...) +{ + va_list ap, cp; + + bug_called_must_BUG = 1; + + va_copy(cp, ap); + va_start(ap, fmt); + BUG_vfl_common(file, line, fmt, ap); + va_end(ap); + trace2_cmd_error_va(fmt, cp); +} + #ifdef SUPPRESS_ANNOTATED_LEAKS void unleak_memory(const void *ptr, size_t len) { From patchwork Sat May 21 17:14:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12857982 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72729C433F5 for ; Sat, 21 May 2022 17:15:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343984AbiEURPE (ORCPT ); Sat, 21 May 2022 13:15:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232331AbiEURPA (ORCPT ); Sat, 21 May 2022 13:15:00 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B92A7666A9 for ; Sat, 21 May 2022 10:14:58 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id t6so15274276wra.4 for ; Sat, 21 May 2022 10:14:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HfMaqs+mdenlCZqp0pUfm1dnTpxAkJje7vISaf94uro=; b=CdLFgKrlJywiKlTftHgpAFQKBVZ0NPNi62yIY5GCKGCKuZMLejQ7/HV2PZhi1dM/38 axxHME0/D15mv5wtR799IiS3ECrInaS1+hlsQZ7LubKdpNCX9U3g7W14usqoEsCdXS9m AMdq9AVDdWfyMY8kDHr7zIDtmurY5Dn4HX75PdVk1idnGqH7ur1tU/SPrRp9u6a+PybS Lswu1xn0DMmclAwmLj5RH28Rl8/JE1McCO58uFH5ovH5mzichYBkuSY2YZDxyTt30rJp abl6C+CZsG8xEt7nnytVVV93miCdcbvs1Ue+azAAk09BgqSRbKeEc5eHpgu5kwp0KGAb ZBCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HfMaqs+mdenlCZqp0pUfm1dnTpxAkJje7vISaf94uro=; b=S94eQ+PAASF5QW+Xe2HGYL3fMzuIRhCGkl2hPA91UnPeTBrZnIfi4WRwUOjx+5ss+E gYVBmDfwrcFpfp++PyUolrbNPBBTtm4o27FV2NZ8kbKXVmBcA44GAGIudclFIWBLvSH2 BBXc9iHxY3F40+dZJzKkdb0dY2igCmo25wUMTNw79KsQ8vjPpPV/7i7yFRXRwugHS7de SKnatLy0ogCydrGgaGTkYHa8TVCxL/R81TsRh0r3oy+U1LsXTPky8b6V/XLKo9Ner7yW kQc+edMcBjf/J7AtWjFKAng2qC3J1pNcJvUnz9ZBsXTzKijkCcxUIw9PpVOVJDVDs8s1 2SoA== X-Gm-Message-State: AOAM532xfFwBy3zPdqDmCbQWZ/G5vLFk8MZKulgaN19OXHmtgvtef32H 3whV4LBYOcRc4h+USH+M2flT7yDmqwodkA== X-Google-Smtp-Source: ABdhPJy15uYCoDgQuLQpYgDRnERYXhguftGJwIwQuQQPcoFqGm8dKLUcA/ShxE0ccXsVcG77zSGWVQ== X-Received: by 2002:adf:e444:0:b0:20d:1329:76ca with SMTP id t4-20020adfe444000000b0020d132976camr12548804wrm.553.1653153296897; Sat, 21 May 2022 10:14:56 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id b15-20020a7bc24f000000b0039736892653sm4964685wmj.27.2022.05.21.10.14.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 May 2022 10:14:53 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Bagas Sanjaya , Abhradeep Chakraborty , =?utf-8?b?w4Z2?= =?utf-8?b?YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 2/5] parse-options.c: use new bug() API for optbug() Date: Sat, 21 May 2022 19:14:39 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.960.g7a4e2fc85c9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When we run into bugs in parse-options.c usage it's good to be able to note all the issues we ran into before dying. This use-case is why we have the optbug() function introduced in 1e5ce570ca3 (parse-options: clearer reporting of API misuse, 2010-12-02) Let's change this code to use the new bug() API introduced in the preceding commit, which cuts down on the verbosity of parse_options_check(). There are existing uses of BUG() in adjacent code that should have been using optbug() that aren't being changed here. That'll be done in a subsequent commit. This only changes the optbug() callers. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/parse-options.c b/parse-options.c index 6e57744fd22..7fff588a45f 100644 --- a/parse-options.c +++ b/parse-options.c @@ -14,15 +14,15 @@ enum opt_parsed { OPT_UNSET = 1<<1, }; -static int optbug(const struct option *opt, const char *reason) +static void optbug(const struct option *opt, const char *reason) { - if (opt->long_name) { - if (opt->short_name) - return error("BUG: switch '%c' (--%s) %s", - opt->short_name, opt->long_name, reason); - return error("BUG: option '%s' %s", opt->long_name, reason); - } - return error("BUG: switch '%c' %s", opt->short_name, reason); + if (opt->long_name && opt->short_name) + bug("switch '%c' (--%s) %s", opt->short_name, + opt->long_name, reason); + else if (opt->long_name) + bug("option '%s' %s", opt->long_name, reason); + else + bug("switch '%c' %s", opt->short_name, reason); } static const char *optname(const struct option *opt, enum opt_parsed flags) @@ -441,28 +441,27 @@ static void check_typos(const char *arg, const struct option *options) static void parse_options_check(const struct option *opts) { - int err = 0; char short_opts[128]; memset(short_opts, '\0', sizeof(short_opts)); for (; opts->type != OPTION_END; opts++) { if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) && (opts->flags & PARSE_OPT_OPTARG)) - err |= optbug(opts, "uses incompatible flags " - "LASTARG_DEFAULT and OPTARG"); + optbug(opts, "uses incompatible flags " + "LASTARG_DEFAULT and OPTARG"); if (opts->short_name) { if (0x7F <= opts->short_name) - err |= optbug(opts, "invalid short name"); + optbug(opts, "invalid short name"); else if (short_opts[opts->short_name]++) - err |= optbug(opts, "short name already used"); + optbug(opts, "short name already used"); } if (opts->flags & PARSE_OPT_NODASH && ((opts->flags & PARSE_OPT_OPTARG) || !(opts->flags & PARSE_OPT_NOARG) || !(opts->flags & PARSE_OPT_NONEG) || opts->long_name)) - err |= optbug(opts, "uses feature " - "not supported for dashless options"); + optbug(opts, "uses feature " + "not supported for dashless options"); switch (opts->type) { case OPTION_COUNTUP: case OPTION_BIT: @@ -471,7 +470,7 @@ static void parse_options_check(const struct option *opts) case OPTION_NUMBER: if ((opts->flags & PARSE_OPT_OPTARG) || !(opts->flags & PARSE_OPT_NOARG)) - err |= optbug(opts, "should not accept an argument"); + optbug(opts, "should not accept an argument"); break; case OPTION_CALLBACK: if (!opts->callback && !opts->ll_callback) @@ -494,10 +493,9 @@ static void parse_options_check(const struct option *opts) } if (opts->argh && strcspn(opts->argh, " _") != strlen(opts->argh)) - err |= optbug(opts, "multi-word argh should use dash to separate words"); + optbug(opts, "multi-word argh should use dash to separate words"); } - if (err) - exit(128); + BUG_if_bug(); } static void parse_options_start_1(struct parse_opt_ctx_t *ctx, From patchwork Sat May 21 17:14:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12857983 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 26AABC433EF for ; Sat, 21 May 2022 17:15:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245320AbiEURPO (ORCPT ); Sat, 21 May 2022 13:15:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344056AbiEURPL (ORCPT ); Sat, 21 May 2022 13:15:11 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E50A66CB4 for ; Sat, 21 May 2022 10:15:07 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id o12-20020a1c4d0c000000b00393fbe2973dso8449916wmh.2 for ; Sat, 21 May 2022 10:15:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nsTOyPms+0I1Xqcafs99ncW4nxy54zVM/Z/fBWT7/6w=; b=WyGdw41MrFJTD98pbxnm6u/itS6cW7bgwKc7eMtj0Uymgtt6HXx0VQ9R5DPW8P1p/3 LCW0gnTa44NbpfqVYJ6C5qfSB16AB2NJBj5eWGRElbL9MALVlyOMsjW2kaYlD8XFWgPl 6F/TtJsjmezusjYyQkJtFYBAb+4DecckWI2TMVrzkcVesC5ltnoy5HYd+51/0/m8XGNm 1rQfyHuHI9gmYNvjDe45PDUU7cyf7qHqWAWjtddUedhUVoeLKzu3PnLVI9NbnI9GN1GI x3YmXtdWSvyhXEo+NNv1CxEE0Y2qVlXOLV+/tEcubARlAo+RQ8VeMCh6TW9u3mvflSSH LBbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nsTOyPms+0I1Xqcafs99ncW4nxy54zVM/Z/fBWT7/6w=; b=Qwj/SeDMhNOME2TCHRTTDjjEKPWBS9tKIwrIOiKhxFFMKlANVl4um20YFp3uIV9p2i Cs4FClO5ugq6SFtD/Xl21dKBAtb4496WirQTWKqFrWtW9c0mpDifHB/td/QR52J9zUwj KITuLkT9mvfUVKseaQsuF2t5PT+1wSlUyxeRFcEGSAf33jz9EZp9DawRBof7jMCBLAHb h8xD7RA+5z2D3RPr64BMnrCBWcgYcUVRC+Ca5qq02xoPi+9wYnIAtFxeqaHLblndxMtX UQUesPlRXBRr1mfFVwtMW3h7hIaRfSZr/Mb+76ON5/SAKPASJBihdLsMr3Xzowqdp28Y 7gZQ== X-Gm-Message-State: AOAM533ps9FR4/mPtLrY4shf+sLcRPBUnYD2F1m8zVm15iB4DAoOYD9T YxdrcBS7Y0AJKbfT6OzjmsRZntnKjHBk8w== X-Google-Smtp-Source: ABdhPJzvsuY3YcNk+dE72eh26asdK1ZHfGUdeaoFqKF/PCxFqJgBsxV6BOlapTW0Trf+ANn3u9e80Q== X-Received: by 2002:a05:600c:3b17:b0:394:88f0:a4b with SMTP id m23-20020a05600c3b1700b0039488f00a4bmr13117486wms.129.1653153305359; Sat, 21 May 2022 10:15:05 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id b15-20020a7bc24f000000b0039736892653sm4964685wmj.27.2022.05.21.10.14.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 May 2022 10:15:00 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Bagas Sanjaya , Abhradeep Chakraborty , =?utf-8?b?w4Z2?= =?utf-8?b?YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 3/5] parse-options.c: use optbug() instead of BUG() "opts" check Date: Sat, 21 May 2022 19:14:40 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.960.g7a4e2fc85c9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the assertions added in bf3ff338a25 (parse-options: stop abusing 'callback' for lowlevel callbacks, 2019-01-27) to use optbug() instead of BUG(). At this point we're looping over individual options, so if we encounter any issues we'd like to report the offending option. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/parse-options.c b/parse-options.c index 7fff588a45f..5875936898f 100644 --- a/parse-options.c +++ b/parse-options.c @@ -474,20 +474,20 @@ static void parse_options_check(const struct option *opts) break; case OPTION_CALLBACK: if (!opts->callback && !opts->ll_callback) - BUG("OPTION_CALLBACK needs one callback"); + optbug(opts, "OPTION_CALLBACK needs one callback"); if (opts->callback && opts->ll_callback) - BUG("OPTION_CALLBACK can't have two callbacks"); + optbug(opts, "OPTION_CALLBACK can't have two callbacks"); break; case OPTION_LOWLEVEL_CALLBACK: if (!opts->ll_callback) - BUG("OPTION_LOWLEVEL_CALLBACK needs a callback"); + optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs a callback"); if (opts->callback) - BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback"); + optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs no high level callback"); break; case OPTION_ALIAS: - BUG("OPT_ALIAS() should not remain at this point. " - "Are you using parse_options_step() directly?\n" - "That case is not supported yet."); + optbug(opts, "OPT_ALIAS() should not remain at this point. " + "Are you using parse_options_step() directly?\n" + "That case is not supported yet."); default: ; /* ok. (usually accepts an argument) */ } From patchwork Sat May 21 17:14:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12857984 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4BABDC433EF for ; Sat, 21 May 2022 17:15:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344056AbiEURPQ (ORCPT ); Sat, 21 May 2022 13:15:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231949AbiEURPO (ORCPT ); Sat, 21 May 2022 13:15:14 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3A01666A9 for ; Sat, 21 May 2022 10:15:13 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id k26so6049879wms.1 for ; Sat, 21 May 2022 10:15:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=70xy1n2gDIvv+iETV/rv+1NlvCJQ2K0m5huNto1lIYw=; b=IGo0/WArEIciKnTvs64+ez+mRNsGgaT8o59mNLg6pZeOw/0jOdeIHfO+NTjZJgKUoi cn7uF1VG/tToARXH4LYf5Dh+okey5zR92LutD2Cw7pYS1qOT6RxeymswInUSDAXojZzp GOoU9nehKgn94+1TMAbLPVZd/Ti4pc1oR0qV5l31PE/2FG8aibnqxwHqXdLAOTr35AKn 7oON0MnpBk9KGf/dfiRt9Kl0S8SIxBAgqLgilB47B7gTx/1MZohlXiE+WzzvPhFV+Y0U cAj0QHWngYCEsjorQTSrQ1UgSpPIjzbO3WwABTiEXpxSBPPRTATQ0R+HBNlv6aOPpFIb 30VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=70xy1n2gDIvv+iETV/rv+1NlvCJQ2K0m5huNto1lIYw=; b=Q85mmSwE65fL8oX50oXStqRnXqwkAYi6nePDueYcEq8gCtCp/4wnw5FTceIKXgi83U M3qqKfiZMeTuBIkpcszwJkFc8PhGD/CbEqfCjukr3bW6MiGcu77xc+ksm3blMYeCiogK C7TM+plhZboo7wkVY+ZdlegAf4nqUPlbCxkX4kLJubANTR7TTZ4euZPMBkAMHLSq2z4D tdDRSXqAuO1j6nGq/MjTWF/zU7VZOSk/QgWTT7T6ATnsnXrgcWBSEd9WYbqv5iX+i39Q 5LU8m/k7+kuLofwvtS+sINeIUvfytAiZ8dBQ5yvIWuzzajfun4tBrsTzaDBnj8nxEBPp r47A== X-Gm-Message-State: AOAM532cQFqp/wOJvIr2agHDSJtXuyH9dbe+LukNYQhwaovmFExVD5kw jY2QmLDQShggx48NJ0s3D3r4V/zaXGbmBg== X-Google-Smtp-Source: ABdhPJzebQIFqGWpm+t+Jx+NxEohJqWAUTqNBNXnp21/j2WneI94+nCBhB8he2GT4rKdR17vSvadjw== X-Received: by 2002:a05:600c:2d86:b0:397:4743:5d25 with SMTP id i6-20020a05600c2d8600b0039747435d25mr349756wmg.11.1653153312121; Sat, 21 May 2022 10:15:12 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id b15-20020a7bc24f000000b0039736892653sm4964685wmj.27.2022.05.21.10.15.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 May 2022 10:15:08 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Bagas Sanjaya , Abhradeep Chakraborty , =?utf-8?b?w4Z2?= =?utf-8?b?YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 4/5] receive-pack: use bug() and BUG_if_bug() Date: Sat, 21 May 2022 19:14:41 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.960.g7a4e2fc85c9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Amend code added in a6a84319686 (receive-pack.c: shorten the execute_commands loop over all commands, 2015-01-07) and amended to hard die in b6a4788586d (receive-pack.c: die instead of error in case of possible future bug, 2015-01-07) to the new bug() function instead. Let's also rename the warn_if_*() function that code is in to BUG_if_*(), its name became outdated in b6a4788586d. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/receive-pack.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index ad20b41e3c8..d1b3e5c419e 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -1810,21 +1810,17 @@ static int should_process_cmd(struct command *cmd) return !cmd->error_string && !cmd->skip_update; } -static void warn_if_skipped_connectivity_check(struct command *commands, +static void BUG_if_skipped_connectivity_check(struct command *commands, struct shallow_info *si) { struct command *cmd; - int checked_connectivity = 1; for (cmd = commands; cmd; cmd = cmd->next) { - if (should_process_cmd(cmd) && si->shallow_ref[cmd->index]) { - error("BUG: connectivity check has not been run on ref %s", - cmd->ref_name); - checked_connectivity = 0; - } + if (!should_process_cmd(cmd) && si->shallow_ref[cmd->index]) + bug("connectivity check has not been run on ref %s", + cmd->ref_name); } - if (!checked_connectivity) - BUG("connectivity check skipped???"); + BUG_if_bug(); } static void execute_commands_non_atomic(struct command *commands, @@ -2005,7 +2001,7 @@ static void execute_commands(struct command *commands, execute_commands_non_atomic(commands, si); if (shallow_update) - warn_if_skipped_connectivity_check(commands, si); + BUG_if_skipped_connectivity_check(commands, si); } static struct command **queue_command(struct command **tail, From patchwork Sat May 21 17:14:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12857985 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C9A5C433F5 for ; Sat, 21 May 2022 17:15:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244001AbiEURPe (ORCPT ); Sat, 21 May 2022 13:15:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344022AbiEURPb (ORCPT ); Sat, 21 May 2022 13:15:31 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F53366AE8 for ; Sat, 21 May 2022 10:15:30 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id i20-20020a05600c355400b0039456976dcaso5748595wmq.1 for ; Sat, 21 May 2022 10:15:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=26OPc/IWX8SJR7d6DqYrd4Zx4/7RJqNnYY2MrayQs5U=; b=SmUhLVCAq0KvAV/U+FBaIhzfGAJRDqVkXdYFTwC2ASrS+LqpiYTWd0fSmj0h5DazJI yCMzmSdZBTay6T8F60gS998cf352pED5Xv74hlkpqpxegUwb0O8AqmHalpZugKqm3+q7 meWyE4f3/rB1DGIAdmD/mdyvkyCFOvXek+N92v+Ngr9dLEWqbtpVcQubtX5VfeD4HEht S++P/GCPmOU+lRclrD8nPKNVPXNpzhvtXYjHl5fSrZcIX1FrKHLAIN6D87KY95kyDZDj W0QeCxvoAV85gX+U4BMwlo4DWwmGrfE7YKnCnxp8iza6nZxuq32Xh56l67dcHxnJLW5o PiRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=26OPc/IWX8SJR7d6DqYrd4Zx4/7RJqNnYY2MrayQs5U=; b=0rTIjpXCxzkWDEfRM2QptZzrq6MPq7VDLA/qz9o+3ngSOUPZt+g2DnZT7JreVcEO+G dq6msB1R6WxMpZCwlft3tiHBcGZZhO4ktFgNORsG5SlgRE3mRKmFqam5oFvehsIk/1HL nqqvYzFbx0GOme4rgl/E9De9bEtJ5ovoy3JcHZHx7diwY8DejOfgSXWoZX9j189cz6ZA tpiuQUt1UtoQHraN1xYekB+ptakFC3BrpCL25zkIdTDQefEqcB23DfUQpB9n0GwiaGhV wjOlUtVE38/Lypq/AEMtYGnt/+mT/oNibTgUYdy1j4RY9GgpnlxEUShWk2ogzMrEs5c4 lzKQ== X-Gm-Message-State: AOAM532TnfxWzArI8bOEZ2YVTmkDFXMX+wUy4v02a89Eu+GsMYDxhOt+ aoJRQEfkV4AYGqR6i9eyaheU9XjoyEz9ow== X-Google-Smtp-Source: ABdhPJwafsXFo7CkCi3OVoqrKwrc7wuyy+cvBX+CuvFRH0w64GF0F9jTvS17SnAnh7wJIZkEv+Wv5A== X-Received: by 2002:a7b:cb91:0:b0:397:3225:244 with SMTP id m17-20020a7bcb91000000b0039732250244mr11585382wmi.68.1653153328313; Sat, 21 May 2022 10:15:28 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id b15-20020a7bc24f000000b0039736892653sm4964685wmj.27.2022.05.21.10.15.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 May 2022 10:15:21 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Bagas Sanjaya , Abhradeep Chakraborty , =?utf-8?b?w4Z2?= =?utf-8?b?YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 5/5] cache-tree.c: use bug() and BUG_if_bug() Date: Sat, 21 May 2022 19:14:42 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.960.g7a4e2fc85c9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change "BUG" output originally added in a97e4075a16 (Keep rename/rename conflicts of intermediate merges while doing recursive merge, 2007-03-31), and later made to say it was a "BUG" in 19c6a4f8369 (merge-recursive: do not return NULL only to cause segfault, 2010-01-21) to use the new bug() function. This gets the same job done with less code, this changes the output a bit, but since we're emitting BUG output let's say it's OK to prefix every line with the "unmerged index entry" message, instead of optimizing for readability. doing it this way gets rid of any state management in the loop itself in favor of BUG_if_bug(). Signed-off-by: Ævar Arnfjörð Bjarmason --- cache-tree.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cache-tree.c b/cache-tree.c index 6752f69d515..9e96097500d 100644 --- a/cache-tree.c +++ b/cache-tree.c @@ -692,14 +692,13 @@ struct tree* write_in_core_index_as_tree(struct repository *repo) { ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL); if (ret == WRITE_TREE_UNMERGED_INDEX) { int i; - fprintf(stderr, "BUG: There are unmerged index entries:\n"); for (i = 0; i < index_state->cache_nr; i++) { const struct cache_entry *ce = index_state->cache[i]; if (ce_stage(ce)) - fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce), - (int)ce_namelen(ce), ce->name); + bug("unmerged index entry on in-memory index write: %d %.*s", + ce_stage(ce), (int)ce_namelen(ce), ce->name); } - BUG("unmerged index entries when writing inmemory index"); + BUG_if_bug(); } return lookup_tree(repo, &index_state->cache_tree->oid);