01.07.2016 Views

SEI CERT C Coding Standard

tqcylJ

tqcylJ

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Integers (INT) - INT32-C. Ensure that operations on signed integers do not result in overflow<br />

extern size_t popcount(uintmax_t);<br />

#define PRECISION(umax_value) popcount(umax_value)<br />

void func(signed long si_a, signed long si_b) {<br />

signed long result;<br />

if ((si_a < 0) || (si_b < 0) ||<br />

(si_b >= PRECISION(ULONG_MAX)) ||<br />

(si_a > (LONG_MAX >> si_b))) {<br />

/* Handle error */<br />

} else {<br />

result = si_a

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!