There is currently no check if there are more than 256 characters between parens. Current implementation silently writes beyond allocated buffer (t[j] = program[i];
) which leads to heap corruption.
Proposed fix:
replace case '(': {...} break;
with this:
case '(':
{
int level = 0;
int j = 0;
int size = 256;
char *t = malloc(size);
i++;
level++;
while (level > 0) {
if (program[i] == '(')
level++;
else if (program[i] == ')')
level--;
if (level > 0) {
t[j] = program[i];
j++;
if (j == size) {
size *= 2;
t = realloc(t, size);
}
}
i++;
}
i--;
t[j] = '\0';
push(t);
}
break;
int size = 256;
char *t = malloc(size);
if (j == size) {
size *= 2;
t = realloc(t, size);
}