...
This compliant solution eliminates the possibility of shifting by more bits than exist in the left hand operand. The PRECISION() macro is defined in INT35-C. Use correct integer precisions:
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <limits.h>
#include <stddef.h>
#include <inttypes.h>
extern size_t popcount(uintmax_t);
#define PRECISION(x) popcount(x)
void func(unsigned int ui_a, unsigned int ui_b) {
unsigned int uresult = 0;
if (ui_b >= PRECISION(UINT_MAX)) {
/* Handle error condition */
} else {
uresult = ui_a << ui_b;
}
/* ... */
} |
Modulo The PRECISION() macro is defined in INT35-C. Use correct integer precisions. Modulo behavior resulting from left-shifting an unsigned integer type is permitted by exception INT30-EX3 to INT30-C. Ensure that unsigned integer operations do not wrap.
...