fix possible buffer overrun and memory leak

Use snprintf instead of sprintf and fclose() before return.

Change-Id: I3ed193464cc0dc90e9935ae19162667ad367628b
This commit is contained in:
Sungmin Choi
2012-12-21 14:24:33 +09:00
parent 9b76b2d29f
commit ec3d44cc7e

View File

@@ -951,13 +951,20 @@ static jboolean android_os_BinderProxy_isBinderAlive(JNIEnv* env, jobject obj)
}
static int getprocname(pid_t pid, char *buf, size_t len) {
char filename[20];
char filename[32];
FILE *f;
sprintf(filename, "/proc/%d/cmdline", pid);
snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
f = fopen(filename, "r");
if (!f) { *buf = '\0'; return 1; }
if (!fgets(buf, len, f)) { *buf = '\0'; return 2; }
if (!f) {
*buf = '\0';
return 1;
}
if (!fgets(buf, len, f)) {
*buf = '\0';
fclose(f);
return 2;
}
fclose(f);
return 0;
}