Skip to content

Commit 14d4f80

Browse files
authored
Merge pull request #15 from drake127/split_fix
Fixes stack overflow in bsdiff.
2 parents b50dbc9 + dd84421 commit 14d4f80

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

bsdiff.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232
#include <string.h>
3333

3434
#define MIN(x,y) (((x)<(y)) ? (x) : (y))
35+
#define MEDIAN3(a,b,c) (((a)<(b)) ? \
36+
((b)<(c) ? (b) : ((a)<(c) ? (c) : (a))) : \
37+
((b)>(c) ? (b) : ((a)>(c) ? (c) : (a))))
3538

3639
static void split(int64_t *I,int64_t *V,int64_t start,int64_t len,int64_t h)
3740
{
38-
int64_t i,j,k,x,tmp,jj,kk;
41+
int64_t i,j,k,x,y,z,tmp,jj,kk;
3942

4043
if(len<16) {
4144
for(k=start;k<start+len;k+=j) {
@@ -56,7 +59,20 @@ static void split(int64_t *I,int64_t *V,int64_t start,int64_t len,int64_t h)
5659
return;
5760
};
5861

59-
x=V[I[start+len/2]+h];
62+
/* Select pivot, algorithm by Bentley & McIlroy */
63+
j=start+len/2;
64+
k=start+len-1;
65+
x=V[I[j]+h];
66+
y=V[I[start]+h];
67+
z=V[I[k]+h];
68+
if(len>40) { /* Big array: Pseudomedian of 9 */
69+
tmp=len/8;
70+
x=MEDIAN3(x,V[I[j-tmp]+h],V[I[j+tmp]+h]);
71+
y=MEDIAN3(y,V[I[start+tmp]+h],V[I[start+tmp+tmp]+h]);
72+
z=MEDIAN3(z,V[I[k-tmp]+h],V[I[k-tmp-tmp]+h]);
73+
}; /* Else medium array: Pseudomedian of 3 */
74+
x=MEDIAN3(x,y,z);
75+
6076
jj=0;kk=0;
6177
for(i=start;i<start+len;i++) {
6278
if(V[I[i]+h]<x) jj++;

0 commit comments

Comments
 (0)