JuliaHubOSS/llvm-cbe

Bug in the char array representation of string

SusanTan opened this issue · 3 comments

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<stdio.h>
#include<stdlib.h>
#include<string.h>

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));
}

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?

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

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