Botan 1.10.17

safeint.h
Go to the documentation of this file.
1 /*
2 * Safe(r) Integer Handling
3 * (C) 2016 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #ifndef BOTAN_UTILS_SAFE_INT_H__
9 #define BOTAN_UTILS_SAFE_INT_H__
10
11 #include <botan/exceptn.h>
12 #include <string>
13
14 namespace Botan {
15
16 class Integer_Overflow_Detected : public Exception
17 {
18 public:
19 Integer_Overflow_Detected(const std::string& file, int line) :
20 Exception("Integer overflow detected at " + file + ":" + to_string(line))
21 {}
22 };
23
24 inline size_t checked_add(size_t x, size_t y, const char* file, int line)
25 {
26 // TODO: use __builtin_x_overflow on GCC and Clang
27 size_t z = x + y;
28 if(z < x)
29 {
30 throw Integer_Overflow_Detected(file, line);
31 }
32 return z;
33 }
34
35 #define BOTAN_CHECKED_ADD(x,y) checked_add(x,y,__FILE__,__LINE__)
36
37 }
38
39 #endif
Botan::Integer_Overflow_Detected::Integer_Overflow_Detected
Integer_Overflow_Detected(const std::string &file, int line)
Definition: safeint.h:19
Botan::checked_add
size_t checked_add(size_t x, size_t y, const char *file, int line)
Definition: safeint.h:24
Botan::Integer_Overflow_Detected
Definition: safeint.h:16
Botan::Exception
std::runtime_error Exception
Definition: exceptn.h:19
Botan
Definition: algo_base.h:14
Botan::to_string
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42