From ae32f7e599aa0e54ceab737a3031c87dff059608 Mon Sep 17 00:00:00 2001 From: qiwang Date: Thu, 22 Aug 2019 11:25:30 +0800 Subject: [PATCH] Don't clear the output array if it failed to open the proc status file According to the original documentation, the functions in Process.java should return -1 when the process doesn't exist,this should be detected by Process.readProcLine() when it fails to open the /proc//status file. Unfortunately readProcLine() zeroes-out the output array before attempting to open the file, so the expected return value -1 is changed to be 0 when the process doesn't exist. It's inconsistent with the interface description definition, so correct it. Test: try to use Process.getUidForPid(int pid) api to read a non-existent process pid's uid, it will return -1 instead of 0. Merged-In: Ic2ac7defea8b15dd65f907d04e168a1283a4f621 Change-Id: Ic2ac7defea8b15dd65f907d04e168a1283a4f621 Signed-off-by: qiwang --- core/jni/android_util_Process.cpp | 16 +++++++--- .../coretests/src/android/os/ProcessTest.java | 31 ++++++++++++++++--- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/core/jni/android_util_Process.cpp b/core/jni/android_util_Process.cpp index 2d7069c50255e..f929dde4dcd17 100644 --- a/core/jni/android_util_Process.cpp +++ b/core/jni/android_util_Process.cpp @@ -644,6 +644,12 @@ static jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz) return si.totalram; } +/* + * The outFields array is initialized to -1 to allow the caller to identify + * when the status file (and therefore the process) they specified is invalid. + * This array should not be overwritten or cleared before we know that the + * status file can be read. + */ void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr, jobjectArray reqFields, jlongArray outFields) { @@ -692,14 +698,14 @@ void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileSt return; } - //ALOGI("Clearing %" PRId32 " sizes", count); - for (i=0; i= 0) { + //ALOGI("Clearing %" PRId32 " sizes", count); + for (i=0; i