NO

Author Topic: Example optimizer bug  (Read 3783 times)

czerny

  • Guest
Example optimizer bug
« on: January 04, 2015, 09:52:53 PM »
Try this function
Code: [Select]
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
Code: [Select]
EQ("bla, blub, zack", "zack");

CLR

  • Guest
Re: Example optimizer bug
« Reply #1 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.

czerny

  • Guest
Re: Example optimizer bug
« Reply #2 on: January 09, 2015, 12:05:11 PM »
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.
« Last Edit: January 09, 2015, 12:16:45 PM by czerny »

Superlokkus

  • Guest
Re: Example optimizer bug
« Reply #3 on: January 09, 2015, 02:52:43 PM »
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

  • Guest
Re: Example optimizer bug
« Reply #4 on: January 09, 2015, 02:53:37 PM »
No inlining.

Superlokkus

  • Guest
Re: Example optimizer bug
« Reply #5 on: January 09, 2015, 03:45:18 PM »
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

  • Guest
Re: Example optimizer bug
« Reply #6 on: January 14, 2015, 10:13:22 AM »
I forgot! The first problem was 'ok' himself. I had to make a local copy in 'o'. Without this the code looks like so:
Code: [Select]
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;
}