YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/safe_math.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
// Inline functions for doing overflow-safe operations on integers.
33
// These should be used when doing bounds checks on user-provided data,
34
// for example.
35
// See also: https://www.securecoding.cert.org/confluence/display/cplusplus/INT32-CPP.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow
36
#ifndef YB_UTIL_SAFE_MATH_H
37
#define YB_UTIL_SAFE_MATH_H
38
39
#include "yb/gutil/mathlimits.h"
40
41
namespace yb {
42
43
namespace safe_math_internal {
44
45
// Template which is specialized for signed and unsigned types separately.
46
template<typename Type, bool is_signed>
47
struct WithOverflowCheck {
48
};
49
50
51
// Specialization for signed types.
52
template<typename Type>
53
struct WithOverflowCheck<Type, true> {
54
4
  static inline Type Add(Type a, Type b, bool *overflowed) {
55
    // Implementation from the CERT article referenced in the file header.
56
4
    *overflowed = (((a > 0) && (b > 0) && (a > (MathLimits<Type>::kMax - b))) ||
57
3
                   ((a < 0) && (b < 0) && (a < (MathLimits<Type>::kMin - b))));
58
4
    return a + b;
59
4
  }
60
};
61
62
// Specialization for unsigned types.
63
template<typename Type>
64
struct WithOverflowCheck<Type, false> {
65
2
  static inline Type Add(Type a, Type b, bool *overflowed) {
66
2
    Type ret = a + b;
67
2
    *overflowed = ret < a;
68
2
    return a + b;
69
2
  }
70
};
71
72
} // namespace safe_math_internal
73
74
// Add 'a' and 'b', and set *overflowed to true if overflow occurred.
75
template<typename Type>
76
6
inline Type AddWithOverflowCheck(Type a, Type b, bool *overflowed) {
77
  // Pick the right specialization based on whether Type is signed.
78
6
  typedef safe_math_internal::WithOverflowCheck<Type, MathLimits<Type>::kIsSigned> my_struct;
79
6
  return my_struct::Add(a, b, overflowed);
80
6
}
_ZN2yb20AddWithOverflowCheckIiEET_S1_S1_Pb
Line
Count
Source
76
4
inline Type AddWithOverflowCheck(Type a, Type b, bool *overflowed) {
77
  // Pick the right specialization based on whether Type is signed.
78
4
  typedef safe_math_internal::WithOverflowCheck<Type, MathLimits<Type>::kIsSigned> my_struct;
79
4
  return my_struct::Add(a, b, overflowed);
80
4
}
_ZN2yb20AddWithOverflowCheckIjEET_S1_S1_Pb
Line
Count
Source
76
2
inline Type AddWithOverflowCheck(Type a, Type b, bool *overflowed) {
77
  // Pick the right specialization based on whether Type is signed.
78
2
  typedef safe_math_internal::WithOverflowCheck<Type, MathLimits<Type>::kIsSigned> my_struct;
79
2
  return my_struct::Add(a, b, overflowed);
80
2
}
81
82
} // namespace yb
83
#endif // YB_UTIL_SAFE_MATH_H