Skip to content
Permalink
Browse files

Buffer, feat: support safe buffer mode.

  • Loading branch information...
xicilion committed Oct 3, 2018
1 parent d1cf7fa commit bee235f350a56cb8a1d8f296518332d643d136d7
Showing with 52 additions and 4 deletions.
  1. +2 −0 fibjs/include/Buffer.h
  2. +3 −1 fibjs/include/Isolate.h
  3. +3 −1 fibjs/src/base/Runtime.cpp
  4. +1 −1 fibjs/src/fs/fs.cpp
  5. +43 −1 fibjs/src/global/Buffer.cpp
@@ -123,6 +123,8 @@ class Buffer : public Buffer_base {
return &m_data[0];
}

bool is_safe_codec(exlib::string codec);

public:
result_t readNumber(int32_t offset, char* buf, int32_t size, bool noAssert, bool le);
result_t writeNumber(int32_t offset, const char* buf, int32_t size, bool noAssert, bool le, int32_t& retVal);
@@ -141,7 +141,9 @@ class Isolate : public exlib::linkitem {

int32_t m_exitCode;

bool m_bFileAccess;
bool m_enable_FileSystem;
bool m_safe_buffer;
int32_t m_max_buffer_size;
};

} /* namespace fibjs */
@@ -268,7 +268,9 @@ Isolate::Isolate(exlib::string fname)
, m_loglevel(console_base::_NOTSET)
, m_defaultMaxListeners(10)
, m_exitCode(0)
, m_bFileAccess(true)
, m_enable_FileSystem(true)
, m_safe_buffer(false)
, m_max_buffer_size(-1)
{
m_fname = fname;

@@ -254,7 +254,7 @@ result_t fs_base::openFile(exlib::string fname, exlib::string flags,

Isolate* isolate = Runtime::check() ? Isolate::current() : ac->isolate();

if (isolate && !isolate->m_bFileAccess)
if (isolate && !isolate->m_enable_FileSystem)
return CHECK_ERROR(CALL_E_INVALID_CALL);

hr = pFile->open(safe_name, flags);
@@ -284,8 +284,28 @@ result_t Buffer_base::concat(v8::Local<v8::Array> buflist, int32_t cutLength, ob
return hr;
}

inline bool is_native_codec(exlib::string codec)
{
return (codec == "hex") || (codec == "base64")
|| (codec == "utf8") || (codec == "utf-8")
|| (codec == "ucs2") || (codec == "ucs-2")
|| (codec == "utf16le") || (codec == "utf-16le")
|| (codec == "ucs4") || (codec == "ucs-4")
|| (codec == "utf32le") || (codec == "utf-32le");
}

inline bool static_is_safe_codec(exlib::string codec)
{
return !Isolate::current()->m_safe_buffer || is_native_codec(codec);
}

result_t Buffer_base::isEncoding(exlib::string codec, bool& retVal)
{
if (!static_is_safe_codec(codec)) {
retVal = false;
return 0;
}

if ((codec == "utf8") || (codec == "utf-8") || (codec == "hex") || (codec == "base64")) {
retVal = true;
} else {
@@ -294,6 +314,11 @@ result_t Buffer_base::isEncoding(exlib::string codec, bool& retVal)
return 0;
}

bool Buffer::is_safe_codec(exlib::string codec)
{
return !holder()->m_safe_buffer || is_native_codec(codec);
}

result_t Buffer::_indexed_getter(uint32_t index, int32_t& retVal)
{
if (index >= m_data.length())
@@ -328,9 +353,14 @@ result_t Buffer::resize(int32_t sz)
if (sz < 0)
return CHECK_ERROR(CALL_E_INVALIDARG);

extMemory(sz - (int32_t)m_data.length());
int32_t sz1 = (int32_t)m_data.length();
extMemory(sz - sz1);

m_data.resize(sz);

if (sz > sz1 && holder()->m_safe_buffer)
memset(&m_data[sz1], 0, sz - sz1);

return 0;
}

@@ -346,6 +376,9 @@ result_t Buffer::append(Buffer_base* data)

result_t Buffer::append(exlib::string str, exlib::string codec)
{
if (!is_safe_codec(codec))
return CHECK_ERROR(Runtime::setError("Unknown codec."));

if ((codec == "utf8") || (codec == "utf-8")) {
extMemory((int32_t)str.length());
m_data.append(str);
@@ -370,6 +403,9 @@ result_t Buffer::append(exlib::string str, exlib::string codec)

result_t Buffer::write(exlib::string str, int32_t offset, int32_t length, exlib::string codec, int32_t& retVal)
{
if (!is_safe_codec(codec))
return CHECK_ERROR(Runtime::setError("Unknown codec."));

int32_t max_length = 0;
int32_t buffer_length = (int32_t)m_data.length();

@@ -1065,6 +1101,9 @@ result_t Buffer::toString(exlib::string& retVal)

result_t Buffer::toString(exlib::string codec, int32_t offset, exlib::string& retVal)
{
if (!is_safe_codec(codec))
return CHECK_ERROR(Runtime::setError("Unknown codec."));

exlib::string str;
int32_t length = (int32_t)m_data.length();

@@ -1086,6 +1125,9 @@ result_t Buffer::toString(exlib::string codec, int32_t offset, exlib::string& re

result_t Buffer::toString(exlib::string codec, int32_t offset, int32_t end, exlib::string& retVal)
{
if (!is_safe_codec(codec))
return CHECK_ERROR(Runtime::setError("Unknown codec."));

exlib::string str;
int32_t length = (int32_t)m_data.length();

0 comments on commit bee235f

Please sign in to comment.
You can’t perform that action at this time.