[PATCH] Use saturated arithmetic for overflow detection.
Ondřej Bílka
neleai@seznam.cz
Wed Oct 30 21:53:00 GMT 2013
On Wed, Oct 30, 2013 at 11:44:18AM -0700, Paul Eggert wrote:
>
> All in all it's not clear that this approach is an improvement
> overall, as the performance penalty may not be worth the increase in
> clarity, and both approaches seem about equally error-prone.
This approach is less error prone as conversion can be carried out
automatically. I attached script simple that uses coccinelle to rewrite
expressions inside malloc to saturating ones which is valid
transformation.
One can run this and then sieve through modifications that are
uninteresting like
malloc (strlen (x) + 1) -> malloc (ADD_S (strlen (x), 1))
Also I do not know how in coccinelle make following transformation
working.
- i = e1 + e2
+ i = ADD_S(e1, e2)
...
ADD_S (i, x)
-------------- next part --------------
echo "@@ expression e1, e2; identifier i; @@
- malloc (e1 * e2)
+ malloc (MUL_S (e1, e2))" > mallocmul.cocci
echo "@@ expression e1, e2; identifier i; @@
- malloc (e1 + e2)
+ malloc (ADD_S (e1, e2))" > mallocadd.cocci
echo "@@ expression e1, e2, e3; @@
- MUL_S (e1 * e2, e3)
+ MUL_S (MUL_S (e1, e2), e3)" > malloclmm.cocci
echo "@@ expression e1, e2, e3; @@
- MUL_S (e1 + e2, e3)
+ MUL_S (ADD_S (e1, e2), e3)" > malloclma.cocci
echo "@@ expression e1, e2, e3; @@
- ADD_S (e1 * e2, e3)
+ ADD_S (MUL_S (e1, e2), e3)" > malloclam.cocci
echo "@@ expression e1, e2, e3; @@
- ADD_S (e1 + e2, e3)
+ ADD_S (ADD_S (e1, e2), e3)" > malloclaa.cocci
echo "@@ expression e1, e2, e3; @@
- MUL_S (e3, e1 * e2)
+ MUL_S (e3, MUL_S (e1, e2))" > mallocrmm.cocci
echo "@@ expression e1, e2, e3; @@
- MUL_S (e3, e1 + e2)
+ MUL_S (e3, ADD_S (e1, e2))" > mallocrma.cocci
echo "@@ expression e1, e2, e3; @@
- ADD_S (e3, e1 * e2)
+ ADD_S (e3, MUL_S (e1, e2))" > mallocram.cocci
echo "@@ expression e1, e2, e3; @@
- ADD_S (e3, e3, e1 + e2)
+ ADD_S (e3, ADD_S (e1, e2))" > mallocraa.cocci
for FILE in `git grep --name-only 'malloc (.*[*+].*)' | grep 'c$' `; do
echo $FILE
for J in `seq 1 2`; do
for SPATCH in *.cocci; do
timeout 3s spatch $SPATCH -in_place $FILE 2> /dev/null
done
done
done
More information about the Libc-alpha
mailing list