procmeminfo: use getline() instead of fgets() everywhere
Bug: 111694435
Test: libmeminfo_test 1 --gtest_filter=TestProcMemInfo.*
Change-Id: Idfc797aa65f45e0152765605c14622e2110dfdc1
Signed-off-by: Sandeep Patil <sspatil@google.com>
diff --git a/libmeminfo/procmeminfo.cpp b/libmeminfo/procmeminfo.cpp
index 347a293..d6332a3 100644
--- a/libmeminfo/procmeminfo.cpp
+++ b/libmeminfo/procmeminfo.cpp
@@ -406,9 +406,10 @@
return false;
}
- char line[1024];
+ char* line = nullptr;
+ size_t line_alloc = 0;
stats->clear();
- while (fgets(line, sizeof(line), fp.get()) != nullptr) {
+ while (getline(&line, &line_alloc, fp.get()) > 0) {
switch (line[0]) {
case 'P':
if (strncmp(line, "Pss:", 4) == 0) {
@@ -441,6 +442,8 @@
}
}
+ // free getline() managed buffer
+ free(line);
return true;
}
@@ -450,14 +453,17 @@
return false;
}
*pss = 0;
- char line[1024];
- while (fgets(line, sizeof(line), fp.get()) != nullptr) {
+ char* line = nullptr;
+ size_t line_alloc = 0;
+ while (getline(&line, &line_alloc, fp.get()) > 0) {
uint64_t v;
if (sscanf(line, "Pss: %" SCNu64 " kB", &v) == 1) {
*pss += v;
}
}
+ // free getline() managed buffer
+ free(line);
return true;
}