I see your points, you are definitely right :)
G
On 2015-09-09 14:04, Giorgio Marzano wrote:
I have compiled this sniplet:
main (int argc, char *argv[])
{
int i;
const mares_iconhd_model_t models[] = {
{"Matrix", MATRIX},
// {"Smart Apnea", SMARTAPNEA},
{"Smart", SMART},
{"Icon HD", ICONHD},
{"Icon AIR", ICONHDNET},
{"Puck Pro", PUCKPRO},
{"Nemo Wide 2", NEMOWIDE2},
{"Puck 2", PUCK2},
};
// Check the product name in the version packet against the list
// with valid names, and return the corresponding model number.
unsigned int model = 0;
for ( i = 0; i < 7; ++i) {
printf ("i: %d, sizeof: %d, strlen: %d\n",i,sizeof(models[i].name),strlen
(models[i].name));
}
}
and the corresponding ouput is:
giorgio@giorgio-laptop:~$ ./prova i: 0, sizeof: 17, strlen: 6 i: 1, sizeof:
17, strlen: 5 i: 2, sizeof: 17, strlen: 7 i: 3, sizeof: 17, strlen: 8 i: 4,
sizeof: 17, strlen: 8 i: 5, sizeof: 17, strlen: 11 i: 6, sizeof: 17,
strlen: 6
So it seems to me that either we use strlen or we use strcmp
No, the memcmp is correct. In the mares header, the name is a 16 byte array. If the name is shorter than 16 bytes, the remaining bytes are padded with zero's. But if there would be a name that's exactly 16 bytes long, then there won't be a terminating zero byte. In that case, strlen and strcmp will result in a buffer overflow! That's why we use memcmp. The other reason is that I want to do an exact match. The "Smart" entry should not match "Smart Apnea". Future models needs to be added explicitly, and not detected by accident.
Jef