Coverage Report

Created: 2021-04-07 06:07

/src/botan/build/include/botan/internal/safeint.h
Line
Count
Source (jump to first uncovered line)
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 final : public Exception
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
0
         {}
22
23
0
      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
2.24M
   {
28
   // TODO: use __builtin_x_overflow on GCC and Clang
29
2.24M
   size_t z = x + y;
30
2.24M
   if(z < x)
31
0
      {
32
0
      throw Integer_Overflow_Detected(file, line);
33
0
      }
34
2.24M
   return z;
35
2.24M
   }
36
37
template<typename RT, typename AT>
38
RT checked_cast_to(AT i)
39
   {
40
   RT c = static_cast<RT>(i);
41
   if(i != static_cast<AT>(c))
42
      throw Internal_Error("Error during integer conversion");
43
   return c;
44
   }
45
46
2.26M
#define BOTAN_CHECKED_ADD(x,y) checked_add(x,y,__FILE__,__LINE__)
47
48
}
49
50
#endif