The following function (From Erlang/OTP) has an interesting mistake:
+++
static char *hex(char digest[16], char buff[33])
{
static char tab[] = "0123456789abcdef";
unsigned char *d = (unsigned char *) digest;
//static char buff[sizeof(digest)*2 + 1];
char *p = buff;
int i;
for (i = 0; i < sizeof(digest); ++i) {
*p++ = tab[(int)((*d) >> 4)];
*p++ = tab[(int)((*d++) & 0xF)];
}
*p = '\0';
return buff;
}
+++
In the for-loop, the call sizeof(digest) is returning 8 (on a 64bit
arch) because the array 'digest' is passed as a parameter and hence is
a pointer. The sizeof(...) operator then returns the pointer size and
not the value of 16 which was intended. I want a semantic match on
this beast :)
So far, I've been able to come up with the following first iteration
of a generalization which should match the above, but needs further
generalization in order to hit similar and like problems:
+++
@ grab_sizeof @
type T;
function f;
identifier x, buff;
@@
static char *f(T x[16], char buff[33])
{
...
* sizeof(x)
...
}
+++
My problem however is that cocci does not like the array specifier in
static char *f(T x[16], char buff[33])
Specifically, it fails on the first occurrence of '[', an example from
my tests:
Fatal error: exception Failure("minus: parse error:
= File "sizeof.2.cocci", line 5, column 28, charpos = 69
around = '[', whole content = static char *hex(char digest[16],
char buff[33])
Will cocci currently support this notation? It looks as if the parser
is not being a happy ladybug :) I have a hunch that just
highlighting where this occurs can uncover many bugs with a quick
glance as it is quite counter-intuitive to the programmer.
The full source code is available in the Erlang/OTP source code distribution at:
https://github.com/erlang/otp/blob/dev/lib/erl_interface/src/connect/ei_connect.c
and the commit that piqued my curiosity at:
https://github.com/cristiangreco/otp/commit/6228f215f1a99ab282688516d3d577295fa8ef5a#diff-0
--
J.
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)