From bf8507f3ecab436e8f60c7d76a30441fde0f8145 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Tue, 4 Jul 2017 11:41:50 +0300 Subject: [PATCH 1/8] mdbx: fix mdbx_env_info() to avoid null-deref in lck-free mode (coverity). Change-Id: Ica8fe6c7f5a18af3a4d7d38ce8a1a092d5f1b2f7 --- src/mdbx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mdbx.c b/src/mdbx.c index 63c4b958..531fb3a8 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -9653,7 +9653,7 @@ int __cold mdbx_env_info(MDBX_env *env, MDBX_envinfo *arg, size_t bytes) { arg->me_recent_txnid != mdbx_meta_txnid_fluid(env, meta))); arg->me_maxreaders = env->me_maxreaders; - arg->me_numreaders = env->me_lck->mti_numreaders; + arg->me_numreaders = env->me_lck ? env->me_lck->mti_numreaders : INT32_MAX; arg->me_dxb_pagesize = env->me_psize; arg->me_sys_pagesize = env->me_os_psize; From 93d92db43dad6bf2d03a47cac19c68e50f604e97 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Tue, 4 Jul 2017 11:51:40 +0300 Subject: [PATCH 2/8] mdbx: fix mischecking in mdbx_reader_check0() (coverity). Change-Id: I2e3aaba1426c3b152a39b90f6d171380948da0a7 --- src/mdbx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mdbx.c b/src/mdbx.c index 531fb3a8..0adaa626 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -10279,7 +10279,7 @@ int __cold mdbx_reader_check0(MDBX_env *env, int rdt_locked, int *dead) { /* stale reader found */ if (!rdt_locked) { err = mdbx_rdt_lock(env); - if (MDBX_IS_ERROR(rc)) { + if (MDBX_IS_ERROR(err)) { rc = err; break; } @@ -10296,7 +10296,7 @@ int __cold mdbx_reader_check0(MDBX_env *env, int rdt_locked, int *dead) { continue; err = mdbx_rpid_check(env, pid); - if (MDBX_IS_ERROR(rc)) { + if (MDBX_IS_ERROR(err)) { rc = err; break; } From beda6902271549e91304fdc2b1d6bf59e381e371 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Tue, 4 Jul 2017 12:17:09 +0300 Subject: [PATCH 3/8] mdbx: fix resource-leak in mdbx_env_open_ex() in case of error (coverity). Change-Id: I9e808e2de4a64d05205a79c9d90feacc87ec930d --- src/mdbx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mdbx.c b/src/mdbx.c index 0adaa626..4382be23 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -4924,7 +4924,7 @@ int __cold mdbx_env_open_ex(MDBX_env *env, const char *path, unsigned flags, rc = mdbx_rthc_alloc(&env->me_txkey, &env->me_lck->mti_readers[0], &env->me_lck->mti_readers[env->me_maxreaders]); if (unlikely(rc != MDBX_SUCCESS)) - return rc; + goto bailout; env->me_flags |= MDBX_ENV_TXKEY; } From 136e98fb934ba048ab031763c94eac895ca84aa8 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Tue, 4 Jul 2017 12:24:14 +0300 Subject: [PATCH 4/8] test: fix initialization 'signalled' field (coverity). Change-Id: Ie6dae6c419bd81f203968bf07f2cf472ea7344a5 --- test/test.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.h b/test/test.h index 98b65801..0a9b8a38 100644 --- a/test/test.h +++ b/test/test.h @@ -136,7 +136,7 @@ protected: public: testcase(const actor_config &config, const mdbx_pid_t pid) - : config(config), pid(pid), nops_completed(0) { + : config(config), pid(pid), signalled(false), nops_completed(0) { start_timestamp.reset(); memset(&last, 0, sizeof(last)); } From 66f8327642386291103d78985aca68e33a6a564f Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Tue, 4 Jul 2017 12:36:00 +0300 Subject: [PATCH 5/8] test: allow null as 'function name' for logging (coverity). Change-Id: I047a4c372514e85d19dd3d3719f8ad3be046171e --- test/test.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/test.cc b/test/test.cc index 50426014..9a265e33 100644 --- a/test/test.cc +++ b/test/test.cc @@ -76,9 +76,10 @@ static void mdbx_debug_logger(int type, const char *function, int line, if (type & MDBX_DBG_PRINT) level = logging::verbose; + if (!function) + function = "unknown"; if (type & MDBX_DBG_ASSERT) { - log_error("mdbx: assertion failure: %s, %d", - function ? function : "unknown", line); + log_error("mdbx: assertion failure: %s, %d", function, line); level = logging::failure; } From 14484a6f32722e7c20ccb34bad5fb85e3adc585f Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Tue, 4 Jul 2017 13:06:32 +0300 Subject: [PATCH 6/8] mdbx: fix minor defects (coverity). Change-Id: I1a5b0788a87ab2a138b342140648642fd5855ae3 --- src/mdbx.c | 6 +++++- src/osal.c | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/mdbx.c b/src/mdbx.c index 4382be23..5182c2bf 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -3849,8 +3849,10 @@ static int mdbx_sync_locked(MDBX_env *env, unsigned flags, target = mdbx_meta_ancient(env, meta1, meta2, true); else if (head == meta1) target = mdbx_meta_ancient(env, meta0, meta2, true); - else if (head == meta2) + else { + mdbx_assert(env, head == meta2); target = mdbx_meta_ancient(env, meta0, meta1, true); + } /* LY: step#2 - update meta-page. */ mdbx_debug( @@ -6190,6 +6192,7 @@ static int mdbx_cursor_first(MDBX_cursor *mc, MDBX_val *key, MDBX_val *data) { if (likely(data)) { if (F_ISSET(leaf->mn_flags, F_DUPDATA)) { + mdbx_cassert(mc, mc->mc_xcursor != nullptr); mdbx_xcursor_init1(mc, leaf); rc = mdbx_cursor_first(&mc->mc_xcursor->mx_cursor, data, NULL); if (unlikely(rc)) @@ -6233,6 +6236,7 @@ static int mdbx_cursor_last(MDBX_cursor *mc, MDBX_val *key, MDBX_val *data) { if (likely(data)) { if (F_ISSET(leaf->mn_flags, F_DUPDATA)) { + mdbx_cassert(mc, mc->mc_xcursor != nullptr); mdbx_xcursor_init1(mc, leaf); rc = mdbx_cursor_last(&mc->mc_xcursor->mx_cursor, data, NULL); if (unlikely(rc)) diff --git a/src/osal.c b/src/osal.c index 08ae9fef..ae28d191 100644 --- a/src/osal.c +++ b/src/osal.c @@ -549,7 +549,7 @@ int mdbx_write(mdbx_filehandle_t fd, const void *buf, size_t bytes) { sigset_t set, old; sigemptyset(&set); sigaddset(&set, SIGPIPE); - int rc = rc = pthread_sigmask(SIG_BLOCK, &set, &old); + int rc = pthread_sigmask(SIG_BLOCK, &set, &old); if (rc != 0) return rc; #endif From d01a97f729e53931fae62954904be4abc8bf2ea0 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Tue, 4 Jul 2017 13:06:54 +0300 Subject: [PATCH 7/8] tools: fix minor defects (coverity). Change-Id: I3a80dcb31c54718d22cdca6272aa028685956243 --- src/tools/mdbx_chk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/mdbx_chk.c b/src/tools/mdbx_chk.c index ad8f85c8..ccc0592b 100644 --- a/src/tools/mdbx_chk.c +++ b/src/tools/mdbx_chk.c @@ -229,7 +229,7 @@ static int pgvisitor(uint64_t pgno, unsigned pgnumber, void *ctx, if (type) { uint64_t page_bytes = payload_bytes + header_bytes + unused_bytes; - uint64_t page_size = pgnumber * envstat.ms_psize; + size_t page_size = (size_t)pgnumber * envstat.ms_psize; int index = pagemap_lookup_dbi(dbi); if (index < 0) return ENOMEM; From e4a8a144b64de12b1aea7dbe9fc700c740228d03 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Tue, 4 Jul 2017 13:07:08 +0300 Subject: [PATCH 8/8] test: fix minor defects (coverity). Change-Id: I87165ca771a717815a2c81c36fcf1e9add2536bb --- test/log.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/log.cc b/test/log.cc index eee0fffe..5ab99513 100644 --- a/test/log.cc +++ b/test/log.cc @@ -125,9 +125,11 @@ bool output(const logging::loglevel priority, const char *format, va_list ap) { switch (end) { default: putc('\n', last); + // fall through case '\n': fflush(last); last = nullptr; + // fall through case ' ': case '_': case ':':