@@ -84,7 +84,9 @@ static off_t search(off_t *I, u_char *old, off_t oldsize,
84
84
}
85
85
86
86
x = st + (en - st )/2 ;
87
- if (memcmp (old + I [x ], new , (size_t )(MIN (oldsize - I [x ], newsize ))) < 0 ) {
87
+ /* Modification ported from ChromiumOS project:
88
+ * https://chromium.googlesource.com/chromiumos/third_party/bsdiff/+/58146f74abd6b1b69693943195f37f4ac6a6acef%5E%21/#F0 */
89
+ if (memcmp (old + I [x ], new , (size_t )(MIN (oldsize - I [x ], newsize ))) <= 0 ) {
88
90
return search (I , old , oldsize , new , newsize , x , en , pos );
89
91
} else {
90
92
return search (I , old , oldsize , new , newsize , st , x , pos );
@@ -212,8 +214,18 @@ int bsdiff(int argc, char *argv[])
212
214
lastoffset = 0 ;
213
215
while (scan < newsize ) {
214
216
oldscore = 0 ;
215
-
217
+ /* Modification ported from ChromiumOS project:
218
+ * https://chromium.googlesource.com/chromiumos/third_party/bsdiff/+/a055996c743add7a9558839276fd1e4994d16bd3%5E%21/#F0 */
219
+ /* If we come across a large block of data that only differs
220
+ * by less than 8 bytes, this loop will take a long time to
221
+ * go past that block of data. We need to track the number of
222
+ * times we're stuck in the block and break out of it. */
223
+ int num_less_than_eight = 0 ;
224
+ off_t prev_len , prev_oldscore , prev_pos ;
216
225
for (scsc = scan += len ; scan < newsize ; scan ++ ) {
226
+ prev_len = len ;
227
+ prev_oldscore = oldscore ;
228
+ prev_pos = pos ;
217
229
/* 'oldscore' is the number of characters that match between the
218
230
* substrings 'old[lastoffset + scan:lastoffset + scsc]' and
219
231
* 'new[scan:scsc]'. */
@@ -240,6 +252,19 @@ int bsdiff(int argc, char *argv[])
240
252
if ((scan + lastoffset < oldsize ) &&
241
253
(old [scan + lastoffset ] == new [scan ]))
242
254
oldscore -- ;
255
+
256
+ /* Modification ported from ChromiumOS project:
257
+ * https://chromium.googlesource.com/chromiumos/third_party/bsdiff/+/426e4aa1cbeb3c8a73002047d7a796ca8e5e17d4%5E%21/#F0 */
258
+ const off_t fuzz = 8 ;
259
+ if (prev_len - fuzz <= len && len <= prev_len &&
260
+ prev_oldscore - fuzz <= oldscore &&
261
+ oldscore <= prev_oldscore &&
262
+ prev_pos <= pos && pos <= prev_pos + fuzz &&
263
+ oldscore <= len && len <= oldscore + fuzz )
264
+ ++ num_less_than_eight ;
265
+ else
266
+ num_less_than_eight = 0 ;
267
+ if (num_less_than_eight > 100 ) break ;
243
268
}
244
269
245
270
/* Skip this section if we found an exact match that would be
0 commit comments