Skip to content

Commit 7a96d04

Browse files
vmutafovvtrifonov
authored andcommitted
Add validation when calling array element accessors
1 parent e23be04 commit 7a96d04

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

test-app/app/src/main/assets/app/tests/testReleaseNativeCounterpart.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ describe("Test native counterpart release", function () {
2525
expect(errorMessage).toBe("Failed calling toString on a java/lang/Object instance. The JavaScript instance no longer has available Java instance counterpart.");
2626
});
2727

28+
it("Calling the indexer operator on a released native array should throw an exception", function(){
29+
var errorMessage = "";
30+
31+
try{
32+
var arr = new java.lang.reflect.Array.newInstance(java.lang.Object.class, 10);
33+
global.__releaseNativeCounterpart(arr);
34+
arr[1];
35+
} catch(e){
36+
errorMessage = e.message;
37+
}
38+
39+
expect(errorMessage).toBe("Failed calling indexer operator on native array. The JavaScript instance no longer has available Java instance counterpart.");
40+
41+
});
42+
2843
it("Calling release on a non native object should throw exception", function () {
2944

3045
var errorMessage = "";

test-app/runtime/src/main/cpp/ArrayElementAccessor.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Local<Value> ArrayElementAccessor::GetArrayElement(Isolate* isolate, const Local
1818

1919
auto arr = objectManager->GetJavaObjectByJsObject(array);
2020

21+
assertNonNullNativeArray(arr);
22+
2123
Local<Value> value;
2224
jsize startIndex = index;
2325
const jsize length = 1;
@@ -85,7 +87,9 @@ void ArrayElementAccessor::SetArrayElement(Isolate* isolate, const Local<Object>
8587
auto objectManager = runtime->GetObjectManager();
8688
auto context = isolate->GetCurrentContext();
8789

88-
auto arr = objectManager->GetJavaObjectByJsObject(array);
90+
tns::JniLocalRef arr = objectManager->GetJavaObjectByJsObject(array);
91+
92+
assertNonNullNativeArray(arr);
8993

9094
const string elementSignature = arraySignature.substr(1);
9195
jboolean isCopy = false;
@@ -198,3 +202,9 @@ Local<Value> ArrayElementAccessor::ConvertToJsValue(Isolate* isolate, ObjectMana
198202

199203
return jsValue;
200204
}
205+
206+
void ArrayElementAccessor::assertNonNullNativeArray(tns::JniLocalRef& arrayReference) {
207+
if(arrayReference.IsNull()){
208+
throw NativeScriptException("Failed calling indexer operator on native array. The JavaScript instance no longer has available Java instance counterpart.");
209+
}
210+
}

test-app/runtime/src/main/cpp/ArrayElementAccessor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ArrayElementAccessor {
1515

1616
private:
1717
v8::Local<v8::Value> ConvertToJsValue(v8::Isolate* isolate, ObjectManager* objectManager, JEnv& env, const std::string& elementSignature, const void* value);
18+
void assertNonNullNativeArray(tns::JniLocalRef& arrayReference);
1819
};
1920
}
2021

0 commit comments

Comments
 (0)