From f14dc8c26fdd6eeb5d5529b5ddc60da5162fb29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Fri, 1 Aug 2025 16:07:40 -0700 Subject: [PATCH] simplify netjniutils MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test: TreeHugger Flag: EXEMPT mainline Signed-off-by: Maciej Żenczykowski Change-Id: Ic1c3207ef3a800f1e5d2da846b1f010f64a0e18f --- staticlibs/native/netjniutils/netjniutils.cpp | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/staticlibs/native/netjniutils/netjniutils.cpp b/staticlibs/native/netjniutils/netjniutils.cpp index 8b7f903f1a..ef49cf657c 100644 --- a/staticlibs/native/netjniutils/netjniutils.cpp +++ b/staticlibs/native/netjniutils/netjniutils.cpp @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include @@ -28,10 +28,7 @@ namespace android { namespace netjniutils { -namespace { - - -int GetNativeFileDescriptorWithoutNdk(JNIEnv* env, jobject javaFd) { +static int GetNativeFileDescriptorWithoutNdk(JNIEnv* env, jobject javaFd) { // Prior to Android S, we need to find the descriptor field in the FileDescriptor class. The // symbol name has been stable in libcore, but is a private implementation detail. // Older libnativehelper_compat_c++ versions had a jniGetFdFromFileDescriptor method, but this @@ -43,14 +40,15 @@ int GetNativeFileDescriptorWithoutNdk(JNIEnv* env, jobject javaFd) { env->DeleteLocalRef(cls); if (fieldID == nullptr) { __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "Failed to get descriptor field."); + abort(); } return fieldID; }(); - return javaFd != nullptr ? env->GetIntField(javaFd, descriptorFieldID) : -1; + return env->GetIntField(javaFd, descriptorFieldID); } -int GetNativeFileDescriptorWithNdk(JNIEnv* env, jobject javaFd) { +static int GetNativeFileDescriptorWithNdk(JNIEnv* env, jobject javaFd) { // Since Android S, there is an NDK API to get a file descriptor present in libnativehelper.so. // libnativehelper is loaded into all processes by the zygote since the zygote uses it // to load the Android Runtime and is also a public library (because of the NDK API). @@ -62,18 +60,17 @@ int GetNativeFileDescriptorWithNdk(JNIEnv* env, jobject javaFd) { __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "Failed to dlsym(AFileDescriptor_getFd): %s", dlerror()); dlclose(handle); + abort(); } return ndkGetFd; }(); - return javaFd != nullptr ? ndkGetFd(env, javaFd) : -1; + return ndkGetFd(env, javaFd); } -} // namespace - int GetNativeFileDescriptor(JNIEnv* env, jobject javaFd) { - static const bool preferNdkFileDescriptorApi = []() -> bool - { return android::modules::sdklevel::IsAtLeastS(); }(); + if (!javaFd) return -1; + static const bool preferNdkFileDescriptorApi = modules::sdklevel::IsAtLeastS(); if (preferNdkFileDescriptorApi) { return GetNativeFileDescriptorWithNdk(env, javaFd); } else {