| 1 |
.. title:: clang-tidy - cert-err34-c |
| 2 |
|
| 3 |
cert-err34-c |
| 4 |
============ |
| 5 |
|
| 6 |
This check flags calls to string-to-number conversion functions that do not |
| 7 |
verify the validity of the conversion, such as ``atoi()`` or ``scanf()``. It |
| 8 |
does not flag calls to ``strtol()``, or other, related conversion functions that |
| 9 |
do perform better error checking. |
| 10 |
|
| 11 |
.. code-block:: c |
| 12 |
|
| 13 |
#include <stdlib.h> |
| 14 |
|
| 15 |
void func(const char *buff) { |
| 16 |
int si; |
| 17 |
|
| 18 |
if (buff) { |
| 19 |
si = atoi(buff); /* 'atoi' used to convert a string to an integer, but function will |
| 20 |
not report conversion errors; consider using 'strtol' instead. */ |
| 21 |
} else { |
| 22 |
/* Handle error */ |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
This check corresponds to the CERT C Coding Standard rule |
| 27 |
`ERR34-C. Detect errors when converting a string to a number |
| 28 |
<https://www.securecoding.cert.org/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number>`_. |