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>
#endif
#include <cinttypes>
#include <cstdio>
#include <cstring>
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<MemStat> 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<MemStat> 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<uint64>(value);
if (r_mem.is_error()) {
LOG(ERROR) << "Failed to parse memory stats " << tag("name", name) << tag("value", value);
*x = static_cast<uint64>(-1);
} else {
xx *= 1024; // memory is in kB
*x = static_cast<uint64>(xx);
*x = r_mem.ok() * 1024; // memory is in kB
}
}
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;
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<MemStat> mem_stat() TD_WARN_UNUSED_RESULT;