From f2a0fa2640c2d01cc875685a525a273a95d70e4e Mon Sep 17 00:00:00 2001 From: Mike Kolupaev Date: Thu, 8 Jun 2017 19:54:00 -0700 Subject: [PATCH] Fix crash in PosixWritableFile::Close() when fstat() fails Summary: We had a crash in this code: `fstat()` failed; `file_stats` contained garbage, in particular `file_stats.st_blksize == 6`; the expression `file_stats.st_blocks / (file_stats.st_blksize / 512)` divided by zero. Closes https://github.com/facebook/rocksdb/pull/2420 Differential Revision: D5216110 Pulled By: al13n321 fbshipit-source-id: 6d8fc5e7c4f98c1139e68c7829ebdbac68b0fce0 --- env/io_posix.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/env/io_posix.cc b/env/io_posix.cc index 1f63c2e2f..a25331f92 100644 --- a/env/io_posix.cc +++ b/env/io_posix.cc @@ -796,10 +796,11 @@ Status PosixWritableFile::Close() { // While we work with Travis-CI team to figure out if this is a // quirk of Docker/AUFS, we will comment this out. struct stat file_stats; - fstat(fd_, &file_stats); + int result = fstat(fd_, &file_stats); // After ftruncate, we check whether ftruncate has the correct behavior. // If not, we should hack it with FALLOC_FL_PUNCH_HOLE - if ((file_stats.st_size + file_stats.st_blksize - 1) / + if (result == 0 && + (file_stats.st_size + file_stats.st_blksize - 1) / file_stats.st_blksize != file_stats.st_blocks / (file_stats.st_blksize / 512)) { IOSTATS_TIMER_GUARD(allocate_nanos);