News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Example optimizer bug

Started by czerny, January 04, 2015, 09:52:53 PM

Previous topic - Next topic

czerny

Try this function
int EQ(char* ok, char* in)
{
int len;
char *p, *q, *o;

if (in && ok) {
if (in[0]) {
o = malloc(strlen(ok)+1);
strcpy(o,ok);
len = strlen(in)-1;
while (in[len] == ' ') in[len--]='\0';
while (*in == ' ') in++;
p = o;
while (NULL != (q = strchr(p, ','))) {
*q = '\0';
if (!strcmp(p, in)) return 1;
p = q + 1;                                       // why is 'o' changing?
while (*p == ' ') p++;
}
if (!strcmp(p, in))
return 1;
} else {
if (!strcmp(ok, "(kein Participle)"))
return 1;                                     // why it take this return?
}
}
return 0;
}


with the call
EQ("bla, blub, zack", "zack");

CLR

Hello czerny.

Quote from: czerny// why is 'o' changing?

p = o;
p points to o,

q = strchr(p, ',');
q points to ',' part of p;

*q = '\0';
modify p and o.

czerny

#2
Quote from: CLR on January 09, 2015, 10:48:31 AM
Hello czerny.

Quote from: czerny// why is 'o' changing?

p = o;
p points to o,

q = strchr(p, ',');
q points to ',' part of p;

*q = '\0';
modify p and o.
That is not the point. Step trough the example, look exactly after the line where my comment is set  and you know what I mean.

Superlokkus

You're using the strlen() function, did you switch off any inlinening?

Because maybe its the bug in the optimizer already found in http://forum.pellesc.de/index.php?topic=6357

czerny


Superlokkus

I recommend to refractor you code, it's hardly readable for me and try to shorten it so you can reproduce the bug with minimal code.

czerny

I forgot! The first problem was 'ok' himself. I had to make a local copy in 'o'. Without this the code looks like so:
int EQ(char* ok, char* in)
{
int len;
char *p, *q;

if (in && ok) {
if (in[0]) {
len = strlen(in)-1;
while (in[len] == ' ') in[len--]='\0';
while (*in == ' ') in++;
p = ok;
while (NULL != (q = strchr(p, ','))) {
*q = '\0';
if (!strcmp(p, in)) return 1;
p = q + 1;           
while (*p == ' ') p++;
}
if (!strcmp(p, in))
return 1;
} else {
if (!strcmp(ok, "(kein Participle)"))
return 1;                                     // why it take this return?
}
}
return 0;
}