Botan  2.9.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  ErrorType error_type() const noexcept override { return ErrorType::InternalError; }
24  };
25 
26 inline size_t checked_add(size_t x, size_t y, const char* file, int line)
27  {
28  // TODO: use __builtin_x_overflow on GCC and Clang
29  size_t z = x + y;
30  if(z < x)
31  {
32  throw Integer_Overflow_Detected(file, line);
33  }
34  return z;
35  }
36 
37 #define BOTAN_CHECKED_ADD(x,y) checked_add(x,y,__FILE__,__LINE__)
38 
39 }
40 
41 #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:26
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
ErrorType error_type() const noexcept override
Definition: safeint.h:23
Definition: bigint.h:1125
ErrorType
Definition: exceptn.h:20
Definition: alg_id.cpp:13
std::string to_string(const secure_vector< uint8_t > &bytes)
Definition: stl_util.h:25