Permalink
Browse files

fs, bugfix: fix crash when read empty file with codec use fs.readFile.

  • Loading branch information...
xicilion committed Oct 21, 2017
1 parent 07e705e commit 39ed71a85cf0231982dcab237eeb823ad23a6c30
Showing with 26 additions and 11 deletions.
  1. +26 −11 fibjs/src/fs/fs.cpp
View
@@ -272,7 +272,12 @@ result_t fs_base::readTextFile(exlib::string fname, exlib::string& retVal,
hr = f->cc_readAll(buf);
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 buf->toString(retVal);
@@ -295,18 +300,28 @@ result_t fs_base::readFile(exlib::string fname, exlib::string encoding,
hr = f->cc_readAll(buf);
f->cc_close();
if (encoding != "") {
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)
if (hr < 0)
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;
}

0 comments on commit 39ed71a

Please sign in to comment.