eBPF-based security platform
coccinelle: report a conditional and an else if that repeat a test
Daniel Borkmannbc6a8a684c88
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 17f77858..c71d4d42 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -712,8 +712,9 @@ can rewrite what it finds does so with ``-D patch``:- An allocation sized for another type than its pointer, and afree of memory no allocator returned.* - ``double_test``, ``double_init``- - A term tested twice, an ``if`` whose branches are alike, and a- member initialized twice.+ - A term tested twice, an ``if`` or a conditional whose branches+ are alike, an ``else if`` that tests what an ``if`` before it+ tested, and a member initialized twice.* - ``va_end``- A ``va_start()`` or ``va_copy()`` without a ``va_end()`` on apath out.diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 22a729eb..7eb0381a 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -57,14 +57,14 @@ signed config image.alone, the rewrite of ``-D patch``, a case for each rule takenfrom CERT C, a case for each BPF hazard rule beside the safe formit leaves alone, a case at its line for ``stale_success``,- ``break_error``, ``sum_overwrite``, ``fd_leak``, ``getenv_null``- and ``strerror_negative`` beside the forms it leaves alone, a- dead store beside the reads ``dead_store`` leaves alone, a count- by hand in a macro and an ``ARRAY_SIZE()`` of a file's own for- ``array_size``, which rewrites the one and leaves the other, a- file a rule gives up on at its time limit named rather than- failed, ``SEC`` declared an attribute in every rule, and the tree- itself+ ``break_error``, ``sum_overwrite``, ``fd_leak``, ``getenv_null``,+ ``strerror_negative`` and ``double_test`` beside the forms it+ leaves alone, a dead store beside the reads ``dead_store`` leaves+ alone, a count by hand in a macro and an ``ARRAY_SIZE()`` of a+ file's own for ``array_size``, which rewrites the one and leaves+ the other, a file a rule gives up on at its time limit named+ rather than failed, ``SEC`` declared an attribute in every rule,+ and the tree itself* - ``t0007-check-operator-linkage``- ``check-operator-linkage.sh``: the libraries an operator binarymay link and its own interpreter, another loader, a librarydiff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.shindex c3c9feac..13dc69c3 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -376,9 +376,10 @@ test_expect_success 'each BPF rule reports its sample and spares the safe form'# the forms the rule leaves alone: the count of reports shows they stay# quiet.SAMPLE_RULES="stale_success break_error sum_overwrite fd_leak getenv_null-strerror_negative"+strerror_negative double_test"SAMPLE_LINES="stale_success:9 break_error:53 sum_overwrite:110 fd_leak:148-getenv_null:170 getenv_null:177 strerror_negative:196 strerror_negative:202"+getenv_null:170 getenv_null:177 strerror_negative:196 strerror_negative:202+double_test:215 double_test:222 double_test:231 double_test:243 double_test:254"write_line_samples() {cat >"$1" <<'EOF'int search_left(int fd, int key)@@ -592,6 +593,62 @@ void report_negated(int ret, int err)err = errno;pr_err("read: %s", strerror(err));}++const char *direction_alike(int egress)+{+ return egress ? "ingress" : "ingress";+}++int kind_two(int kind)+{+ if (kind == 3)+ return 1;+ else if (kind == 3)+ return 2;+ return 0;+}++int kind_named_twice(const char *kind)+{+ if (!strcmp(kind, "xdp"))+ return 1;+ else if (!strcmp(kind, "xdp"))+ return 2;+ else+ return 0;+}++int kind_repeated(int kind)+{+ if (kind == 3)+ return 1;+ else if (kind == 4)+ return 2;+ else if (kind == 3)+ return 3;+ return 0;+}++int kind_repeated_else(int kind)+{+ if (kind == 3)+ return 1;+ else if (kind == 4)+ return 2;+ else if (kind == 3)+ return 3;+ else+ return 0;+}++int counted_twice(struct lexer *l)+{+ if (l->pos++ == 0)+ return 1;+ else if (l->pos++ == 0)+ return 2;+ return 0;+}EOF}diff --git a/testing/coccinelle/double_test.cocci b/testing/coccinelle/double_test.cocciindex 3e9783f0..32255e05 100644--- a/testing/coccinelle/double_test.cocci+++ b/testing/coccinelle/double_test.cocci@@ -1,8 +1,9 @@// SPDX-License-Identifier: BSD-2-Clause// Copyright (c) 2026 Authors of shirudo//-// A condition that tests one term twice, or an if whose branches do the-// same, is a copy left in place of what was meant.+// A condition that tests one term twice, an if or a conditional whose+// branches do the same, or an else if that tests what an if before it+// tested and so never runs, is a copy left in place of what was meant.virtual report@@ -33,3 +34,39 @@ position p;p << b.p;@@coccilib.report.print_report(p[0], "both branches of the if do the same")++@t@+expression C, E;+position p;+@@+ C ?@p E : E++@script:python depends on report@+p << t.p;+@@+coccilib.report.print_report(p[0], "both branches of the conditional are alike")++@e@+expression C, C2;+statement S1, S2, S3, S4;+position p;+@@+(+ if (C) S1 else if (C@p) S2+|+ if (C) S1 else if (C@p) S2 else S3+|+ if (C) S1 else if (C2) S2 else if (C@p) S3+|+ if (C) S1 else if (C2) S2 else if (C@p) S3 else S4+)++@script:python depends on report@+p << e.p;+C << e.C;+@@+import re+# A test that changes what it tests, as a ++ or an assignment does, may+# find another answer the second time.+if not re.search(r"\+\+|--|[^=!<>]=[^=]", C):+ coccilib.report.print_report(p[0], "an else if that tests what an if before it tested")