Avoid returning value on stack
AMediaFormat_getString() returnes a string not guaranteed to live beyond
the call. The underlying implementation of String8 seems to have made
this safe by using SharedBuffers, but that's luck of the implementatoon,
not a guarantee of the API. We instead get our value by chasing through
where we just added it to the longer-lived vector.
Bug: 193904641
Test: atest libmedkandk_test
Test: atest android.mediav2.cts.MediaFormatUnitTest (master)
Change-Id: I7df35b7dbccf24c72cfd977a6b13d85aee8ae555
diff --git a/media/ndk/NdkMediaFormat.cpp b/media/ndk/NdkMediaFormat.cpp
index c1793ce..51f6c78 100644
--- a/media/ndk/NdkMediaFormat.cpp
+++ b/media/ndk/NdkMediaFormat.cpp
@@ -200,8 +200,11 @@
AString tmp;
if (mData->mFormat->findString(name, &tmp)) {
String8 ret(tmp.c_str());
- mData->mStringCache.add(String8(name), ret);
- *out = ret.string();
+ ssize_t i = mData->mStringCache.add(String8(name), ret);
+ if (i < 0) {
+ return false;
+ }
+ *out = mData->mStringCache.valueAt(i).string();
return true;
}
return false;