Use to_integer_safe instead of sscanf in mem_stat.

GitOrigin-RevId: a1ce7c920596c4d4198488048d1592b72d871ebe
This commit is contained in:
levlam 2018-02-11 16:05:51 +03:00
parent b6e6eac530
commit 834fa51b45
2 changed files with 10 additions and 17 deletions

View File

@ -35,10 +35,6 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#endif #endif
#include <cinttypes>
#include <cstdio>
#include <cstring>
namespace td { namespace td {
namespace detail { namespace detail {
Stat from_native_stat(const struct ::stat &buf) { Stat from_native_stat(const struct ::stat &buf) {
@ -68,8 +64,7 @@ Stat fstat(int native_fd) {
struct ::stat buf; struct ::stat buf;
int err = fstat(native_fd, &buf); int err = fstat(native_fd, &buf);
auto fstat_errno = errno; auto fstat_errno = errno;
LOG_IF(FATAL, err < 0) << Status::PosixError(fstat_errno, PSLICE() LOG_IF(FATAL, err < 0) << Status::PosixError(fstat_errno, PSLICE() << "stat of fd " << native_fd << " failed");
<< "stat of " << tag("fd", native_fd) << " failed");
return detail::from_native_stat(buf); return detail::from_native_stat(buf);
} }
@ -166,7 +161,6 @@ Result<MemStat> mem_stat() {
const char *s = mem; const char *s = mem;
MemStat res; MemStat res;
std::memset(&res, 0, sizeof(res));
while (*s) { while (*s) {
const char *name_begin = s; const char *name_begin = s;
while (*s != 0 && *s != '\n') { while (*s != 0 && *s != '\n') {
@ -192,14 +186,13 @@ Result<MemStat> mem_stat() {
x = &res.resident_size_; x = &res.resident_size_;
} }
if (x != nullptr) { if (x != nullptr) {
unsigned long long xx; Slice value(name_end, s);
if (name_end == s || name_end + 1 == s || std::sscanf(name_end + 1, "%llu", &xx) != 1) { auto r_mem = to_integer_safe<uint64>(value);
LOG(ERROR) << "Failed to parse memory stats" << tag("line", Slice(name_begin, s)) if (r_mem.is_error()) {
<< tag(":number", Slice(name_end, s)); LOG(ERROR) << "Failed to parse memory stats " << tag("name", name) << tag("value", value);
*x = static_cast<uint64>(-1); *x = static_cast<uint64>(-1);
} else { } else {
xx *= 1024; // memory is in kB *x = r_mem.ok() * 1024; // memory is in kB
*x = static_cast<uint64>(xx);
} }
} }
if (*s == 0) { if (*s == 0) {

View File

@ -33,10 +33,10 @@ Stat fstat(int native_fd); // TODO return Result<Stat>
Status update_atime(CSlice path) TD_WARN_UNUSED_RESULT; Status update_atime(CSlice path) TD_WARN_UNUSED_RESULT;
struct MemStat { struct MemStat {
uint64 resident_size_; uint64 resident_size_ = 0;
uint64 resident_size_peak_; uint64 resident_size_peak_ = 0;
uint64 virtual_size_; uint64 virtual_size_ = 0;
uint64 virtual_size_peak_; uint64 virtual_size_peak_ = 0;
}; };
Result<MemStat> mem_stat() TD_WARN_UNUSED_RESULT; Result<MemStat> mem_stat() TD_WARN_UNUSED_RESULT;