Section Subclause 6.2.5, paragraph 9, of the C Standard [ISO/IEC 9899:2011] states:
...
This noncompliant code example using atomic integers can result in unsigned integer overflow wrapping:
...
Code Block | ||||
---|---|---|---|---|
| ||||
atomic_int i; int ui_a; /* Initialize i, ui_a */ atomic_fetch_add(&i, ui_a); |
Compliant Solution
This compliant solution performs a postcondition test to ensure that the result of the unsigned addition operation to i
is not less than the operand ui_a
:
...
Code Block | ||||
---|---|---|---|---|
| ||||
atomic_int i; int ui_a; /* Initialize ui_a, i */ atomic_fetch_add(&i, ui_a); if (atomic_load(&i) < ui_a) { /* handle error condition */ } |
Exceptions
...