Botan 2.19.5 Crypto and TLS for C&

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 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 {}
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
Botan::Exception
Definition: exceptn.h:86
Botan::Integer_Overflow_Detected
Definition: safeint.h:17
Botan::Integer_Overflow_Detected::Integer_Overflow_Detected
Integer_Overflow_Detected(const std::string &file, int line)
Definition: safeint.h:19
Botan::Integer_Overflow_Detected::error_type
ErrorType error_type() const noexcept override
Definition: safeint.h:23
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:29
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Botan
Definition: alg_id.cpp:13
Botan::ErrorType
ErrorType
Definition: exceptn.h:20
Botan::ErrorType::InternalError
@ InternalError
Botan::checked_add
size_t checked_add(size_t x, size_t y, const char *file, int line)
Definition: safeint.h:26
Botan::to_string
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
std
Definition: bigint.h:1155