Coverage Report

Created: 2020-05-23 13:54

/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 BOTAN_PUBLIC_API(2,0) 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.19M
   {
28
2.19M
   // TODO: use __builtin_x_overflow on GCC and Clang
29
2.19M
   size_t z = x + y;
30
2.19M
   if(z < x)
31
0
      {
32
0
      throw Integer_Overflow_Detected(file, line);
33
0
      }
34
2.19M
   return z;
35
2.19M
   }
36
37
2.21M
#define BOTAN_CHECKED_ADD(x,y) checked_add(x,y,__FILE__,__LINE__)
38
39
}
40
41
#endif