##// END OF EJS Templates
mpatch: introduce a safeadd() helper to work around UB int overflow...
Augie Fackler -
r38248:1ec4cb8c stable
parent child Browse files
Show More
@@ -20,6 +20,7 b''
20 20 of the GNU General Public License, incorporated herein by reference.
21 21 */
22 22
23 #include <limits.h>
23 24 #include <stdlib.h>
24 25 #include <string.h>
25 26
@@ -27,6 +28,15 b''
27 28 #include "compat.h"
28 29 #include "mpatch.h"
29 30
31 /* VC9 doesn't include bool and lacks stdbool.h based on cext/util.h */
32 #if defined(_MSC_VER) || __STDC_VERSION__ < 199901L
33 #define true 1
34 #define false 0
35 typedef unsigned char bool;
36 #else
37 #include <stdbool.h>
38 #endif
39
30 40 static struct mpatch_flist *lalloc(ssize_t size)
31 41 {
32 42 struct mpatch_flist *a = NULL;
@@ -60,6 +70,24 b' static ssize_t lsize(struct mpatch_flist'
60 70 return a->tail - a->head;
61 71 }
62 72
73 /* add helper to add src and *dest iff it won't overflow */
74 static inline bool safeadd(int src, int *dest)
75 {
76 if ((src > 0) == (*dest > 0)) {
77 if (*dest > 0) {
78 if (src > (INT_MAX - *dest)) {
79 return false;
80 }
81 } else {
82 if (src < (INT_MIN - *dest)) {
83 return false;
84 }
85 }
86 }
87 *dest += src;
88 return true;
89 }
90
63 91 /* move hunks in source that are less cut to dest, compensating
64 92 for changes in offset. the last hunk may be split if necessary.
65 93 */
General Comments 0
You need to be logged in to leave comments. Login now