bitcoin/bitcoin — pull request #2604 by zeldovich (2013-05-01 20:49:23+00:00)
Title: EvalScript: avoid relying on undefined behavior for overflow check

In C/C++, signed integer overflow is undefined behavior, and some
compilers (such as gcc) will optimize away checks like the one that
was present in EvalScript; specifically:

int nBegin = ...;
int nEnd = nBegin + size;
if (nBegin < 0 || nEnd < nBegin)

will get compiled into:

if (nBegin < 0 || size < 0)

This patch changes the overflow check to avoid relying on the behavior
of signed integer overflow, by checking for size > INT_MAX - nBegin
(and computing nEnd after the check).