Coverage Report

Created: 2026-01-09 06:56

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
766M
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
766M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
766M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
766M
    return arg1 + arg2;
35
766M
  } else {
36
2.00k
    ThrowProgramError("Arithmetic overflow");
37
2.00k
    abort();  // Never reached.
38
2.00k
  }
39
766M
}
dng_safe_arithmetic.cpp:int (anonymous namespace)::SafeAdd<int>(int, int)
Line
Count
Source
25
5.54M
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
5.54M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
5.54M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
5.54M
    return arg1 + arg2;
35
5.54M
  } else {
36
0
    ThrowProgramError("Arithmetic overflow");
37
0
    abort();  // Never reached.
38
0
  }
39
5.54M
}
dng_safe_arithmetic.cpp:long (anonymous namespace)::SafeAdd<long>(long, long)
Line
Count
Source
25
752M
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
752M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
752M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
752M
    return arg1 + arg2;
35
752M
  } else {
36
0
    ThrowProgramError("Arithmetic overflow");
37
0
    abort();  // Never reached.
38
0
  }
39
752M
}
dng_safe_arithmetic.cpp:unsigned int (anonymous namespace)::SafeAdd<unsigned int>(unsigned int, unsigned int)
Line
Count
Source
25
7.61M
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
7.61M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
7.61M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
7.61M
    return arg1 + arg2;
35
7.61M
  } else {
36
1.93k
    ThrowProgramError("Arithmetic overflow");
37
1.93k
    abort();  // Never reached.
38
1.93k
  }
39
7.61M
}
dng_safe_arithmetic.cpp:unsigned long (anonymous namespace)::SafeAdd<unsigned long>(unsigned long, unsigned long)
Line
Count
Source
25
462k
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
462k
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
462k
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
462k
    return arg1 + arg2;
35
462k
  } else {
36
71
    ThrowProgramError("Arithmetic overflow");
37
71
    abort();  // Never reached.
38
71
  }
39
462k
}
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
121M
T SafeUnsignedMult(T arg1, T arg2) {
46
121M
  if (arg1 == 0 || arg2 <= std::numeric_limits<T>::max() / arg1) {
47
121M
    return arg1 * arg2;
48
121M
  } else {
49
58.6k
    ThrowProgramError("Arithmetic overflow");
50
58.6k
    abort();  // Never reached.
51
58.6k
  }
52
121M
}
dng_safe_arithmetic.cpp:unsigned int (anonymous namespace)::SafeUnsignedMult<unsigned int>(unsigned int, unsigned int)
Line
Count
Source
45
121M
T SafeUnsignedMult(T arg1, T arg2) {
46
121M
  if (arg1 == 0 || arg2 <= std::numeric_limits<T>::max() / arg1) {
47
121M
    return arg1 * arg2;
48
121M
  } else {
49
58.6k
    ThrowProgramError("Arithmetic overflow");
50
58.6k
    abort();  // Never reached.
51
58.6k
  }
52
121M
}
dng_safe_arithmetic.cpp:unsigned long (anonymous namespace)::SafeUnsignedMult<unsigned long>(unsigned long, unsigned long)
Line
Count
Source
45
578k
T SafeUnsignedMult(T arg1, T arg2) {
46
578k
  if (arg1 == 0 || arg2 <= std::numeric_limits<T>::max() / arg1) {
47
578k
    return arg1 * arg2;
48
578k
  } else {
49
0
    ThrowProgramError("Arithmetic overflow");
50
0
    abort();  // Never reached.
51
0
  }
52
578k
}
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
5.54M
std::int32_t SafeInt32Add(std::int32_t arg1, std::int32_t arg2) {
66
5.54M
  return SafeAdd<std::int32_t>(arg1, arg2);
67
5.54M
}
68
69
752M
std::int64_t SafeInt64Add(std::int64_t arg1, std::int64_t arg2) {
70
752M
  return SafeAdd<std::int64_t>(arg1, arg2);
71
752M
}
72
73
bool SafeUint32Add(std::uint32_t arg1, std::uint32_t arg2,
74
846k
                   std::uint32_t *result) {
75
846k
  try {
76
846k
    *result = SafeUint32Add(arg1, arg2);
77
846k
    return true;
78
846k
  } catch (const dng_exception &) {
79
898
    return false;
80
898
  }
81
846k
}
82
83
7.61M
std::uint32_t SafeUint32Add(std::uint32_t arg1, std::uint32_t arg2) {
84
7.61M
  return SafeAdd<std::uint32_t>(arg1, arg2);
85
7.61M
}
86
87
462k
std::uint64_t SafeUint64Add(std::uint64_t arg1, std::uint64_t arg2) {
88
462k
  return SafeAdd<std::uint64_t>(arg1, arg2);
89
462k
}
90
91
68.5M
bool SafeInt32Sub(std::int32_t arg1, std::int32_t arg2, std::int32_t *result) {
92
68.5M
  if ((arg2 >= 0 && arg1 >= std::numeric_limits<int32_t>::min() + arg2) ||
93
68.5M
      (arg2 < 0 && arg1 <= std::numeric_limits<int32_t>::max() + arg2)) {
94
68.5M
    *result = arg1 - arg2;
95
68.5M
    return true;
96
68.5M
  } else {
97
373
    return false;
98
373
  }
99
68.5M
}
100
101
1.85M
std::int32_t SafeInt32Sub(std::int32_t arg1, std::int32_t arg2) {
102
1.85M
  std::int32_t result = 0;
103
104
1.85M
  if (!SafeInt32Sub(arg1, arg2, &result)) {
105
0
    ThrowProgramError("Arithmetic overflow");
106
0
  }
107
108
1.85M
  return result;
109
1.85M
}
110
111
3.09M
std::uint32_t SafeUint32Sub(std::uint32_t arg1, std::uint32_t arg2) {
112
3.09M
  if (arg1 >= arg2) {
113
3.08M
    return arg1 - arg2;
114
3.08M
  } else {
115
6.65k
    ThrowProgramError("Arithmetic overflow");
116
6.65k
    abort();  // Never reached.
117
6.65k
  }
118
3.09M
}
119
120
bool SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2,
121
8.43M
                    std::uint32_t *result) {
122
8.43M
  try {
123
8.43M
    *result = SafeUint32Mult(arg1, arg2);
124
8.43M
    return true;
125
8.43M
  } catch (const dng_exception &) {
126
490
    return false;
127
490
  }
128
8.43M
}
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
121M
std::uint32_t SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2) {
151
121M
  return SafeUnsignedMult<std::uint32_t>(arg1, arg2);
152
121M
}
153
154
std::uint32_t SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2,
155
1.86M
                             std::uint32_t arg3) {
156
1.86M
  return SafeUint32Mult(SafeUint32Mult(arg1, arg2), arg3);
157
1.86M
}
158
159
std::uint32_t SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2,
160
1.72M
                             std::uint32_t arg3, std::uint32_t arg4) {
161
1.72M
  return SafeUint32Mult(SafeUint32Mult(arg1, arg2, arg3), arg4);
162
1.72M
}
163
164
2.78k
std::int32_t SafeInt32Mult(std::int32_t arg1, std::int32_t arg2) {
165
2.78k
  const std::int64_t tmp =
166
2.78k
      static_cast<std::int64_t>(arg1) * static_cast<std::int64_t>(arg2);
167
2.78k
  if (tmp >= std::numeric_limits<std::int32_t>::min() &&
168
2.66k
      tmp <= std::numeric_limits<std::int32_t>::max()) {
169
2.51k
    return static_cast<std::int32_t>(tmp);
170
2.51k
  } else {
171
265
    ThrowProgramError("Arithmetic overflow");
172
265
    abort();
173
265
  }
174
2.78k
}
175
176
578k
std::size_t SafeSizetMult(std::size_t arg1, std::size_t arg2) {
177
578k
  return SafeUnsignedMult<std::size_t>(arg1, arg2);
178
578k
}
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
368k
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
368k
  if (arg2 == 0) {
218
0
    ThrowProgramError("Division by zero");
219
0
    abort();  // Never reached.
220
368k
  } else if (arg1 == 0) {
221
    // If arg1 is zero, return zero to avoid wraparound in the expression
222
    //   "arg1 - 1" below.
223
671
    return 0;
224
367k
  } else {
225
367k
    return (arg1 - 1) / arg2 + 1;
226
367k
  }
227
368k
}
228
229
bool RoundUpUint32ToMultiple(std::uint32_t val, std::uint32_t multiple_of,
230
3.84M
                             std::uint32_t *result) {
231
3.84M
  try {
232
3.84M
    *result = RoundUpUint32ToMultiple(val, multiple_of);
233
3.84M
    return true;
234
3.84M
  } catch (const dng_exception &) {
235
0
    return false;
236
0
  }
237
3.84M
}
238
239
std::uint32_t RoundUpUint32ToMultiple(std::uint32_t val,
240
3.94M
                                      std::uint32_t multiple_of) {
241
3.94M
  if (multiple_of == 0) {
242
0
    ThrowProgramError("multiple_of is zero in RoundUpUint32ToMultiple");
243
0
  }
244
245
3.94M
  const std::uint32_t remainder = val % multiple_of;
246
3.94M
  if (remainder == 0) {
247
2.79M
    return val;
248
2.79M
  } else {
249
1.15M
    return SafeUint32Add(val, multiple_of - remainder);
250
1.15M
  }
251
3.94M
}
252
253
12.7M
bool ConvertUint32ToInt32(std::uint32_t val, std::int32_t *result) {
254
12.7M
  try {
255
12.7M
    *result = ConvertUint32ToInt32(val);
256
12.7M
    return true;
257
12.7M
  } catch (const dng_exception &) {
258
28
    return false;
259
28
  }
260
12.7M
}
261
262
14.7M
std::int32_t ConvertUint32ToInt32(std::uint32_t val) {
263
14.7M
  const std::uint32_t kInt32MaxAsUint32 =
264
14.7M
      static_cast<std::uint32_t>(std::numeric_limits<std::int32_t>::max());
265
266
14.7M
  if (val <= kInt32MaxAsUint32) {
267
14.7M
    return static_cast<std::int32_t>(val);
268
14.7M
  } else {
269
41
    ThrowProgramError("Arithmetic overflow");
270
41
    abort();  // Never reached.
271
41
  }
272
14.7M
}
273
274
184k
std::int32_t ConvertDoubleToInt32(double val) {
275
184k
  const double kMin =
276
184k
      static_cast<double>(std::numeric_limits<std::int32_t>::min());
277
184k
  const double kMax =
278
184k
      static_cast<double>(std::numeric_limits<std::int32_t>::max());
279
  // NaNs will fail this test; they always compare false.
280
184k
  if (val > kMin - 1.0 && val < kMax + 1.0) {
281
181k
    return static_cast<std::int32_t>(val);
282
181k
  } else {
283
2.79k
    ThrowProgramError("Argument not in range in ConvertDoubleToInt32");
284
2.79k
    abort();  // Never reached.
285
2.79k
  }
286
184k
}
287
288
844M
std::uint32_t ConvertDoubleToUint32(double val) {
289
844M
  const double kMax =
290
844M
      static_cast<double>(std::numeric_limits<std::uint32_t>::max());
291
  // NaNs will fail this test; they always compare false.
292
844M
  if (val >= 0.0 && val < kMax + 1.0) {
293
844M
    return static_cast<std::uint32_t>(val);
294
844M
  } else {
295
822
    ThrowProgramError("Argument not in range in ConvertDoubleToUint32");
296
822
    abort();  // Never reached.
297
822
  }
298
844M
}
299
300
13.6k
float ConvertDoubleToFloat(double val) {
301
13.6k
  const double kMax = std::numeric_limits<float>::max();
302
13.6k
  if (val > kMax) {
303
249
    return std::numeric_limits<float>::infinity();
304
13.3k
  } else if (val < -kMax) {
305
665
    return -std::numeric_limits<float>::infinity();
306
12.7k
  } else {
307
    // The cases that end up here are:
308
    // - values in [-kMax, kMax]
309
    // - NaN (because it always compares false)
310
12.7k
    return static_cast<float>(val);
311
12.7k
  }
312
13.6k
}