Android: Fix memory issues in _variant_to_jvalue()#113159
Android: Fix memory issues in _variant_to_jvalue()#113159akien-mga merged 1 commit intogodotengine:masterfrom
_variant_to_jvalue()#113159Conversation
platform/android/jni_utils.cpp
Outdated
There was a problem hiding this comment.
Should those be initialized as well?
There was a problem hiding this comment.
In these cases, not initializing won't cause any problems, because NewObjectA() should only access the part of the jvalue that is actually needed for that particular class.
We could initialize it just in case we change it later or copy-paste this code to different context where that wouldn't be true? But, personally, I guess I'd lean towards leaving it be
m4gr3d
left a comment
There was a problem hiding this comment.
Looks good! Thanks for the fix!
Can you think of ways we could catch this with the instrumented tests?
b04aad5 to
ff070ae
Compare
For the case where
UPDATE: Actually, no, it looks like dictionaries will take the I haven't had a chance to try the instrumented tests yet! I'll take a look |
ff070ae to
c2f8d1a
Compare
|
I've just push a new version of this which makes much more drastic changes:
However, I still have the more minimal version of this PR saved in a branch, if we want to go back to that |
The changes look good and make sense. I tried to think of a scenario where the current version with
We will still need the minimal version to cherry-pick against 4.5 Edit: nevermind, my changes were merged in 4.6 so there's nothing to fix in 4.5. |
|
Thanks! |
There's some issues in
_variant_to_jvalue()which mostly stem fromjvaluebeing aunion(so, all the fields are stored on an overlapping chunk of memory) and the code seems to be treating it sort of like it's astruct(or at least like it's 32-bits rather than 64-bits?).For example, the
jvalue valueisn't initialized to anything, so it starts out with junk memory, but the default case is to setvalue.i = 0, which will only set the lower 32-bits and leave the upper 32-bits with junk in them:godot/platform/android/jni_utils.cpp
Lines 273 to 275 in 9dd6c4d
Also, the function may set other fields like
value.z,value.jorvalue.f(whenforce_objectisfalse), but at the end, it will overwrite anything that's there by settingvalue.l:godot/platform/android/jni_utils.cpp
Line 277 in 9dd6c4d
This PR fixes all that by:
jvalue valueto zero (which also means we don't need a default case or some of theelsebranches, we can just leave it as-is)value.lat the endSome aspects of the bugs addressed here are a regression caused by #107075 (which I discovered after rebasing #111732), but some are pre-existing problems.