// SkSafeMath always check that a series of operations do not overflow. // This must be correct for all platforms, because this is a check for safety at runtime.
class SkSafeMath {
public:
SkSafeMath() = default;
size_t add(size_t x, size_t y) {
size_t result = x + y;
fOK &= result >= x; return result;
}
/** *Returna+b,unlessthisresultisanoverflow/underflow.Inthosecases,fOKwill *besettofalse,anditisundefinedwhatthisreturns.
*/ int addInt(int a, int b) {
static_assert(sizeof(int) == 4, "int is not 4 bytes"); if (b < 0 && a < std::numeric_limits<int>::min() - b) {
fOK = false; return a;
} elseif (b > 0 && a > std::numeric_limits<int>::max() - b) {
fOK = false; return a;
} return a + b;
}
int subInt(int a, int b) { if (b == std::numeric_limits<int>::min()) {
fOK = false; return a;
} return addInt(a, -b);
}
int mulInt(int x, int y) {
int64_t result = (int64_t)x * (int64_t)y; if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
fOK = false; return x;
} return (int)result;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.