Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect binary search logic which would cause the longest matching string to be missed #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions bsdiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ static int64_t matchlen(const uint8_t *old,int64_t oldsize,const uint8_t *new,in
static int64_t search(const int64_t *I,const uint8_t *old,int64_t oldsize,
const uint8_t *new,int64_t newsize,int64_t st,int64_t en,int64_t *pos)
{
int64_t x,y;
int64_t x,y,cmpsize;
int32_t res;

if(en-st<2) {
x=matchlen(old+I[st],oldsize-I[st],new,newsize);
Expand All @@ -160,7 +161,9 @@ static int64_t search(const int64_t *I,const uint8_t *old,int64_t oldsize,
};

x=st+(en-st)/2;
if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) {
cmpsize=MIN(oldsize-I[x],newsize);
res=memcmp(old+I[x],new,cmpsize);
if((res<0) || ((res==0) && (cmpsize<newsize))) {
return search(I,old,oldsize,new,newsize,x,en,pos);
} else {
return search(I,old,oldsize,new,newsize,st,x,pos);
Expand Down