Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_safe_arithmetic.cpp

raw
1 /*****************************************************************************/
2 // Copyright 2015-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #include "dng_safe_arithmetic.h"
10
11 #include <cmath>
12 #include <limits>
13
14 #include "dng_exceptions.h"
15
16 // Implementation of safe integer arithmetic follows guidelines from
17 // https://www.securecoding.cert.org/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap
18 // and
19 // https://www.securecoding.cert.org/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow
20
21 namespace {
22
23 // Template functions for safe arithmetic. These functions are not exposed in
24 // the header for the time being to avoid having to add checks for the various
25 // constraints on the template argument (e.g. that it is integral and possibly
26 // signed or unsigned only). This should be done using a static_assert(), but
27 // we want to be portable to pre-C++11 compilers.
28
29 // Returns the result of adding arg1 and arg2 if it will fit in a T (where T is
30 // a signed or unsigned integer type). Otherwise, throws a dng_exception with
31 // error code dng_error_unknown.
32 template <class T>
33 T SafeAdd(T arg1, T arg2) {
34 // The condition is reformulated relative to the version on
35 // www.securecoding.cert.org to check for valid instead of invalid cases. It
36 // seems safer to enumerate the valid cases (and potentially miss one) than
37 // enumerate the invalid cases.
38 // If T is an unsigned type, the second half of the condition always evaluates
39 // to false and will presumably be compiled out by the compiler.
40 if ((arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) ||
41 (arg1 < 0 && arg2 >= std::numeric_limits<T>::min() - arg1)) {
42 return arg1 + arg2;
43 } else {
44 ThrowOverflow ("Arithmetic overflow in SafeAdd");
45
46 // Dummy return statement.
47 return 0;
48 }
49 }
50
51 // Returns the result of multiplying arg1 and arg2 if it will fit in a T (where
52 // T is an unsigned integer type). Otherwise, throws a dng_exception with error
53 // code dng_error_unknown.
54 template <class T>
55 T SafeUnsignedMult(T arg1, T arg2) {
56 if (arg1 == 0 || arg2 <= std::numeric_limits<T>::max() / arg1) {
57 return arg1 * arg2;
58 } else {
59 ThrowOverflow ("Arithmetic overflow in SafeUnsignedMult");
60
61 // Dummy return statement.
62 return 0;
63 }
64 }
65
66 } // namespace
67
68 bool SafeInt32Add(int32 arg1, int32 arg2, int32 *result) {
69 try {
70 *result = SafeInt32Add(arg1, arg2);
71 return true;
72 } catch (const dng_exception &) {
73 return false;
74 }
75 }
76
77 int32 SafeInt32Add(int32 arg1, int32 arg2) {
78 return SafeAdd<int32>(arg1, arg2);
79 }
80
81 int64 SafeInt64Add(int64 arg1, int64 arg2) {
82 return SafeAdd<int64>(arg1, arg2);
83 }
84
85 bool SafeUint32Add(uint32 arg1, uint32 arg2,
86 uint32 *result) {
87 try {
88 *result = SafeUint32Add(arg1, arg2);
89 return true;
90 } catch (const dng_exception &) {
91 return false;
92 }
93 }
94
95 uint32 SafeUint32Add(uint32 arg1, uint32 arg2) {
96 return SafeAdd<uint32>(arg1, arg2);
97 }
98
99 uint64 SafeUint64Add(uint64 arg1, uint64 arg2) {
100 return SafeAdd<uint64>(arg1, arg2);
101 }
102
103 bool SafeInt32Sub(int32 arg1, int32 arg2, int32 *result) {
104 if ((arg2 >= 0 && arg1 >= std::numeric_limits<int32_t>::min() + arg2) ||
105 (arg2 < 0 && arg1 <= std::numeric_limits<int32_t>::max() + arg2)) {
106 *result = arg1 - arg2;
107 return true;
108 } else {
109 return false;
110 }
111 }
112
113 int32 SafeInt32Sub(int32 arg1, int32 arg2) {
114 int32 result = 0;
115
116 if (!SafeInt32Sub(arg1, arg2, &result)) {
117 ThrowOverflow ("Arithmetic overflow in SafeInt32Sub");
118 }
119
120 return result;
121 }
122
123 uint32 SafeUint32Sub(uint32 arg1, uint32 arg2) {
124 if (arg1 >= arg2) {
125 return arg1 - arg2;
126 } else {
127 ThrowOverflow ("Arithmetic overflow in SafeInt32Sub");
128
129 // Dummy return statement.
130 return 0;
131 }
132 }
133
134 bool SafeUint32Mult(uint32 arg1, uint32 arg2,
135 uint32 *result) {
136 try {
137 *result = SafeUint32Mult(arg1, arg2);
138 return true;
139 } catch (const dng_exception &) {
140 return false;
141 }
142 }
143
144 bool SafeUint32Mult(uint32 arg1, uint32 arg2, uint32 arg3,
145 uint32 *result) {
146 try {
147 *result = SafeUint32Mult(arg1, arg2, arg3);
148 return true;
149 } catch (const dng_exception &) {
150 return false;
151 }
152 }
153
154 bool SafeUint32Mult(uint32 arg1, uint32 arg2, uint32 arg3,
155 uint32 arg4, uint32 *result) {
156 try {
157 *result = SafeUint32Mult(arg1, arg2, arg3, arg4);
158 return true;
159 } catch (const dng_exception &) {
160 return false;
161 }
162 }
163
164 uint32 SafeUint32Mult(uint32 arg1, uint32 arg2) {
165 return SafeUnsignedMult<uint32>(arg1, arg2);
166 }
167
168 uint32 SafeUint32Mult(uint32 arg1, uint32 arg2,
169 uint32 arg3) {
170 return SafeUint32Mult(SafeUint32Mult(arg1, arg2), arg3);
171 }
172
173 uint32 SafeUint32Mult(uint32 arg1, uint32 arg2,
174 uint32 arg3, uint32 arg4) {
175 return SafeUint32Mult(SafeUint32Mult(arg1, arg2, arg3), arg4);
176 }
177
178 int32 SafeInt32Mult(int32 arg1, int32 arg2) {
179 const int64 tmp =
180 static_cast<int64>(arg1) * static_cast<int64>(arg2);
181 if (tmp >= std::numeric_limits<int32>::min() &&
182 tmp <= std::numeric_limits<int32>::max()) {
183 return static_cast<int32>(tmp);
184 } else {
185 ThrowOverflow ("Arithmetic overflow in SafeInt32Mult");
186
187 // Dummy return statement.
188 return 0;
189 }
190 }
191
192 bool SafeInt32Mult(int32 arg1, int32 arg2, int32 *result) {
193
194 const int64 tmp =
195 static_cast<int64>(arg1) * static_cast<int64>(arg2);
196
197 if (tmp >= std::numeric_limits<int32>::min() &&
198 tmp <= std::numeric_limits<int32>::max()) {
199
200 *result = static_cast<int32>(tmp);
201
202 } else {
203
204 return false;
205
206 }
207
208 return true;
209
210 }
211
212 std::size_t SafeSizetMult(std::size_t arg1, std::size_t arg2) {
213 return SafeUnsignedMult<std::size_t>(arg1, arg2);
214 }
215
216 namespace dng_internal {
217
218 int64 SafeInt64MultSlow(int64 arg1, int64 arg2) {
219 bool overflow = true;
220
221 if (arg1 > 0) {
222 if (arg2 > 0) {
223 overflow = (arg1 > std::numeric_limits<int64>::max() / arg2);
224 } else {
225 overflow = (arg2 < std::numeric_limits<int64>::min() / arg1);
226 }
227 } else {
228 if (arg2 > 0) {
229 overflow = (arg1 < std::numeric_limits<int64>::min() / arg2);
230 } else {
231 overflow = (arg1 != 0 &&
232 arg2 < std::numeric_limits<int64>::max() / arg1);
233 }
234 }
235
236 if (overflow) {
237 ThrowOverflow ("Arithmetic overflow in SafeInt64MultSlow");
238
239 // Dummy return statement.
240 return 0;
241 } else {
242 return arg1 * arg2;
243 }
244 }
245
246 } // namespace dng_internal
247
248 uint32 SafeUint32DivideUp(uint32 arg1, uint32 arg2) {
249 // It might seem more intuitive to implement this function simply as
250 //
251 // return arg2 == 0 ? 0 : (arg1 + arg2 - 1) / arg2;
252 //
253 // but the expression "arg1 + arg2" can wrap around.
254
255 if (arg2 == 0) {
256 ThrowProgramError("Division by zero");
257
258 // Dummy return to avoid compiler error about missing return statement.
259 return 0;
260 } else if (arg1 == 0) {
261 // If arg1 is zero, return zero to avoid wraparound in the expression
262 // "arg1 - 1" below.
263 return 0;
264 } else {
265 return (arg1 - 1) / arg2 + 1;
266 }
267 }
268
269 bool RoundUpUint32ToMultiple(uint32 val, uint32 multiple_of,
270 uint32 *result) {
271 if (multiple_of == 0) {
272 return false;
273 }
274
275 const uint32 remainder = val % multiple_of;
276 if (remainder == 0) {
277 *result = val;
278 return true;
279 } else {
280 return SafeUint32Add(val, multiple_of - remainder, result);
281 }
282 }
283
284 uint32 RoundUpUint32ToMultiple(uint32 val,
285 uint32 multiple_of) {
286 if (multiple_of == 0) {
287 ThrowProgramError("multiple_of is zero in RoundUpUint32ToMultiple");
288 }
289
290 const uint32 remainder = val % multiple_of;
291 if (remainder == 0) {
292 return val;
293 } else {
294 return SafeUint32Add(val, multiple_of - remainder);
295 }
296 }
297
298 bool ConvertUint32ToInt32(uint32 val, int32 *result) {
299 const uint32 kInt32MaxAsUint32 =
300 static_cast<uint32>(std::numeric_limits<int32>::max());
301
302 if (val <= kInt32MaxAsUint32) {
303 *result = static_cast<int32>(val);
304 return true;
305 } else {
306 return false;
307 }
308 }
309
310 int32 ConvertUint32ToInt32(uint32 val) {
311 const uint32 kInt32MaxAsUint32 =
312 static_cast<uint32>(std::numeric_limits<int32>::max());
313
314 if (val <= kInt32MaxAsUint32) {
315 return static_cast<int32>(val);
316 } else {
317 ThrowOverflow ("Arithmetic overflow in ConvertUint32ToInt32");
318
319 // Dummy return statement.
320 return 0;
321 }
322 }
323
324 int32 ConvertDoubleToInt32(double val) {
325 const double kMin =
326 static_cast<double>(std::numeric_limits<int32>::min());
327 const double kMax =
328 static_cast<double>(std::numeric_limits<int32>::max());
329 // NaNs will fail this test; they always compare false.
330 if (val > kMin - 1.0 && val < kMax + 1.0) {
331 return static_cast<int32>(val);
332 } else {
333 ThrowOverflow ("Arithmetic overflow in ConvertDoubleToInt32");
334
335 // Dummy return statement.
336 return 0;
337 }
338 }
339
340 uint32 ConvertDoubleToUint32(double val) {
341 const double kMax =
342 static_cast<double>(std::numeric_limits<uint32>::max());
343 // NaNs will fail this test; they always compare false.
344 if (val >= 0.0 && val < kMax + 1.0) {
345 return static_cast<uint32>(val);
346 } else {
347 ThrowOverflow ("Arithmetic overflow in ConvertDoubleToUint32");
348
349 // Dummy return statement.
350 return 0;
351 }
352 }
353
354 float ConvertDoubleToFloat(double val) {
355 const double kMax = std::numeric_limits<float>::max();
356 if (val > kMax) {
357 return std::numeric_limits<float>::infinity();
358 } else if (val < -kMax) {
359 return -std::numeric_limits<float>::infinity();
360 } else {
361 // The cases that end up here are:
362 // - values in [-kMax, kMax]
363 // - NaN (because it always compares false)
364 return static_cast<float>(val);
365 }
366 }
367
368 /*****************************************************************************/
369
370 dng_safe_uint32::dng_safe_uint32 (const dng_safe_int32 &x)
371 {
372
373 if (x.Get () < 0)
374 {
375 ThrowOverflow ("Overflow in dng_safe_uint32");
376 }
377
378 fValue = static_cast<uint32> (x.Get ());
379
380 }
381
382 /*****************************************************************************/
383
384 dng_safe_int32::dng_safe_int32 (const dng_safe_uint32 &x)
385 {
386
387 Set_uint32 (x.Get ());
388
389 }
390
391 /*****************************************************************************/