https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121195

            Bug ID: 121195
           Summary: [GCOV] Incorrect cov for return-stmt after access of
                    errno.
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: gcov-profile
          Assignee: unassigned at gcc dot gnu.org
          Reporter: njuwy at smail dot nju.edu.cn
  Target Milestone: ---

gcc version:
gcc version 16.0.0 20250704 (experimental) (GCC) 

cmd:
gcc --coverage -std=c2x -lm -O0 test.c -o test
./test
gcov ./*.gcda -t > test.c.gcov

$ cat test.c
#include <errno.h>
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
void process(int value) {
    if (value < 0) {
        errno = EINVAL;
        return;
    }
    char *endptr;
    const char *locale = setlocale(LC_ALL, NULL);
    if (!locale) {
        fprintf(stderr, "Failed to get locale\n");
        return;
    }

    char buf[256];
    snprintf(buf, sizeof(buf), "%s", locale);
    long result = strtol(buf, &endptr, 10);
    if (*endptr != '\0') {
        fprintf(stderr, "Invalid number format\n");
        return;
    }
}
int main() {

    for (int i = 0; i < 5; ++i) {
        process(i - 2);
    }
    return EXIT_SUCCESS;
}

$ cat test.c.gcov
        -:    1:#include <errno.h>
        -:    2:#include <locale.h>
        -:    3:#include <stdlib.h>
        -:    4:#include <stdio.h>
        5:    5:void process(int value) {
        5:    6:    if (value < 0) {
        2:    7:        errno = EINVAL;
        5:    8:        return;
        -:    9:    }
        -:   10:    char *endptr;
        3:   11:    const char *locale = setlocale(LC_ALL, NULL);
        3:   12:    if (!locale) {
    #####:   13:        fprintf(stderr, "Failed to get locale\n");
    #####:   14:        return;
        -:   15:    }
        -:   16:
        -:   17:    char buf[256];
        3:   18:    snprintf(buf, sizeof(buf), "%s", locale);
        3:   19:    long result = strtol(buf, &endptr, 10);
        3:   20:    if (*endptr != '\0') {
        3:   21:        fprintf(stderr, "Invalid number format\n");
        3:   22:        return;
        -:   23:    }
        -:   24:}
        1:   25:int main() {
        -:   26:
        6:   27:    for (int i = 0; i < 5; ++i) {
        5:   28:        process(i - 2);
        -:   29:    }
        1:   30:    return EXIT_SUCCESS;
        -:   31:}


Cov of line 8 was wrongly marked as 5.

Reply via email to