Re: [RFC PATCH] coccinelle: check for integer overflow in binary search

From: Markus Elfring
Date: Thu Sep 05 2019 - 08:33:14 EST


> +identifier l, h, m;

Can expressions make sense for these metavariables?


> +@@
> +(
> + while (\(l < h\|l <= h\|(h - l) > 1\|(l + 1) < h\|l < (h - 1)\)) {
> + ...
> +(
> + ((l + h)@p / c)
> +|
> + ((l + h)@p >> c)
> +)
> + ...
> + }

* I suggest again to look at further possibilities to reduce undesirable
code duplication also together with the usage of SmPL disjunctions.

* The condition specification might be easier to read with a few
additional spaces (or the following variant).

* The SmPL ellipses will probably need further considerations.


+@@
+(
+ while (
+( l \( < \| <= \) h
+| (h - l) > 1
+| (h - 1) > l
+| (l + 1) < h
+) )
+ {
+ <+...
+ ((l + h)@p \( / \| >> \) c)
+ ...+>
+ }


> +@script:python depends on report@
> +p << r.p;
> +@@
> +
> +msg="WARNING: custom implementation of bsearch is error-prone. "
> +msg+="Consider using lib/bsearch.c or fix the midpoint calculation "
> +msg+="as 'm = l + (h - l) / 2;' to prevent the arithmetic overflow."
> +coccilib.report.print_report(p[0], msg)

The Linux coding style supports to put a long string literal also into a single line.
Thus I find such a message construction nicer without the extra variable âmsgâ.

Regards,
Markus