| 1 |
.. title:: clang-tidy - android-comparison-in-temp-failure-retry |
| 2 |
|
| 3 |
android-comparison-in-temp-failure-retry |
| 4 |
======================================== |
| 5 |
|
| 6 |
Diagnoses comparisons that appear to be incorrectly placed in the argument to |
| 7 |
the ``TEMP_FAILURE_RETRY`` macro. Having such a use is incorrect in the vast |
| 8 |
majority of cases, and will often silently defeat the purpose of the |
| 9 |
``TEMP_FAILURE_RETRY`` macro. |
| 10 |
|
| 11 |
For context, ``TEMP_FAILURE_RETRY`` is `a convenience macro |
| 12 |
<https://www.gnu.org/software/libc/manual/html_node/Interrupted-Primitives.html>`_ |
| 13 |
provided by both glibc and Bionic. Its purpose is to repeatedly run a syscall |
| 14 |
until it either succeeds, or fails for reasons other than being interrupted. |
| 15 |
|
| 16 |
Example buggy usage looks like: |
| 17 |
|
| 18 |
.. code-block:: c |
| 19 |
|
| 20 |
char cs[1]; |
| 21 |
while (TEMP_FAILURE_RETRY(read(STDIN_FILENO, cs, sizeof(cs)) != 0)) { |
| 22 |
// Do something with cs. |
| 23 |
} |
| 24 |
|
| 25 |
Because TEMP_FAILURE_RETRY will check for whether the result *of the comparison* |
| 26 |
is ``-1``, and retry if so. |
| 27 |
|
| 28 |
If you encounter this, the fix is simple: lift the comparison out of the |
| 29 |
``TEMP_FAILURE_RETRY`` argument, like so: |
| 30 |
|
| 31 |
.. code-block:: c |
| 32 |
|
| 33 |
char cs[1]; |
| 34 |
while (TEMP_FAILURE_RETRY(read(STDIN_FILENO, cs, sizeof(cs))) != 0) { |
| 35 |
// Do something with cs. |
| 36 |
} |
| 37 |
|
| 38 |
Options |
| 39 |
------- |
| 40 |
|
| 41 |
.. option:: RetryMacros |
| 42 |
|
| 43 |
A comma-separated list of the names of retry macros to be checked. |