<p dir="ltr">I see your points, you are definitely right :)</p>
<p dir="ltr">G<br><br><br></p>
<div class="gmail_quote">Il 09/Set/2015 14:45, "Jef Driesen" <<a href="mailto:jef@libdivecomputer.org">jef@libdivecomputer.org</a>> ha scritto:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 2015-09-09 14:04, Giorgio Marzano wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I have compiled this sniplet:<br>
<br>
main (int argc, char *argv[])<br>
{<br>
int i;<br>
const mares_iconhd_model_t models[] = {<br>
{"Matrix", MATRIX},<br>
// {"Smart Apnea", SMARTAPNEA},<br>
{"Smart", SMART},<br>
{"Icon HD", ICONHD},<br>
{"Icon AIR", ICONHDNET},<br>
{"Puck Pro", PUCKPRO},<br>
{"Nemo Wide 2", NEMOWIDE2},<br>
{"Puck 2", PUCK2},<br>
};<br>
<br>
// Check the product name in the version packet against the list<br>
// with valid names, and return the corresponding model number.<br>
unsigned int model = 0;<br>
for ( i = 0; i < 7; ++i) {<br>
printf ("i: %d, sizeof: %d, strlen: %d\n",i,sizeof(models[i].name),strlen<br>
(models[i].name));<br>
<br>
}<br>
}<br>
<br>
and the corresponding ouput is:<br>
<br>
<br>
giorgio@giorgio-laptop:~$ ./prova i: 0, sizeof: 17, strlen: 6 i: 1, sizeof:<br>
17, strlen: 5 i: 2, sizeof: 17, strlen: 7 i: 3, sizeof: 17, strlen: 8 i: 4,<br>
sizeof: 17, strlen: 8 i: 5, sizeof: 17, strlen: 11 i: 6, sizeof: 17,<br>
strlen: 6<br>
<br>
<br>
So it seems to me that either we use strlen or we use strcmp<br>
</blockquote>
<br>
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.<br>
<br>
Jef<br>
</blockquote></div>