Botan  2.4.0
Crypto and TLS for C++11
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 
17  {
18  public:
19  Integer_Overflow_Detected(const std::string& file, int line) :
20  Exception("Integer overflow detected at " + file + ":" + std::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
Integer_Overflow_Detected(const std::string &file, int line)
Definition: safeint.h:19
size_t checked_add(size_t x, size_t y, const char *file, int line)
Definition: safeint.h:24
Definition: bigint.h:635
Definition: alg_id.cpp:13
std::string to_string(const secure_vector< uint8_t > &bytes)
Definition: stl_util.h:25