对于GCC 13.2,以下代码的输出取决于优化级别:
#include <ctype.h>
#include <stdio.h>
char *SkipAName(char *s) {
if (('A' <= *s && *s <= 'Z') || ('a' <= *s && *s <= 'z') || *s == '_' ||
*s == '$') {
if (*s == '$') {
s++;
}
while (isalnum(*s)) {
s++;
}
if (*s == '_') {
s++;
}
}
return s;
}
int TestName(char *name) {
while (*name) {
name++;
}
return 0;
}
int StrICmp(char *s1, char *s2) {
while (*s1 && tolower(*s1) == tolower(*s2)) {
s1++;
s2++;
}
return tolower(*s1) - tolower(*s2);
}
int DoTable(char *s) {
char *name, c;
do {
name = s;
s = SkipAName(s);
c = *s;
*s = 0;
TestName(name);
*s = c;
if (*s == '(') {
break;
}
if (*s != ',') {
printf("Error 1\n");
return 1;
}
*s = 0;
if (StrICmp(name, "sparse") == 0) {
} else {
printf("Error 2\n");
return 1;
}
*s++ = ',';
while (*s == ',') {
s++;
}
} while (*s);
printf("OK\n");
return 0;
}
int main() {
char buf[] = "sparse,C(1)";
DoTable(buf);
return 0;
}
$ gcc-13 -O0 test.c && ./a.out
OK
$ gcc-13 -O1 test.c && ./a.out
OK
$ gcc-13 -O2 test.c && ./a.out
Error 2
$ gcc-13 -O3 test.c && ./a.out
Error 2
代码来自this project,我试图制作一个最小的可重复的示例;这就是为什么这段代码看起来很尴尬.这是this issue.
我想知道这是GCC 13中的一个错误,还是我遇到了一些不确定的行为.有了Compiler Explorer,看起来第52行的*s = 0
似乎被优化了.该代码在GCC 12.3版中运行良好.