Skip to content

Commit

Permalink
fix(java): catch std::range_error exception
Browse files Browse the repository at this point in the history
  • Loading branch information
spacelan committed Jun 1, 2020
1 parent e4381a3 commit 016566a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
4 changes: 0 additions & 4 deletions java/src/test/java/com/baidu/openrasp/v8/ContextImpl.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.baidu.openrasp.v8;

import java.util.*;
import com.jsoniter.spi.JsoniterSpi;
import com.jsoniter.extra.Base64Support;
import com.jsoniter.output.JsonStream;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;

public class ContextImpl extends Context {

Expand Down
47 changes: 31 additions & 16 deletions java/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,46 @@ void GetStack(v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value
}

std::string Jstring2String(JNIEnv* env, jstring str) {
auto size = env->GetStringLength(str);
if (size < 0) {
return {};
}
if (size > max_buffer_size) {
size = max_buffer_size;
}
std::vector<char16_t> u16(size);
env->GetStringRegion(str, 0, size, reinterpret_cast<jchar*>(u16.data()));
try {
auto size = env->GetStringLength(str);
if (size < 0) {
return {};
}
if (size > max_buffer_size) {
size = max_buffer_size;
}
std::vector<char16_t> u16(size);
env->GetStringRegion(str, 0, size, reinterpret_cast<jchar*>(u16.data()));
#ifdef _WIN32
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}.to_bytes(
std::wstring(u16.begin(), u16.end()));
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}.to_bytes(
std::wstring(u16.begin(), u16.end()));
#else
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16.data(), u16.data() + size);
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16.data(), u16.data() + size);
#endif
} catch (std::exception& exception) {
std::string s;
auto utf = env->GetStringUTFChars(str, nullptr);
auto size = env->GetStringUTFLength(str);
if (utf && size > 0) {
s = std::string(utf, size < max_buffer_size ? size : max_buffer_size);
}
env->ReleaseStringUTFChars(str, utf);
return s;
}
}

jstring String2Jstring(JNIEnv* env, const std::string& str) {
try {
#ifdef _WIN32
std::wstring ws = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}.from_bytes(str);
std::u16string u16(ws.begin(), ws.end());
std::wstring ws = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}.from_bytes(str);
std::u16string u16(ws.begin(), ws.end());
#else
std::u16string u16 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.from_bytes(str);
std::u16string u16 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.from_bytes(str);
#endif
return env->NewString(reinterpret_cast<const jchar*>(u16.data()), u16.size());
return env->NewString(reinterpret_cast<const jchar*>(u16.data()), u16.size());
} catch (std::exception& exception) {
return env->NewStringUTF(str.data());
}
}

v8::MaybeLocal<v8::String> Jstring2V8string(JNIEnv* env, jstring jstr) {
Expand Down

0 comments on commit 016566a

Please sign in to comment.