Skip to content

Commit

Permalink
Fix binary search midpoint overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancorvussolis committed Mar 4, 2016
1 parent 8c60a21 commit c92b470
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions crvskkserv.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Express 2013 for Windows Desktop
VisualStudioVersion = 12.0.30501.0
# Visual Studio 2013
VisualStudioVersion = 12.0.40629.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "crvskkserv", "crvskkserv\crvskkserv.vcxproj", "{EE56E58B-C54E-425C-A0AF-292B4653B1F0}"
EndProject
Expand Down
42 changes: 21 additions & 21 deletions crvskkserv/search_dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void init_search_dictionary(DICINFO &dicinfo)
_wfopen_s(&fpidx, idxpath, RB);
if(fpidx != NULL)
{
while(fread(&pos, 4, 1, fpidx) == 1)
while(fread(&pos, sizeof(pos), 1, fpidx) == 1)
{
dicinfo.pos.push_back(pos);
}
Expand All @@ -44,7 +44,7 @@ void init_search_dictionary(DICINFO &dicinfo)

while((pb = fgets(buf, sizeof(buf), fpdic)) != NULL)
{
sbuf.append(buf);
sbuf += buf;

if(!sbuf.empty() && sbuf.back() == '\n')
{
Expand All @@ -67,7 +67,8 @@ void init_search_dictionary(DICINFO &dicinfo)
}
else if(okuri != -1)
{
if((pidx = sbuf.find_first_of('\x20')) != std::string::npos)
pidx = sbuf.find("\x20/");
if(pidx != std::string::npos && pidx <= sbuf.size())
{
map.insert(PAIR(sbuf.substr(0, pidx), pos));
}
Expand Down Expand Up @@ -107,10 +108,8 @@ void search_dictionary(DICINFO &dicinfo, const std::string &key, std::string &s)
{
FILE *fpdic;
CHAR buf[DICBUFSIZE];
std::string sbuf, ckey;
std::string ckey, sbuf, kbuf, cbuf;
long pos, left, mid, right;
int comp;
size_t pidx;

_wfopen_s(&fpdic, dicinfo.path.c_str(), RB);
if(fpdic == NULL)
Expand All @@ -121,17 +120,17 @@ void search_dictionary(DICINFO &dicinfo, const std::string &key, std::string &s)
ckey = key + "\x20";

left = 0;
right = dicinfo.pos.size() - 1;
right = (long)dicinfo.pos.size() - 1;

while(left <= right)
{
mid = (left + right) / 2;
pos = dicinfo.pos.at(mid);

mid = left + (right - left) / 2;
pos = dicinfo.pos[mid];
fseek(fpdic, pos, SEEK_SET);
memset(buf, 0, sizeof(buf));

sbuf.clear();
kbuf.clear();
cbuf.clear();

while(fgets(buf, _countof(buf), fpdic) != NULL)
{
Expand All @@ -143,19 +142,20 @@ void search_dictionary(DICINFO &dicinfo, const std::string &key, std::string &s)
}
}

comp = strncmp(ckey.c_str(), sbuf.c_str(), ckey.size());
if(comp == 0)
size_t cidx = sbuf.find("\x20/");
if(cidx != std::wstring::npos && cidx < sbuf.size())
{
if((pidx = sbuf.find_first_of('\x20')) != std::string::npos)
{
if((pidx = sbuf.find_first_of('/', pidx)) != std::string::npos)
{
s = sbuf.substr(pidx);
}
}
kbuf = sbuf.substr(0, cidx + 1);
cbuf = sbuf.substr(cidx + 1);
}

int cmpkey = ckey.compare(kbuf);
if(cmpkey == 0)
{
s = cbuf;
break;
}
else if(comp > 0)
else if(cmpkey > 0)
{
left = mid + 1;
}
Expand Down

0 comments on commit c92b470

Please sign in to comment.