Coverage Report

Created: 2026-04-12 06:26

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
375M
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
375M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
375M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
375M
    return arg1 + arg2;
35
375M
  } else {
36
1.34k
    ThrowProgramError("Arithmetic overflow");
37
1.34k
    abort();  // Never reached.
38
1.34k
  }
39
375M
}
dng_safe_arithmetic.cpp:int (anonymous namespace)::SafeAdd<int>(int, int)
Line
Count
Source
25
4.33M
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
4.33M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
4.33M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
4.33M
    return arg1 + arg2;
35
4.33M
  } else {
36
0
    ThrowProgramError("Arithmetic overflow");
37
0
    abort();  // Never reached.
38
0
  }
39
4.33M
}
dng_safe_arithmetic.cpp:long (anonymous namespace)::SafeAdd<long>(long, long)
Line
Count
Source
25
365M
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
365M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
365M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
365M
    return arg1 + arg2;
35
365M
  } else {
36
0
    ThrowProgramError("Arithmetic overflow");
37
0
    abort();  // Never reached.
38
0
  }
39
365M
}
dng_safe_arithmetic.cpp:unsigned int (anonymous namespace)::SafeAdd<unsigned int>(unsigned int, unsigned int)
Line
Count
Source
25
5.74M
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.74M
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
5.74M
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
5.74M
    return arg1 + arg2;
35
5.74M
  } else {
36
1.32k
    ThrowProgramError("Arithmetic overflow");
37
1.32k
    abort();  // Never reached.
38
1.32k
  }
39
5.74M
}
dng_safe_arithmetic.cpp:unsigned long (anonymous namespace)::SafeAdd<unsigned long>(unsigned long, unsigned long)
Line
Count
Source
25
468k
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
468k
  if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
33
468k
      (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
34
468k
    return arg1 + arg2;
35
468k
  } else {
36
20
    ThrowProgramError("Arithmetic overflow");
37
20
    abort();  // Never reached.
38
20
  }
39
468k
}
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
60.9M
T SafeUnsignedMult(T arg1, T arg2) {
46
60.9M
  if (arg1 == 0 || arg2 <= std::numeric_limits<T>::max() / arg1) {
47
60.9M
    return arg1 * arg2;
48
60.9M
  } else {
49
25.8k
    ThrowProgramError("Arithmetic overflow");
50
25.8k
    abort();  // Never reached.
51
25.8k
  }
52
60.9M
}
dng_safe_arithmetic.cpp:unsigned int (anonymous namespace)::SafeUnsignedMult<unsigned int>(unsigned int, unsigned int)
Line
Count
Source
45
60.5M
T SafeUnsignedMult(T arg1, T arg2) {
46
60.5M
  if (arg1 == 0 || arg2 <= std::numeric_limits<T>::max() / arg1) {
47
60.5M
    return arg1 * arg2;
48
60.5M
  } else {
49
25.8k
    ThrowProgramError("Arithmetic overflow");
50
25.8k
    abort();  // Never reached.
51
25.8k
  }
52
60.5M
}
dng_safe_arithmetic.cpp:unsigned long (anonymous namespace)::SafeUnsignedMult<unsigned long>(unsigned long, unsigned long)
Line
Count
Source
45
355k
T SafeUnsignedMult(T arg1, T arg2) {
46
355k
  if (arg1 == 0 || arg2 <= std::numeric_limits<T>::max() / arg1) {
47
355k
    return arg1 * arg2;
48
355k
  } else {
49
0
    ThrowProgramError("Arithmetic overflow");
50
0
    abort();  // Never reached.
51
0
  }
52
355k
}
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
4.33M
std::int32_t SafeInt32Add(std::int32_t arg1, std::int32_t arg2) {
66
4.33M
  return SafeAdd<std::int32_t>(arg1, arg2);
67
4.33M
}
68
69
365M
std::int64_t SafeInt64Add(std::int64_t arg1, std::int64_t arg2) {
70
365M
  return SafeAdd<std::int64_t>(arg1, arg2);
71
365M
}
72
73
bool SafeUint32Add(std::uint32_t arg1, std::uint32_t arg2,
74
506k
                   std::uint32_t *result) {
75
506k
  try {
76
506k
    *result = SafeUint32Add(arg1, arg2);
77
506k
    return true;
78
506k
  } catch (const dng_exception &) {
79
655
    return false;
80
655
  }
81
506k
}
82
83
5.74M
std::uint32_t SafeUint32Add(std::uint32_t arg1, std::uint32_t arg2) {
84
5.74M
  return SafeAdd<std::uint32_t>(arg1, arg2);
85
5.74M
}
86
87
468k
std::uint64_t SafeUint64Add(std::uint64_t arg1, std::uint64_t arg2) {
88
468k
  return SafeAdd<std::uint64_t>(arg1, arg2);
89
468k
}
90
91
47.9M
bool SafeInt32Sub(std::int32_t arg1, std::int32_t arg2, std::int32_t *result) {
92
47.9M
  if ((arg2 >= 0 && arg1 >= std::numeric_limits<int32_t>::min() + arg2) ||
93
47.9M
      (arg2 < 0 && arg1 <= std::numeric_limits<int32_t>::max() + arg2)) {
94
47.9M
    *result = arg1 - arg2;
95
47.9M
    return true;
96
47.9M
  } else {
97
242
    return false;
98
242
  }
99
47.9M
}
100
101
1.44M
std::int32_t SafeInt32Sub(std::int32_t arg1, std::int32_t arg2) {
102
1.44M
  std::int32_t result = 0;
103
104
1.44M
  if (!SafeInt32Sub(arg1, arg2, &result)) {
105
0
    ThrowProgramError("Arithmetic overflow");
106
0
  }
107
108
1.44M
  return result;
109
1.44M
}
110
111
1.78M
std::uint32_t SafeUint32Sub(std::uint32_t arg1, std::uint32_t arg2) {
112
1.78M
  if (arg1 >= arg2) {
113
1.76M
    return arg1 - arg2;
114
1.76M
  } else {
115
17.6k
    ThrowProgramError("Arithmetic overflow");
116
17.6k
    abort();  // Never reached.
117
17.6k
  }
118
1.78M
}
119
120
bool SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2,
121
5.92M
                    std::uint32_t *result) {
122
5.92M
  try {
123
5.92M
    *result = SafeUint32Mult(arg1, arg2);
124
5.92M
    return true;
125
5.92M
  } catch (const dng_exception &) {
126
346
    return false;
127
346
  }
128
5.92M
}
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
60.5M
std::uint32_t SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2) {
151
60.5M
  return SafeUnsignedMult<std::uint32_t>(arg1, arg2);
152
60.5M
}
153
154
std::uint32_t SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2,
155
884k
                             std::uint32_t arg3) {
156
884k
  return SafeUint32Mult(SafeUint32Mult(arg1, arg2), arg3);
157
884k
}
158
159
std::uint32_t SafeUint32Mult(std::uint32_t arg1, std::uint32_t arg2,
160
796k
                             std::uint32_t arg3, std::uint32_t arg4) {
161
796k
  return SafeUint32Mult(SafeUint32Mult(arg1, arg2, arg3), arg4);
162
796k
}
163
164
1.98k
std::int32_t SafeInt32Mult(std::int32_t arg1, std::int32_t arg2) {
165
1.98k
  const std::int64_t tmp =
166
1.98k
      static_cast<std::int64_t>(arg1) * static_cast<std::int64_t>(arg2);
167
1.98k
  if (tmp >= std::numeric_limits<std::int32_t>::min() &&
168
1.92k
      tmp <= std::numeric_limits<std::int32_t>::max()) {
169
1.80k
    return static_cast<std::int32_t>(tmp);
170
1.80k
  } else {
171
173
    ThrowProgramError("Arithmetic overflow");
172
173
    abort();
173
173
  }
174
1.98k
}
175
176
355k
std::size_t SafeSizetMult(std::size_t arg1, std::size_t arg2) {
177
355k
  return SafeUnsignedMult<std::size_t>(arg1, arg2);
178
355k
}
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
246k
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
246k
  if (arg2 == 0) {
218
0
    ThrowProgramError("Division by zero");
219
0
    abort();  // Never reached.
220
246k
  } else if (arg1 == 0) {
221
    // If arg1 is zero, return zero to avoid wraparound in the expression
222
    //   "arg1 - 1" below.
223
543
    return 0;
224
245k
  } else {
225
245k
    return (arg1 - 1) / arg2 + 1;
226
245k
  }
227
246k
}
228
229
bool RoundUpUint32ToMultiple(std::uint32_t val, std::uint32_t multiple_of,
230
3.00M
                             std::uint32_t *result) {
231
3.00M
  try {
232
3.00M
    *result = RoundUpUint32ToMultiple(val, multiple_of);
233
3.00M
    return true;
234
3.00M
  } catch (const dng_exception &) {
235
0
    return false;
236
0
  }
237
3.00M
}
238
239
std::uint32_t RoundUpUint32ToMultiple(std::uint32_t val,
240
3.05M
                                      std::uint32_t multiple_of) {
241
3.05M
  if (multiple_of == 0) {
242
0
    ThrowProgramError("multiple_of is zero in RoundUpUint32ToMultiple");
243
0
  }
244
245
3.05M
  const std::uint32_t remainder = val % multiple_of;
246
3.05M
  if (remainder == 0) {
247
2.35M
    return val;
248
2.35M
  } else {
249
701k
    return SafeUint32Add(val, multiple_of - remainder);
250
701k
  }
251
3.05M
}
252
253
9.60M
bool ConvertUint32ToInt32(std::uint32_t val, std::int32_t *result) {
254
9.60M
  try {
255
9.60M
    *result = ConvertUint32ToInt32(val);
256
9.60M
    return true;
257
9.60M
  } catch (const dng_exception &) {
258
23
    return false;
259
23
  }
260
9.60M
}
261
262
11.1M
std::int32_t ConvertUint32ToInt32(std::uint32_t val) {
263
11.1M
  const std::uint32_t kInt32MaxAsUint32 =
264
11.1M
      static_cast<std::uint32_t>(std::numeric_limits<std::int32_t>::max());
265
266
11.1M
  if (val <= kInt32MaxAsUint32) {
267
11.1M
    return static_cast<std::int32_t>(val);
268
11.1M
  } else {
269
35
    ThrowProgramError("Arithmetic overflow");
270
35
    abort();  // Never reached.
271
35
  }
272
11.1M
}
273
274
138k
std::int32_t ConvertDoubleToInt32(double val) {
275
138k
  const double kMin =
276
138k
      static_cast<double>(std::numeric_limits<std::int32_t>::min());
277
138k
  const double kMax =
278
138k
      static_cast<double>(std::numeric_limits<std::int32_t>::max());
279
  // NaNs will fail this test; they always compare false.
280
138k
  if (val > kMin - 1.0 && val < kMax + 1.0) {
281
135k
    return static_cast<std::int32_t>(val);
282
135k
  } else {
283
2.85k
    ThrowProgramError("Argument not in range in ConvertDoubleToInt32");
284
2.85k
    abort();  // Never reached.
285
2.85k
  }
286
138k
}
287
288
436M
std::uint32_t ConvertDoubleToUint32(double val) {
289
436M
  const double kMax =
290
436M
      static_cast<double>(std::numeric_limits<std::uint32_t>::max());
291
  // NaNs will fail this test; they always compare false.
292
436M
  if (val >= 0.0 && val < kMax + 1.0) {
293
436M
    return static_cast<std::uint32_t>(val);
294
436M
  } else {
295
580
    ThrowProgramError("Argument not in range in ConvertDoubleToUint32");
296
580
    abort();  // Never reached.
297
580
  }
298
436M
}
299
300
8.29k
float ConvertDoubleToFloat(double val) {
301
8.29k
  const double kMax = std::numeric_limits<float>::max();
302
8.29k
  if (val > kMax) {
303
207
    return std::numeric_limits<float>::infinity();
304
8.09k
  } else if (val < -kMax) {
305
418
    return -std::numeric_limits<float>::infinity();
306
7.67k
  } else {
307
    // The cases that end up here are:
308
    // - values in [-kMax, kMax]
309
    // - NaN (because it always compares false)
310
7.67k
    return static_cast<float>(val);
311
7.67k
  }
312
8.29k
}