Skip to content

Commit

Permalink
Merge pull request chenxiaolong#1234 from chenxiaolong/libmiscstuff-m…
Browse files Browse the repository at this point in the history
…emory-leak

libmiscstuff: Fix memory leak if throwing Java exception via JNI fails
  • Loading branch information
chenxiaolong authored Jul 1, 2018
2 parents 5438ac5 + 9373af3 commit 3c2baa9
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions libmiscstuff-jni/libmiscstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,29 @@ MB_PRINTF(3, 4)
static bool throw_exception(JNIEnv *env, const char *class_name,
const char *fmt, ...)
{
jclass clazz;
char *buf;
int ret;
va_list ap;

va_start(ap, fmt);
ret = vasprintf(&buf, fmt, ap);
int ret = vasprintf(&buf, fmt, ap);
va_end(ap);

if (ret < 0) {
// Can't propagate the error any further
abort();
}

clazz = env->FindClass(class_name);
auto free_buf = finally([&] {
free(buf);
});

jclass clazz = env->FindClass(class_name);
if (!clazz) {
// Java will throw an exception after returning
return false;
}

ret = env->ThrowNew(clazz, buf);
free(buf);

return ret == 0;
return env->ThrowNew(clazz, buf) == 0;
}

JNIEXPORT void JNICALL
Expand Down

0 comments on commit 3c2baa9

Please sign in to comment.