Skip to content

Commit

Permalink
* Fixed errors found by PVS studio
Browse files Browse the repository at this point in the history
  • Loading branch information
adamansky committed Apr 23, 2020
1 parent 2f0c825 commit c960b56
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 86 deletions.
6 changes: 6 additions & 0 deletions Changelog
@@ -1,3 +1,9 @@
iowow (1.4.3) UNRELEASED; urgency=medium

* Fixed errors found by PVS studio

-- Anton Adamansky <adamansky@gmail.com> Thu, 23 Apr 2020 18:39:39 +0700

iowow (1.4.2) testing; urgency=medium

* Added IWULIST, IWLIST implementation (iwarr.h)
Expand Down
19 changes: 19 additions & 0 deletions pvs_studio_analyze.sh
@@ -0,0 +1,19 @@
#!/bin/bash
set -e

SCRIPTPATH="$(
cd "$(dirname "$0")"
pwd -P
)"
cd $SCRIPTPATH

if [ ! -d ./build ]; then
mkdir ./build
fi

cd ./build
cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release \
&& make
pvs-studio-analyzer analyze -a 45 -l ${HOME}/.config/PVS-Studio/PVS-Studio.lic -j4 -o ./pvs.log
rm -rf ./pvs_report
plog-converter -a 'GA:1,2,3;64:1;OP:1,2,3;MISRA:1' -t fullhtml -o ./pvs_report ./pvs.log
16 changes: 8 additions & 8 deletions src/fs/iwexfile.c
Expand Up @@ -39,18 +39,18 @@
struct MMAPSLOT;
typedef struct IWFS_EXT_IMPL {
IWFS_FILE file; /**< Underlying file */
IWDLSNR *dlsnr; /**< Data events listener */
pthread_rwlock_t *rwlock; /**< Thread RW lock */
struct MMAPSLOT *mmslots; /**< Memory mapping slots */
void *rspolicy_ctx; /**< Custom opaque data for policy functions */
IW_EXT_RSPOLICY rspolicy; /**< File resize policy function ptr */
uint64_t fsize; /**< Current file size */
uint64_t maxoff; /**< Maximum allowed file offset. Unlimited if zero.
If maximum offset is reached `IWFS_ERROR_MAXOFF` will be reported. */
pthread_rwlock_t *rwlock; /**< Thread RW lock */
IW_EXT_RSPOLICY rspolicy; /**< File resize policy function ptr */
void *rspolicy_ctx; /**< Custom opaque data for policy functions */
struct MMAPSLOT *mmslots; /**< Memory mapping slots */
IWDLSNR *dlsnr; /**< Data events listener */
HANDLE fh; /**< File handle */
size_t psize; /**< System page size */
bool use_locks; /**< Use rwlocks to guard method access */
HANDLE fh; /**< File handle */
iwfs_omode omode; /**< File open mode */
bool use_locks; /**< Use rwlocks to guard method access */
} EXF;

typedef struct MMAPSLOT {
Expand Down Expand Up @@ -154,7 +154,7 @@ static iwrc _exfile_initmmap_slot_lw(struct IWFS_EXT *f, MMAPSLOT *s) {
: MAP_SHARED;
int prot = (impl->omode & IWFS_OWRITE) ? (PROT_WRITE + PROT_READ) : (PROT_READ);
s->len = nlen;
s->mmap = mmap(s->mmap, s->len, prot, flags, impl->fh, s->off);
s->mmap = mmap(s->mmap, s->len, prot, flags, impl->fh, s->off); // -V774
if (s->mmap == MAP_FAILED) {
iwrc rc = iwrc_set_errno(IW_ERROR_ERRNO, errno);
iwlog_ecode_error3(rc);
Expand Down
2 changes: 1 addition & 1 deletion src/fs/iwfile.c
Expand Up @@ -206,7 +206,7 @@ iwrc iwfs_file_open(IWFS_FILE *f, const IWFS_FILE_OPTS *_opts) {
}

if (!opts->lock_mode) {
opts->lock_mode = IWFS_DEFAULT_LOCKMODE;
opts->lock_mode = IWFS_DEFAULT_LOCKMODE; // -V1048
}
if (!opts->omode) {
opts->omode = IWFS_DEFAULT_OMODE;
Expand Down
13 changes: 9 additions & 4 deletions src/fs/iwfsmfile.c
Expand Up @@ -81,6 +81,8 @@ typedef uint8_t fsm_bmopts_t;

IW_INLINE int _fsm_cmp_ptr(const FSMBK *a, const FSMBK *b);
#define _fsm_cmp(a_, b_) (_fsm_cmp_ptr(&(a_), &(b_)))

// -V:KBTREE_INIT:522, 641
KBTREE_INIT(fsm, FSMBK, _fsm_cmp)

struct IWFS_FSM_IMPL {
Expand Down Expand Up @@ -488,6 +490,7 @@ static iwrc _fsm_blk_allocate_aligned_lw(FSM *impl,
} \
} \
}
//-V:__kb_traverse:576, 701, 619, 769, 522
__kb_traverse(FSMBK, impl->fsm, _fsm_traverse);
#undef _fsm_traverse

Expand Down Expand Up @@ -519,6 +522,7 @@ static void _fsm_load_fsm_lw(FSM *impl, const uint8_t *bm, uint64_t len) {
uint64_t fbklength = 0;
uint64_t fbkoffset = 0;
if (impl->fsm) {
// -V:kb_destroy:701, 769
kb_destroy(fsm, impl->fsm);
}
impl->fsm = kb_init(fsm, KB_DEFAULT_SIZE);
Expand Down Expand Up @@ -1053,7 +1057,7 @@ static iwrc _fsm_resize_fsm_bitmap_lw(FSM *impl, uint64_t size) {
bmoffset = bmoffset << impl->bpow;
bmlen = sp << impl->bpow;
} else if (rc == IWFS_ERROR_NO_FREE_SPACE) {
bmoffset = impl->bmlen * (1U << impl->bpow) * 8U;
bmoffset = impl->bmlen * (1ULL << impl->bpow) * 8U;
bmoffset = IW_ROUNDUP(bmoffset, impl->aunit);
}
if (!impl->mmap_all) {
Expand Down Expand Up @@ -1211,7 +1215,7 @@ static iwrc _fsm_trim_tail_lw(FSM *impl) {
RCGO(rc, finish);
}

rc = _fsm_bmptr(impl, &bmptr);
rc = _fsm_bmptr(impl, &bmptr); // -V519
RCGO(rc, finish);

lastblk = (impl->bmoff + impl->bmlen) >> impl->bpow;
Expand Down Expand Up @@ -1297,7 +1301,7 @@ static iwrc _fsm_read_meta_lr(FSM *impl) {
}

/* Magic */
memcpy(&lv, hdr + rp, sizeof(lv));
memcpy(&lv, hdr + rp, sizeof(lv)); // -V512
lv = IW_ITOHL(lv);
if (lv != IWFSM_MAGICK) {
rc = IWFS_ERROR_INVALID_FILEMETA;
Expand Down Expand Up @@ -1541,6 +1545,7 @@ static iwrc _fsm_close(struct IWFS_FSM *f) {
}
IWRC(impl->pool.close(&impl->pool), rc);
if (impl->fsm) {
// -V:__kb_destroy:701, 769
__kb_destroy(impl->fsm);
}
IWRC(_fsm_ctrl_unlock(impl), rc);
Expand Down Expand Up @@ -1643,7 +1648,7 @@ static iwrc _fsm_reallocate(struct IWFS_FSM *f, off_t nlen, off_t *oaddr, off_t
return IWFS_ERROR_RANGE_NOT_ALIGNED;
}
uint64_t sp;
uint64_t nlen_blk = IW_ROUNDUP((uint64_t) nlen, 1U << impl->bpow) >> impl->bpow;
uint64_t nlen_blk = IW_ROUNDUP((uint64_t) nlen, 1ULL << impl->bpow) >> impl->bpow;
uint64_t olen_blk = (uint64_t) * olen >> impl->bpow;
uint64_t oaddr_blk = (uint64_t) * oaddr >> impl->bpow;
uint64_t naddr_blk = oaddr_blk;
Expand Down
4 changes: 2 additions & 2 deletions src/kv/dbg/iwkvdbg.c
Expand Up @@ -10,7 +10,7 @@ void iwkvd_kvblk(FILE *f, KVBLK *kb, int maxvlen) {
uint32_t klen, vlen;
IWFS_FSM *fsm = &kb->db->iwkv->fsm;
blkn_t blkn = ADDR2BLK(kb->addr);
fprintf(f, "\n === KVBLK[%u] maxoff=%" PRIx64 ", zidx=%d, idxsz=%d, szpow=%u, flg=%x, db=%d\n",
fprintf(f, "\n === KVBLK[%u] maxoff=%" PRIx64 ", zidx=%d, idxsz=%d, szpow=%u, flg=%x, db=%d\n", // -V576
blkn, (int64_t) kb->maxoff, kb->zidx, kb->idxsz, kb->szpow, kb->flags, kb->db->id);

iwrc rc = fsm->probe_mmap(fsm, 0, &mm, 0);
Expand Down Expand Up @@ -72,7 +72,7 @@ iwrc iwkvd_sblk(FILE *f, IWLCTX *lx, SBLK *sb, int flags) {
sb->p0,
sb->kvblk->db->id);

fprintf(f, "\n === SBLK[%u] szpow=%d, lkl=%d, lk=%s\n", blkn, sb->kvblk->szpow, lkl, lkbuf);
fprintf(f, "\n === SBLK[%u] szpow=%d, lkl=%d, lk=%s\n", blkn, sb->kvblk->szpow, lkl, lkbuf); // -V576

for (int i = 0, j = 0; i < sb->pnum; ++i, ++j) {
if (j == 3) {
Expand Down
4 changes: 1 addition & 3 deletions src/kv/iwal.c
Expand Up @@ -125,9 +125,7 @@ static void _destroy(IWAL *wal) {
pthread_mutex_destroy(wal->mtxp);
wal->mtxp = 0;
}
if (wal->path) {
free(wal->path);
}
free(wal->path);
if (wal->buf) {
wal->buf -= sizeof(WBSEP);
free(wal->buf);
Expand Down

0 comments on commit c960b56

Please sign in to comment.