Issue |
149780
|
Summary |
[LLVM-COV] The call to the standard library function isdigit() in the while loop condition leads to inccrect coverage.
|
Labels |
new issue
|
Assignees |
|
Reporter |
8ss-boop
|
llvm version:
clang version 21.0.0git (https://github.com/llvm/llvm-project.git 872eac7af0050813062baba9662beb81093b6b55)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /root/software/llvm-releases/llvm-0704/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
```c
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void) {
const char *str = "12345abc67890";
int sum = 0;
size_t i = 0;
while (str[i]) {
if (isdigit((unsigned char)str[i])) {
size_t j = i;
while (isdigit((unsigned char)str[j]))
++j;
size_t len = j - i;
char *buf = malloc(len + 1);
if (!buf) return 1;
memcpy(buf, str + i, len);
buf[len] = '\0';
sum += atoi(buf);
free(buf);
i = j;
} else {
++i;
}
}
printf("Sum of numbers in string: %d\n", sum);
return 0;
}
```
for the program above, we got the following coverage report from llvm-cov
```
/*
clang -std=c2x -lm -fcoverage-mapping -fprofile-instr-generate test.c
./a.out
llvm-profdata merge default.profraw -o default.profdata;
llvm-cov show --show-line-counts -instr-profile="" ./a.out > test.c.lcov
*/
1| |#include <ctype.h>
2| |#include <stdlib.h>
3| |#include <stdio.h>
4| |#include <string.h>
5| |
6| 1|int main(void) {
7| 1| const char *str = "12345abc67890";
8| 1| int sum = 0;
9| 1| size_t i = 0;
10| |
11| 6| while (str[i]) {
12| 5| if (isdigit((unsigned char)str[i])) {
13| 2| size_t j = i;
14| 2| while (isdigit((unsigned char)str[j]))
15| 10| ++j;
16| 2| size_t len = j - i;
17| 2| char *buf = malloc(len + 1);
18| 2| if (!buf) return 1;
19| 2| memcpy(buf, str + i, len);
20| 2| buf[len] = '\0';
21| 2| sum += atoi(buf);
22| 2| free(buf);
23| 2| i = j;
24| 3| } else {
25| 3| ++i;
26| 3| }
27| 5| }
28| 1| printf("Sum of numbers in string: %d\n", sum);
29| 1| return 0;
30| 1|}
```
the output is:
```
Sum of numbers in string: 80235
```
line 14 was executed 12 times instead of 2.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs