Fix open(2) failure check Coverity CID #1021880
Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/d823954c Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/d823954c Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/d823954c Branch: refs/heads/master Commit: d823954cbf0dc9c7f9c4498dcde3fa14a1bcc230 Parents: a020cb2 Author: James Peach <[email protected]> Authored: Mon Dec 29 21:23:18 2014 -0800 Committer: James Peach <[email protected]> Committed: Fri Jan 2 12:45:51 2015 -0800 ---------------------------------------------------------------------- proxy/logging/LogFile.cc | 47 ++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d823954c/proxy/logging/LogFile.cc ---------------------------------------------------------------------- diff --git a/proxy/logging/LogFile.cc b/proxy/logging/LogFile.cc index e86a7ef..6f2a91f 100644 --- a/proxy/logging/LogFile.cc +++ b/proxy/logging/LogFile.cc @@ -865,32 +865,33 @@ MetaInfo::_read_from_file() void MetaInfo::_write_to_file() { - int fd = open(_filename, O_WRONLY | O_CREAT | O_TRUNC, - Log::config->logfile_perm); - - if (fd <= 0) { + int fd = open(_filename, O_WRONLY | O_CREAT | O_TRUNC, Log::config->logfile_perm); + if (fd < 0) { Warning("Could not open metafile %s for writing: %s", _filename, strerror(errno)); - } else { - int n; - if (_flags & VALID_CREATION_TIME) { - n = snprintf(_buffer, BUF_SIZE, "creation_time = %lu\n", (unsigned long) _creation_time); - // TODO modify this runtime check so that it is not an assertion - ink_release_assert(n <= BUF_SIZE); - if (write(fd, _buffer, n) == -1) { - Warning("Could not write creation_time"); - } + return; + } + + int n; + if (_flags & VALID_CREATION_TIME) { + n = snprintf(_buffer, BUF_SIZE, "creation_time = %lu\n", (unsigned long) _creation_time); + // TODO modify this runtime check so that it is not an assertion + ink_release_assert(n <= BUF_SIZE); + if (write(fd, _buffer, n) == -1) { + Warning("Could not write creation_time"); } - if (_flags & VALID_SIGNATURE) { - n = snprintf(_buffer, BUF_SIZE, "object_signature = %" PRIu64 "\n", _log_object_signature); - // TODO modify this runtime check so that it is not an assertion - ink_release_assert(n <= BUF_SIZE); - if (write(fd, _buffer, n) == -1) { - Warning("Could not write object_signaure"); - } - Debug("log-meta", "MetaInfo::_write_to_file\n" - "\tfilename = %s\n" - "\tsignature value = %" PRIu64 "\n" "\tsignature string = %s", _filename, _log_object_signature, _buffer); + } + + if (_flags & VALID_SIGNATURE) { + n = snprintf(_buffer, BUF_SIZE, "object_signature = %" PRIu64 "\n", _log_object_signature); + // TODO modify this runtime check so that it is not an assertion + ink_release_assert(n <= BUF_SIZE); + if (write(fd, _buffer, n) == -1) { + Warning("Could not write object_signaure"); } + Debug("log-meta", "MetaInfo::_write_to_file\n" + "\tfilename = %s\n" + "\tsignature value = %" PRIu64 "\n" "\tsignature string = %s", _filename, _log_object_signature, _buffer); } + close(fd); }
