JuliaHubOSS / llvm-cbe

resurrected LLVM "C Backend", with improvements
Other
826 stars 141 forks source link

Bug in the char array representation of string #147

Closed SusanTan closed 2 years ago

SusanTan commented 2 years ago

The code below produces wrong output. The correct output: g, HHH, 5, YY, ++, ///, \,

the produced output: g, HHH, 5, YY, ++, ///, \

(missing the last comma) I think the produced cbe.c file probably doesn't include '\0' in the argument passed to function "split", so the loop iterates 1 less iteration than the original c code.

include

include

include

char split(char str);

int main(void) { char input[13] = "gHHH5YY++///\"; printf("%s\n", split(input));

return 0; }

char split(char str) { char last = str; char result = malloc(3strlen(str)); char counter = result;

for(char c = str; c; c++) { if(c != last) { strcpy(counter, ", "); counter += 2; last = c; } counter = c; counter++; } *(counter--) = '\0';

return realloc(result, strlen(result)); }

vtjnash commented 2 years ago

If I am counting right, isn't your declared copy of the string (the input array variable) a byte too small to hold the text, so this program behavior is undefined?

vtjnash commented 2 years ago

And the realloc looks definitely a byte too small, so again UB

SusanTan commented 2 years ago

Oops! Sorry about that.. Yes it's a bug from the benchmark.