[coverity] fix some issues found by coverity

pull/1006/head
Timothy Stack 2 years ago
parent 7eb4d1b6e6
commit 84ec014a8d

@ -87,8 +87,7 @@ public:
auto lock_path = archive_path;
lock_path += ".lck";
auto open_res
= lnav::filesystem::open_file(lock_path, O_CREAT | O_RDWR, 0600);
auto open_res = lnav::filesystem::create_file(lock_path, O_RDWR, 0600);
if (open_res.isErr()) {
throw std::runtime_error(open_res.unwrapErr());
}
@ -375,7 +374,7 @@ extract(const std::string& filename, const extract_cb& cb)
archive_read_close(arc);
archive_write_close(ext);
lnav::filesystem::open_file(done_path, O_CREAT | O_WRONLY, 0600);
lnav::filesystem::create_file(done_path, O_WRONLY, 0600);
return Ok();
}

@ -71,7 +71,7 @@ public:
}
return retval;
};
}
/**
* dup(2) the given file descriptor and wrap it in an auto_fd.
@ -99,10 +99,7 @@ public:
*
* @param fd The file descriptor to be managed.
*/
explicit auto_fd(int fd = -1) : af_fd(fd)
{
require(fd >= -1);
}
explicit auto_fd(int fd = -1) : af_fd(fd) { require(fd >= -1); }
/**
* Non-const copy constructor. Management of the file descriptor will be
@ -111,7 +108,7 @@ public:
*
* @param af The source of the file descriptor.
*/
auto_fd(auto_fd&& af) noexcept : af_fd(af.release()){};
auto_fd(auto_fd&& af) noexcept : af_fd(af.release()) {}
/**
* Const copy constructor. The file descriptor from the source will be
@ -137,14 +134,12 @@ public:
*/
~auto_fd()
{
this->reset();
};
this->reset(); }
/** @return The file descriptor as a plain integer. */
operator int() const
{
return this->af_fd;
};
return this->af_fd; }
/**
* Replace the current descriptor with the given one. The current
@ -159,7 +154,7 @@ public:
this->reset(fd);
return *this;
};
}
/**
* Transfer management of the given file descriptor to this object.
@ -171,7 +166,7 @@ public:
{
this->reset(af.release());
return *this;
};
}
/**
* Return a pointer that can be passed to functions that require an out
@ -183,7 +178,7 @@ public:
{
this->reset();
return &this->af_fd;
};
}
/**
* Stop managing the file descriptor in this object and return its value.
@ -196,15 +191,14 @@ public:
this->af_fd = -1;
return retval;
};
}
/**
* @return The file descriptor.
*/
int get() const
{
return this->af_fd;
};
return this->af_fd; }
/**
* Closes the current file descriptor and replaces its value with the given
@ -230,7 +224,7 @@ public:
}
this->af_fd = fd;
}
};
}
void close_on_exec() const
{

@ -73,6 +73,8 @@ public:
operator T*() const { return this->am_ptr; }
T* operator->() { return this->am_ptr; }
auto_mem& operator=(T* ptr)
{
this->reset(ptr);

@ -51,14 +51,14 @@ template<process_state ProcState>
class auto_pid {
public:
explicit auto_pid(pid_t child, int status = 0)
: ap_child(child), ap_status(status)
: ap_status(status), ap_child(child)
{
}
auto_pid(const auto_pid& other) = delete;
auto_pid(auto_pid&& other) noexcept
: ap_child(std::move(other).release()), ap_status(other.ap_status)
: ap_status(other.ap_status), ap_child(std::move(other).release())
{
}
@ -69,8 +69,9 @@ public:
auto_pid& operator=(auto_pid&& other) noexcept
{
auto other_status = other.ap_status;
this->reset(std::move(other).release());
this->ap_status = other.ap_status;
this->ap_status = other_status;
return *this;
}
@ -86,19 +87,18 @@ public:
static_assert(ProcState == process_state::running,
"this method is only available in the RUNNING state");
return this->ap_child == 0;
};
}
pid_t release() &&
{
return std::exchange(this->ap_child, -1);
};
return std::exchange(this->ap_child, -1); }
int status() const
{
static_assert(ProcState == process_state::finished,
"wait_for_child() must be called first");
return this->ap_status;
};
}
bool was_normal_exit() const
{
@ -159,8 +159,8 @@ public:
}
private:
pid_t ap_child;
int ap_status{0};
pid_t ap_child;
};
namespace lnav {

@ -38,9 +38,23 @@ namespace lnav {
namespace filesystem {
Result<auto_fd, std::string>
open_file(const ghc::filesystem::path& path, int flags, mode_t mode)
create_file(const ghc::filesystem::path& path, int flags, mode_t mode)
{
auto fd = openp(path, flags, mode);
auto fd = openp(path, flags | O_CREAT, mode);
if (fd == -1) {
return Err(fmt::format(FMT_STRING("Failed to open: {} -- {}"),
path.string(),
strerror(errno)));
}
return Ok(auto_fd(fd));
}
Result<auto_fd, std::string>
open_file(const ghc::filesystem::path& path, int flags)
{
auto fd = openp(path, flags);
if (fd == -1) {
return Err(fmt::format(FMT_STRING("Failed to open: {} -- {}"),

@ -59,9 +59,12 @@ openp(const ghc::filesystem::path& path, int flags, mode_t mode)
return open(path.c_str(), flags, mode);
}
Result<auto_fd, std::string> create_file(const ghc::filesystem::path& path,
int flags,
mode_t mode);
Result<auto_fd, std::string> open_file(const ghc::filesystem::path& path,
int flags,
mode_t mode);
int flags);
Result<struct stat, std::string> stat_file(const ghc::filesystem::path& path);

@ -58,6 +58,7 @@ compress(const void* input, size_t len)
zs.next_in = (Bytef*) input;
zs.avail_out = (uInt) retval.size();
zs.next_out = (Bytef*) retval.in();
zs.total_out = 0;
auto rc = deflateInit2(
&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY);
@ -87,6 +88,7 @@ uncompress(const std::string& src, const void* buffer, size_t size)
strm.next_in = (Bytef*) buffer;
strm.avail_in = size;
strm.total_in = 0;
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;

@ -33,6 +33,7 @@
#include <sys/socket.h>
#include <sys/types.h>
#include "auto_mem.hh"
#include "config.h"
#include "fmt/format.h"
@ -42,12 +43,13 @@ namespace tcp {
Result<auto_fd, std::string>
connect(const char* hostname, const char* servname)
{
struct addrinfo hints, *ai;
struct addrinfo hints;
auto_mem<addrinfo> ai(freeaddrinfo);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
auto rc = getaddrinfo(hostname, servname, &hints, &ai);
auto rc = getaddrinfo(hostname, servname, &hints, ai.out());
if (rc != 0) {
return Err(fmt::format(FMT_STRING("unable to resolve {}:{} -- {}"),

@ -544,9 +544,11 @@ protected:
bool lv_selectable{false};
vis_line_t lv_selection{0};
struct timeval lv_mouse_time;
struct timeval lv_mouse_time {
0, 0
};
int lv_scroll_accel{1};
int lv_scroll_velo;
int lv_scroll_velo{0};
int lv_mouse_y{-1};
lv_mode_t lv_mouse_mode{lv_mode_t::NONE};
vis_line_t lv_tail_space{1};

@ -2697,8 +2697,8 @@ SELECT tbl_name FROM sqlite_master WHERE sql LIKE 'CREATE VIRTUAL TABLE%'
stdin_tmp_path = temp_pair.first;
stdin_opts.so_out_fd = std::move(temp_pair.second);
} else {
auto open_res = lnav::filesystem::open_file(
stdin_opts.so_out, O_RDWR | O_CREAT | O_TRUNC, 0600);
auto open_res = lnav::filesystem::create_file(
stdin_opts.so_out, O_RDWR | O_TRUNC, 0600);
if (open_res.isErr()) {
fmt::print(stderr, "error: {}\n", open_res.unwrapErr());
return EXIT_FAILURE;

@ -142,7 +142,7 @@ public:
std::vector<logline_value>& values);
bool vi_supports_indexes{true};
int vi_column_count;
int vi_column_count{0};
string_attrs_t vi_attrs;
protected:

@ -41,6 +41,7 @@
#include <sys/wait.h>
#include <unistd.h>
#include "base/fs_util.hh"
#include "base/lnav_log.hh"
#include "config.h"
#include "line_buffer.hh"
@ -79,9 +80,15 @@ piper_proc::piper_proc(auto_fd pipefd, bool timestamp, auto_fd filefd)
line_buffer lb;
off_t woff = 0, last_woff = 0;
file_range last_range;
int nullfd;
nullfd = open("/dev/null", O_RDWR);
auto open_res = lnav::filesystem::open_file("/dev/null", O_RDWR);
if (open_res.isErr()) {
fprintf(stderr,
"unable to open /dev/null: %s\n",
open_res.unwrapErr().c_str());
exit(EXIT_FAILURE);
}
auto nullfd = open_res.unwrap();
if (pipefd != STDIN_FILENO) {
dup2(nullfd, STDIN_FILENO);
}

@ -201,7 +201,7 @@ struct prepared_stmt {
Result<void, std::string> execute()
{
auto rc = sqlite3_step(this->ps_stmt.in());
if (rc == SQLITE_OK && rc == SQLITE_DONE) {
if (rc == SQLITE_OK || rc == SQLITE_DONE) {
return Ok();
}

@ -959,11 +959,11 @@ static auto a = injector::bind_multiple<vtab_module_base>()
int
register_views_vtab(sqlite3* db)
{
char* errmsg;
if (sqlite3_exec(db, CREATE_FILTER_VIEW, nullptr, nullptr, &errmsg)
auto_mem<char> errmsg(sqlite3_free);
if (sqlite3_exec(db, CREATE_FILTER_VIEW, nullptr, nullptr, errmsg.out())
!= SQLITE_OK)
{
log_error("Unable to create filter view: %s", errmsg);
log_error("Unable to create filter view: %s", errmsg.in());
}
return 0;

Loading…
Cancel
Save