Coverage Report

Created: 2026-09-04 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dng_sdk/source/dng_safe_arithmetic.cpp
Line
Count
Source
1
#include "dng_safe_arithmetic.h"
2
3
#include <cmath>
4
#include <limits>
5
6
#include "dng_exceptions.h"
7
8
// Implementation of safe integer arithmetic follows guidelines from
9
// https://www.securecoding.cert.org/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap
10
// and
11
// https://www.securecoding.cert.org/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow
12
13
namespace {
14
15
// Template functions for safe arithmetic. These functions are not exposed in
16
// the header for the time being to avoid having to add checks for the various
17
// constraints on the template argument (e.g. that it is integral and possibly
18
// signed or unsigned only). This should be done using a static_assert(), but
19
// we want to be portable to pre-C++11 compilers.
20
21
// Returns the result of adding arg1 and arg2 if it will fit in a T (where T is
22
// a signed or unsigned integer type). Otherwise, throws a dng_exception with
23
// error code dng_error_unknown.
24
template <class T>
25
574M
T SafeAdd(T arg1, T arg2) {
26
  // The condition is reformulated relative to the version on
27
  // www.securecoding.cert.org to check for valid instead of invalid cases. It
28
  // seems safer to enumerate the valid cases (and potentially miss one) than
29
  // enumerate the invalid cases.
30
  // If T is an unsigned type, the second half of the condition always evaluates
31
  // to false and will presumably be compiled out by the compiler.
32
574M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
574M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
574M
    return arg1 + arg2;
35
574M
  } else {
36
1.52k
    ThrowProgramError("Arithmetic overflow");
37
1.52k
    abort();  // Never reached.
38
1.52k
  }
39
574M
}
dng_safe_arithmetic.cpp:int (anonymous namespace)::SafeAdd<int>(int, int)
Line
Count
Source
25
3.49M
T SafeAdd(T arg1, T arg2) {
26
  // The condition is reformulated relative to the version on
27
  // www.securecoding.cert.org to check for valid instead of invalid cases. It
28
  // seems safer to enumerate the valid cases (and potentially miss one) than
29
  // enumerate the invalid cases.
30
  // If T is an unsigned type, the second half of the condition always evaluates
31
  // to false and will presumably be compiled out by the compiler.
32
3.49M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
3.49M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
3.49M
    return arg1 + arg2;
35
3.49M
  } else {
36
0
    ThrowProgramError("Arithmetic overflow");
37
0
    abort();  // Never reached.
38
0
  }
39
3.49M
}
dng_safe_arithmetic.cpp:long (anonymous namespace)::SafeAdd<long>(long, long)
Line
Count
Source
25
559M
T SafeAdd(T arg1, T arg2) {
26
  // The condition is reformulated relative to the version on
27
  // www.securecoding.cert.org to check for valid instead of invalid cases. It
28
  // seems safer to enumerate the valid cases (and potentially miss one) than
29
  // enumerate the invalid cases.
30
  // If T is an unsigned type, the second half of the condition always evaluates
31
  // to false and will presumably be compiled out by the compiler.
32
559M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
559M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
559M
    return arg1 + arg2;
35
559M
  } else {
36
0
    ThrowProgramError("Arithmetic overflow");
37
0
    abort();  // Never reached.
38
0
  }
39
559M
}
dng_safe_arithmetic.cpp:unsigned int (anonymous namespace)::SafeAdd<unsigned int>(unsigned int, unsigned int)
Line
Count
Source
25
9.57M
T SafeAdd(T arg1, T arg2) {
26
  // The condition is reformulated relative to the version on
27
  // www.securecoding.cert.org to check for valid instead of invalid cases. It
28
  // seems safer to enumerate the valid cases (and potentially miss one) than
29
  // enumerate the invalid cases.
30
  // If T is an unsigned type, the second half of the condition always evaluates
31
  // to false and will presumably be compiled out by the compiler.
32
9.57M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
9.56M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
9.56M
    return arg1 + arg2;
35
9.56M
  } else {
36
1.49k
    ThrowProgramError("Arithmetic overflow");
37
1.49k
    abort();  // Never reached.
38
1.49k
  }
39
9.57M
}
dng_safe_arithmetic.cpp:unsigned long (anonymous namespace)::SafeAdd<unsigned long>(unsigned long, unsigned long)
Line
Count
Source
25
1.56M
T SafeAdd(T arg1, T arg2) {
26
  // The condition is reformulated relative to the version on
27
  // www.securecoding.cert.org to check for valid instead of invalid cases. It
28
  // seems safer to enumerate the valid cases (and potentially miss one) than
29
  // enumerate the invalid cases.
30
  // If T is an unsigned type, the second half of the condition always evaluates
31
  // to false and will presumably be compiled out by the compiler.
32
1.56M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
1.56M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
1.56M
    return arg1 + arg2;
35
1.56M
  } else {
36
34
    ThrowProgramError("Arithmetic overflow");
37
34
    abort();  // Never reached.
38
34
  }
39
1.56M
}
40
41
// Returns the result of multiplying arg1 and arg2 if it will fit in a T (where
42
// T is an unsigned integer type). Otherwise, throws a dng_exception with error
43
// code dng_error_unknown.
44
template <class T>
45
82.7M
T SafeUnsignedMult(T arg1, T arg2) {
46
82.7M
  if (arg1 == 0 || arg2 <= std::numeric_limits<T>::max() / arg1) {
47
82.7M
    return arg1 * arg2;
48
82.7M
  } else {
49
24.5k
    ThrowProgramError("Arithmetic overflow");
50
24.5k
    abort();  // Never reached.
51
24.5k
  }
52
82.7M
}
dng_safe_arithmetic.cpp:unsigned int (anonymous namespace)::SafeUnsignedMult<unsigned int>(unsigned int, unsigned int)
Line
Count
Source
45
82.2M
T SafeUnsignedMult(T arg1, T arg2) {
46
82.2M
  if (arg1 == 0 || arg2 <= std::numeric_limits<T>::max() / arg1) {
47
82.2M
    return arg1 * arg2;
48
82.2M
  } else {
49
24.5k
    ThrowProgramError("Arithmetic overflow");
50
24.5k
    abort();  // Never reached.
51
24.5k
  }
52
82.2M
}
dng_safe_arithmetic.cpp:unsigned long (anonymous namespace)::SafeUnsignedMult<unsigned long>(unsigned long, unsigned long)
Line
Count
Source
45
472k
T SafeUnsignedMult(T arg1, T arg2) {
46
472k
  if (arg1 == 0 || arg2 <= std::numeric_limits<T>::max() / arg1) {
47
472k
    return arg1 * arg2;
48
472k
  } else {
49
0
    ThrowProgramError("Arithmetic overflow");
50
0
    abort();  // Never reached.
51
0
  }
52
472k
}
53
54
}  // namespace
55
56
0
bool SafeInt32Add(std::int32_t arg1, std::int32_t arg2, std::int32_t *result) {
57
0
  try {
58
0
    *result = SafeInt32Add(arg1, arg2);
59
0
    return true;
60
0
  } catch (const dng_exception &) {
61
0
    return false;
62
0
  }
63
0
}
64
65
3.49M
std::int32_t SafeInt32Add(std::int32_t arg1, std::int32_t arg2) {
66
3.49M
  return SafeAdd<std::int32_t>(arg1, arg2);
67
3.49M
}
68
69
559M
std::int64_t SafeInt64Add(std::int64_t arg1, std::int64_t arg2) {
70
559M
  return SafeAdd<std::int64_t>(arg1, arg2);
71
559M
}
72
73
bool SafeUint32Add(std::uint32_t arg1, std::uint32_t arg2,
74
641k
                   std::uint32_t *result) {
75
641k
  try {
76
641k
    *result = SafeUint32Add(arg1, arg2);
77
641k
    return true;
78
641k
  } catch (const dng_exception &) {
79
694
    return false;
80
694
  }
81
641k
}
82
83
9.57M
std::uint32_t SafeUint32Add(std::uint32_t arg1, std::uint32_t arg2) {
84
9.57M
  return SafeAdd<std::uint32_t>(arg1, arg2);
85
9.57M
}
86
87
1.56M
std::uint64_t SafeUint64Add(std::uint64_t arg1, std::uint64_t arg2) {
88
1.56M
  return SafeAdd<std::uint64_t>(arg1, arg2);
89
1.56M
}
90
91
48.5M
bool SafeInt32Sub(std::int32_t arg1, std::int32_t arg2, std::int32_t *result) {
92
48.5M
  if ((arg2 >= 0 && arg1 >= std::numeric_limits<int32_t>::min() + arg2) ||
93
48.5M
      (arg2 < 0 && arg1 <= std::numeric_limits<int32_t>::max() + arg2)) {
94
48.5M
    *result = arg1 - arg2;
95
48.5M
    return true;
96
48.5M
  } else {
97
268
    return false;
98
268
  }
99
48.5M
}
100
101
1.17M
std::int32_t SafeInt32Sub(std::int32_t arg1, std::int32_t arg2) {
102
1.17M
  std::int32_t result = 0;
103
104
1.17M
  if (!SafeInt32Sub(arg1, arg2, &result)) {
105
0
    ThrowProgramError("Arithmetic overflow");
106
0
  }
107
108
1.17M
  return result;
109
1.17M
}
110
111
2.28M
std::uint32_t SafeUint32Sub(std::uint32_t arg1, std::uint32_t arg2) {
112
2.28M
  if (arg1 >= arg2) {
113
2.26M
    return arg1 - arg2;
114
2.26M
  } else {
115
20.7k
    ThrowProgramError("Arithmetic overflow");
116
20.7k
    abort();  // Never reached.
117
20.7k
  }
118
2.28M
}
119
120
bool SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2,
121
7.80M
                    std::uint32_t *result) {
122
7.80M
  try {
123
7.80M
    *result = SafeUint32Mult(arg1, arg2);
124
7.80M
    return true;
125
7.80M
  } catch (const dng_exception &) {
126
401
    return false;
127
401
  }
128
7.80M
}
129
130
bool SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2, std::uint32_t arg3,
131
0
                    std::uint32_t *result) {
132
0
  try {
133
0
    *result = SafeUint32Mult(arg1, arg2, arg3);
134
0
    return true;
135
0
  } catch (const dng_exception &) {
136
0
    return false;
137
0
  }
138
0
}
139
140
bool SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2, std::uint32_t arg3,
141
0
                    std::uint32_t arg4, std::uint32_t *result) {
142
0
  try {
143
0
    *result = SafeUint32Mult(arg1, arg2, arg3, arg4);
144
0
    return true;
145
0
  } catch (const dng_exception &) {
146
0
    return false;
147
0
  }
148
0
}
149
150
82.2M
std::uint32_t SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2) {
151
82.2M
  return SafeUnsignedMult<std::uint32_t>(arg1, arg2);
152
82.2M
}
153
154
std::uint32_t SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2,
155
1.08M
                             std::uint32_t arg3) {
156
1.08M
  return SafeUint32Mult(SafeUint32Mult(arg1, arg2), arg3);
157
1.08M
}
158
159
std::uint32_t SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2,
160
980k
                             std::uint32_t arg3, std::uint32_t arg4) {
161
980k
  return SafeUint32Mult(SafeUint32Mult(arg1, arg2, arg3), arg4);
162
980k
}
163
164
2.53k
std::int32_t SafeInt32Mult(std::int32_t arg1, std::int32_t arg2) {
165
2.53k
  const std::int64_t tmp =
166
2.53k
      static_cast<std::int64_t>(arg1) * static_cast<std::int64_t>(arg2);
167
2.53k
  if (tmp >= std::numeric_limits<std::int32_t>::min() &&
168
2.46k
      tmp <= std::numeric_limits<std::int32_t>::max()) {
169
2.34k
    return static_cast<std::int32_t>(tmp);
170
2.34k
  } else {
171
189
    ThrowProgramError("Arithmetic overflow");
172
189
    abort();
173
189
  }
174
2.53k
}
175
176
472k
std::size_t SafeSizetMult(std::size_t arg1, std::size_t arg2) {
177
472k
  return SafeUnsignedMult<std::size_t>(arg1, arg2);
178
472k
}
179
180
namespace dng_internal {
181
182
0
std::int64_t SafeInt64MultSlow(std::int64_t arg1, std::int64_t arg2) {
183
0
  bool overflow = true;
184
185
0
  if (arg1 > 0) {
186
0
    if (arg2 > 0) {
187
0
      overflow = (arg1 > std::numeric_limits<std::int64_t>::max() / arg2);
188
0
    } else {
189
0
      overflow = (arg2 < std::numeric_limits<std::int64_t>::min() / arg1);
190
0
    }
191
0
  } else {
192
0
    if (arg2 > 0) {
193
0
      overflow = (arg1 < std::numeric_limits<std::int64_t>::min() / arg2);
194
0
    } else {
195
0
      overflow = (arg1 != 0 &&
196
0
                  arg2 < std::numeric_limits<std::int64_t>::max() / arg1);
197
0
    }
198
0
  }
199
200
0
  if (overflow) {
201
0
    ThrowProgramError("Arithmetic overflow");
202
0
    abort();  // Never reached.
203
0
  } else {
204
0
    return arg1 * arg2;
205
0
  }
206
0
}
207
208
}  // namespace dng_internal
209
210
291k
std::uint32_t SafeUint32DivideUp(std::uint32_t arg1, std::uint32_t arg2) {
211
  // It might seem more intuitive to implement this function simply as
212
  //
213
  //   return arg2 == 0 ? 0 : (arg1 + arg2 - 1) / arg2;
214
  //
215
  // but the expression "arg1 + arg2" can wrap around.
216
217
291k
  if (arg2 == 0) {
218
0
    ThrowProgramError("Division by zero");
219
0
    abort();  // Never reached.
220
291k
  } else if (arg1 == 0) {
221
    // If arg1 is zero, return zero to avoid wraparound in the expression
222
    //   "arg1 - 1" below.
223
702
    return 0;
224
290k
  } else {
225
290k
    return (arg1 - 1) / arg2 + 1;
226
290k
  }
227
291k
}
228
229
bool RoundUpUint32ToMultiple(std::uint32_t val, std::uint32_t multiple_of,
230
2.42M
                             std::uint32_t *result) {
231
2.42M
  try {
232
2.42M
    *result = RoundUpUint32ToMultiple(val, multiple_of);
233
2.42M
    return true;
234
2.42M
  } catch (const dng_exception &) {
235
0
    return false;
236
0
  }
237
2.42M
}
238
239
std::uint32_t RoundUpUint32ToMultiple(std::uint32_t val,
240
2.47M
                                      std::uint32_t multiple_of) {
241
2.47M
  if (multiple_of == 0) {
242
0
    ThrowProgramError("multiple_of is zero in RoundUpUint32ToMultiple");
243
0
  }
244
245
2.47M
  const std::uint32_t remainder = val % multiple_of;
246
2.47M
  if (remainder == 0) {
247
1.60M
    return val;
248
1.60M
  } else {
249
867k
    return SafeUint32Add(val, multiple_of - remainder);
250
867k
  }
251
2.47M
}
252
253
8.19M
bool ConvertUint32ToInt32(std::uint32_t val, std::int32_t *result) {
254
8.19M
  try {
255
8.19M
    *result = ConvertUint32ToInt32(val);
256
8.19M
    return true;
257
8.19M
  } catch (const dng_exception &) {
258
22
    return false;
259
22
  }
260
8.19M
}
261
262
9.39M
std::int32_t ConvertUint32ToInt32(std::uint32_t val) {
263
9.39M
  const std::uint32_t kInt32MaxAsUint32 =
264
9.39M
      static_cast<std::uint32_t>(std::numeric_limits<std::int32_t>::max());
265
266
9.39M
  if (val <= kInt32MaxAsUint32) {
267
9.39M
    return static_cast<std::int32_t>(val);
268
9.39M
  } else {
269
34
    ThrowProgramError("Arithmetic overflow");
270
34
    abort();  // Never reached.
271
34
  }
272
9.39M
}
273
274
181k
std::int32_t ConvertDoubleToInt32(double val) {
275
181k
  const double kMin =
276
181k
      static_cast<double>(std::numeric_limits<std::int32_t>::min());
277
181k
  const double kMax =
278
181k
      static_cast<double>(std::numeric_limits<std::int32_t>::max());
279
  // NaNs will fail this test; they always compare false.
280
181k
  if (val > kMin - 1.0 && val < kMax + 1.0) {
281
178k
    return static_cast<std::int32_t>(val);
282
178k
  } else {
283
2.88k
    ThrowProgramError("Argument not in range in ConvertDoubleToInt32");
284
2.88k
    abort();  // Never reached.
285
2.88k
  }
286
181k
}
287
288
241M
std::uint32_t ConvertDoubleToUint32(double val) {
289
241M
  const double kMax =
290
241M
      static_cast<double>(std::numeric_limits<std::uint32_t>::max());
291
  // NaNs will fail this test; they always compare false.
292
241M
  if (val >= 0.0 && val < kMax + 1.0) {
293
241M
    return static_cast<std::uint32_t>(val);
294
241M
  } else {
295
859
    ThrowProgramError("Argument not in range in ConvertDoubleToUint32");
296
859
    abort();  // Never reached.
297
859
  }
298
241M
}
299
300
7.73k
float ConvertDoubleToFloat(double val) {
301
7.73k
  const double kMax = std::numeric_limits<float>::max();
302
7.73k
  if (val > kMax) {
303
191
    return std::numeric_limits<float>::infinity();
304
7.54k
  } else if (val < -kMax) {
305
395
    return -std::numeric_limits<float>::infinity();
306
7.14k
  } else {
307
    // The cases that end up here are:
308
    // - values in [-kMax, kMax]
309
    // - NaN (because it always compares false)
310
7.14k
    return static_cast<float>(val);
311
7.14k
  }
312
7.73k
}