Skip to content

Commit

Permalink
fs, bugfix: fix crash when read empty file with codec use fs.readFile.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Oct 21, 2017
1 parent 07e705e commit 39ed71a
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions fibjs/src/fs/fs.cpp
Expand Up @@ -272,7 +272,12 @@ result_t fs_base::readTextFile(exlib::string fname, exlib::string& retVal,
hr = f->cc_readAll(buf); hr = f->cc_readAll(buf);
f->cc_close(); f->cc_close();


if (hr < 0 || hr == CALL_RETURN_NULL) if (hr == CALL_RETURN_NULL) {
retVal.clear();
return 0;
}

if (hr < 0)
return hr; return hr;


return buf->toString(retVal); return buf->toString(retVal);
Expand All @@ -295,18 +300,28 @@ result_t fs_base::readFile(exlib::string fname, exlib::string encoding,
hr = f->cc_readAll(buf); hr = f->cc_readAll(buf);
f->cc_close(); f->cc_close();


if (encoding != "") { if (hr < 0)
exlib::string str;
hr = iconv_base::decode(encoding, buf, str);
if (hr < 0)
return hr;
retVal = str;
} else
retVal = buf;

if (hr < 0 || hr == CALL_RETURN_NULL)
return hr; return hr;


if (hr == CALL_RETURN_NULL) {
if (encoding != "") {
exlib::string str;
retVal = str;
} else {
buf = new Buffer();
retVal = buf;
}
} else {
if (encoding != "") {
exlib::string str;
hr = iconv_base::decode(encoding, buf, str);
if (hr < 0)
return hr;
retVal = str;
} else
retVal = buf;
}

return 0; return 0;
} }


Expand Down

0 comments on commit 39ed71a

Please sign in to comment.