fix possible buffer overrun in librank
Use snprintf instead of sprintf and fclose() before return.
Change-Id: I2c367b2e7b943f4ed10551c18ecc971e6b830c66
Signed-off-by: Daegeun Song <daegeun.song@lge.com>
diff --git a/librank/librank.c b/librank/librank.c
index 2e3c3fc..28322b9 100644
--- a/librank/librank.c
+++ b/librank/librank.c
@@ -443,13 +443,20 @@
}
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;
}