From 3cc8ce6ba0934b1a4d9db184daf055c1207ef105 Mon Sep 17 00:00:00 2001 From: arostovtsev Date: Thu, 4 Feb 2021 09:19:30 -0800 Subject: [PATCH] Propagate OOME if NewByteArray allocation failed It's possible for NewByteArray to fail when the JVM is running out of memory. In that case, we want to propagate the OOME to the caller instead of segfaulting in the JVM. RELNOTES: None. PiperOrigin-RevId: 355641836 --- src/main/native/unix_jni.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/native/unix_jni.cc b/src/main/native/unix_jni.cc index 5518a96017c5c7..bd925d16328d64 100644 --- a/src/main/native/unix_jni.cc +++ b/src/main/native/unix_jni.cc @@ -1015,7 +1015,11 @@ static jbyteArray getxattr_common(JNIEnv *env, } } else { result = env->NewByteArray(size); - env->SetByteArrayRegion(result, 0, size, value); + // Result may be NULL if allocation failed. In that case, we'll return the + // NULL and an OOME will be thrown when we are back in Java. + if (result != NULL) { + env->SetByteArrayRegion(result, 0, size, value); + } } ReleaseStringLatin1Chars(path_chars); ReleaseStringLatin1Chars(name_chars);