musikr: fix use-after-free in jni

This commit is contained in:
Alexander Capehart 2025-02-21 09:30:37 -07:00
parent a1d62c2a08
commit e442fcf253
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
2 changed files with 9 additions and 8 deletions

View file

@ -31,7 +31,7 @@ JInputStream::JInputStream(JNIEnv *env, jobject jInputStream) : env(env), jInput
if (!env->IsInstanceOf(jInputStream, *jInputStreamClass)) {
throw std::runtime_error("Object is not NativeInputStream");
}
jInputStreamNameMethod = jInputStreamClass.method("name",
jmethodID jInputStreamNameMethod = jInputStreamClass.method("name",
"()Ljava/lang/String;");
jInputStreamReadBlockMethod = jInputStreamClass.method("readBlock",
"(Ljava/nio/ByteBuffer;)Z");
@ -44,6 +44,9 @@ JInputStream::JInputStream(JNIEnv *env, jobject jInputStream) : env(env), jInput
"(J)Z");
jInputStreamTellMethod = jInputStreamClass.method("tell", "()J");
jInputStreamLengthMethod = jInputStreamClass.method("length", "()J");
JStringRef jName = { env, reinterpret_cast<jstring>(env->CallObjectMethod(
jInputStream, jInputStreamNameMethod)) };
_name = TagLib::String(env->GetStringUTFChars(*jName, nullptr));
}
JInputStream::~JInputStream() {
@ -51,11 +54,8 @@ JInputStream::~JInputStream() {
// so we don't need to delete any references here
}
TagLib::FileName JInputStream::name() const {
// Not actually used except in FileRef, can safely ignore.
JStringRef jName { env, reinterpret_cast<jstring>(env->CallObjectMethod(
jInputStream, jInputStreamNameMethod)) };
return jName.copy().toCString();
TagLib::FileName /* const char * */ JInputStream::name() const {
return _name.toCString(true);
}
TagLib::ByteVector JInputStream::readBlock(size_t length) {

View file

@ -23,6 +23,7 @@
#include "JObjectRef.h"
#include "taglib/tiostream.h"
#include "taglib/tstring.h"
class JInputStream: public TagLib::IOStream {
public:
@ -36,7 +37,7 @@ public:
/*!
* Returns the stream name in the local file system encoding.
*/
TagLib::FileName name() const override;
TagLib::FileName /* const char * */ name() const override;
/*!
* Reads a block of size \a length at the current get pointer.
@ -115,7 +116,7 @@ public:
private:
JNIEnv *env;
jobject jInputStream;
jmethodID jInputStreamNameMethod;
TagLib::String _name;
jmethodID jInputStreamReadBlockMethod;
jmethodID jInputStreamIsOpenMethod;
jmethodID jInputStreamSeekFromBeginningMethod;