code bug?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

code bug?

Jump to solution
560 Views
sebasira
Senior Contributor I

Me again!

 

Now with a doubt about a small portion of code... I use cppcheck to find error that compiler can not detect. Well, it acuse this error, but I can't still figrue it out.

 

Here's the code:

byte* const Tabla_CamposDir[]={  DIR_hora,  DIR_cta_cte,  DIR_calle,  DIR_numero,  DIR_ubicacion,  DIR_pasajero,  DIR_demora,  DIR_comentario,  DIR_zona,  DIR_base,  DIR_calle1,  NULL,               // NOTHING  DIR_tarjeta,  DIR_pactado,  DIR_calle2,  DIR_opcional1,  DIR_opcional2,  DIR_opcional3};  void DIRECCION_resetCampos (void){  word i, j;  byte* ptr;    for (i=0; i<(sizeof(Tabla_CamposDir)/2); i++){    ptr = Tabla_CamposDir[i];    // Erase 6 bytes of each buffer (minimum length)    if (ptr != NULL){      for (j=0; j<6; j++){        *(ptr + j) = 0;      }    }  }

 What's inside Tabla_CamposDir are all arrays of at least 7bytes each, I mean:

byte DIR_hora[7];byte DIR_cta_cte[10];

 

The cppcheck says:  Buffer access out-of-bounds: Tabla_CamposDir 

at ptr = Tabla_CamposDir[i];

 

If that's rigth.... How can I solve it? Maybe I'm blind, but I can't figure it out!!

 

 

Thanks to you all!!!

Labels (1)
0 Kudos
1 Solution
302 Views
djsimpson
Contributor III

Hi Sebastian,

 

   sizeof(Tabla_CamposDir)/2 is only the number of elements in the array if the sizeof a byte* is 2. I am not familar with cppchecker, but you would need to check what size it assumes a pointer to a byte const is. If cppcheck thought sizeof(byte *) was 4, then it would indeed flag a access out-of-bounds.

 

A better way for calculating the number of elements would be : sizeof(Tabla_CamposDir)/sizeof(Table_CamposDir[0])

 

David

 

 

View solution in original post

0 Kudos
2 Replies
303 Views
djsimpson
Contributor III

Hi Sebastian,

 

   sizeof(Tabla_CamposDir)/2 is only the number of elements in the array if the sizeof a byte* is 2. I am not familar with cppchecker, but you would need to check what size it assumes a pointer to a byte const is. If cppcheck thought sizeof(byte *) was 4, then it would indeed flag a access out-of-bounds.

 

A better way for calculating the number of elements would be : sizeof(Tabla_CamposDir)/sizeof(Table_CamposDir[0])

 

David

 

 

0 Kudos
302 Views
sebasira
Senior Contributor I

Hi David!

 

Well, you're the winner!!! LOL!

 

Thanks for taking a look at it.

You're rigth, I didn't notice that the size of pointer to byte could be different. With the modification you suggest no errors are thrown

 

 

Thanks a lot!

0 Kudos