Skip to content

[Coverage] case in a switch statement incorrectly marked as covered #158080

@erkaii

Description

@erkaii

Hit the issue when measuring coverage for the following Debian package lines:

  1. distro-info-1.5+deb12u1/distro-info-util.c:759
  2. distro-info-1.5+deb12u1/distro-info-util.c:875
  3. mawk,mawk-1.3.4.20200120/print.c:58
  4. mawk,mawk-1.3.4.20200120/rexp0.c:233

How to reproduce it (Compiler explorer link):

cat > test.c << EOF
int main(int argc, char** argv) {
    switch(0) {
        case 0:
            if (argc > 2)
                break;
            break;
        case 1:
            break;
    }
    return 0;
}
EOF
clang -fprofile-instr-generate -fcoverage-mapping test.c -o test
./test
llvm-profdata merge default.profraw -o default.profdata
llvm-cov show -instr-profile default.profdata test

Coverage report where line 7 is incorrectly marked as covered:

    1|      1|int main(int argc, char** argv) {
    2|      1|    switch(0) {
    3|      1|        case 0:
    4|      1|            if (argc > 2)
    5|      0|                break;
    6|      1|            break;
    7|      1|        case 1:
    8|      0|            break;
    9|      1|    }
   10|      1|    return 0;
   11|      1|}

The necessary condition to trigger this bug:

  1. An if statement containing a control-flow change statement inside a switch.
  2. A control-flow change statement immediately following the if statement.

Tests done with

clang --version
Ubuntu clang version 20.1.8 (++20250708082409+6fb913d3e2ec-1~exp1~20250708202428.132)
Target: x86_64-pc-linux-gnu
Thread model: posix

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions