diff --git a/tdutils/td/utils/port/Stat.cpp b/tdutils/td/utils/port/Stat.cpp index e0028e3b..54fb05d7 100644 --- a/tdutils/td/utils/port/Stat.cpp +++ b/tdutils/td/utils/port/Stat.cpp @@ -35,10 +35,6 @@ #include #endif -#include -#include -#include - namespace td { namespace detail { Stat from_native_stat(const struct ::stat &buf) { @@ -68,8 +64,7 @@ Stat fstat(int native_fd) { struct ::stat buf; int err = fstat(native_fd, &buf); auto fstat_errno = errno; - LOG_IF(FATAL, err < 0) << Status::PosixError(fstat_errno, PSLICE() - << "stat of " << tag("fd", native_fd) << " failed"); + LOG_IF(FATAL, err < 0) << Status::PosixError(fstat_errno, PSLICE() << "stat of fd " << native_fd << " failed"); return detail::from_native_stat(buf); } @@ -166,7 +161,6 @@ Result mem_stat() { const char *s = mem; MemStat res; - std::memset(&res, 0, sizeof(res)); while (*s) { const char *name_begin = s; while (*s != 0 && *s != '\n') { @@ -192,14 +186,13 @@ Result mem_stat() { x = &res.resident_size_; } if (x != nullptr) { - unsigned long long xx; - if (name_end == s || name_end + 1 == s || std::sscanf(name_end + 1, "%llu", &xx) != 1) { - LOG(ERROR) << "Failed to parse memory stats" << tag("line", Slice(name_begin, s)) - << tag(":number", Slice(name_end, s)); + Slice value(name_end, s); + auto r_mem = to_integer_safe(value); + if (r_mem.is_error()) { + LOG(ERROR) << "Failed to parse memory stats " << tag("name", name) << tag("value", value); *x = static_cast(-1); } else { - xx *= 1024; // memory is in kB - *x = static_cast(xx); + *x = r_mem.ok() * 1024; // memory is in kB } } if (*s == 0) { diff --git a/tdutils/td/utils/port/Stat.h b/tdutils/td/utils/port/Stat.h index 40d54e51..73b1bfc0 100644 --- a/tdutils/td/utils/port/Stat.h +++ b/tdutils/td/utils/port/Stat.h @@ -33,10 +33,10 @@ Stat fstat(int native_fd); // TODO return Result Status update_atime(CSlice path) TD_WARN_UNUSED_RESULT; struct MemStat { - uint64 resident_size_; - uint64 resident_size_peak_; - uint64 virtual_size_; - uint64 virtual_size_peak_; + uint64 resident_size_ = 0; + uint64 resident_size_peak_ = 0; + uint64 virtual_size_ = 0; + uint64 virtual_size_peak_ = 0; }; Result mem_stat() TD_WARN_UNUSED_RESULT;