Possible bug in SDK with If statement logic?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
SamuraiX
Posts: 76
Joined: Tue Jan 31, 2006 6:28 am
Location: USA
Contact:

Possible bug in SDK with If statement logic?

Post by SamuraiX »

I think I found a bug in the SDK. While the compiler does not complain, the psp is crashing within executing this if statement. The fix that I found was to be more detailed within the If statement. Running the same code for dos there was no issue with the original code.

original

Code: Select all

int player_check_energy(int which, int ani){
   if(
      self->model->animation[ani] &&
      (which &&
      self->model->animation[ani]->mponly != 2 &&
      self->mp > self->model->animation[ani]->energycost) ||
      (!which &&
      self->model->animation[ani]->mponly != 1 &&
      self->health > self->model->animation[ani]->energycost)
   )
      return 1;

   return 0;
}

to the new version

Code: Select all

int player_check_energy(int which, int ani){
   if(
      self->model->animation[ani] &&
      ((which &&
      (self->model->animation[ani]->mponly != 2) &&
      (self->mp > self->model->animation[ani]->energycost)) ||
      (!which &&
      (self->model->animation[ani]->mponly != 1) &&
      (self->health > self->model->animation[ani]->energycost)))
   )
      return 1;

   return 0;
}
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

You have a statement of the form

Code: Select all

if(a && b || c)
and you rewrote it as

Code: Select all

if(a && (b || c))
The compiler didn't complain because what you wrote is technically valid, but the order of operations isn't what you expected. If you were to have compiled this with all warnings enabled (-Wall) you get the very clear warning:

Code: Select all

test.c: In function ‘player_check_energy’:
test.c:14: warning: suggest parentheses around && within ||
This isn't a SDK bug.
Post Reply