Skip to content

Commit

Permalink
Solved issues reported by static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
torrentg committed Dec 28, 2022
1 parent f0dc295 commit fc075c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ coverage: cqueue-tests.cpp
lcov --remove coverage/coverage.info '*/cqueue-tests.cpp' -o coverage/coverage.info
genhtml -o coverage coverage/coverage.info

static-analysis: cqueue.hpp
cppcheck --enable=all --inconclusive --suppress=unusedFunction --suppress=passedByValue --suppress=missingIncludeSystem cqueue.hpp
clang-tidy cqueue.hpp -checks='-*,readability-*,-readability-redundant-access-specifiers,performance-*,portability-*,misc-*,clang-analyzer-*,bugprone-*,-clang-diagnostic-error' -extra-arg=-std=c++20

clean:
rm -f cqueue-tests
rm -f cqueue-coverage
Expand Down
46 changes: 24 additions & 22 deletions cqueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace gto {
* @see https://en.wikipedia.org/wiki/Circular_buffer
* @see https://github.com/torrentg/cqueue
* @note This class is not thread-safe.
* @version 1.0.1
* @version 1.0.2
*/
template<std::copyable T, typename Allocator = std::allocator<T>>
class cqueue {
Expand Down Expand Up @@ -49,8 +49,8 @@ class cqueue {
return (n < 0 ? queue->size() : static_cast<size_type>(n));
}
public:
explicit iter(queue_type *o, difference_type p = 0) :
queue{o}, pos{p} {}
explicit iter(queue_type *other, difference_type position = 0) :
queue{other}, pos{position} {}
iter(const iter<std::remove_const_t<value_type>> &other) requires std::is_const_v<value_type> :
queue{other.queue}, pos{other.pos} {}
iter(const iter<value_type> &other) = default;
Expand Down Expand Up @@ -135,7 +135,7 @@ class cqueue {
//! Resize buffer.
constexpr void resizeIfRequired(size_type n);
//! Resize buffer.
void resize(size_type n);
void resize(size_type len);
//! Clear and dealloc memory (preserve capacity and allocator).
void reset() noexcept;

Expand Down Expand Up @@ -165,7 +165,7 @@ class cqueue {
//! Copy assignment.
constexpr cqueue & operator=(const cqueue &other);
//! Move assignment.
constexpr cqueue & operator=(cqueue &&other) { this->swap(other); return *this; }
constexpr cqueue & operator=(cqueue &&other) noexcept { this->swap(other); return *this; }

//! Return container allocator.
constexpr allocator_type get_allocator() const noexcept { return mAllocator; }
Expand Down Expand Up @@ -252,7 +252,7 @@ class cqueue {
//! Clear content.
void clear() noexcept;
//! Swap content.
constexpr void swap (cqueue &x) noexcept;
constexpr void swap (cqueue &other) noexcept;
//! Ensure buffer size.
constexpr void reserve(size_type n);
//! Shrink reserved memory to current size.
Expand All @@ -271,9 +271,8 @@ constexpr gto::cqueue<T, Allocator>::cqueue(size_type capacity, const_alloc_refe
{
if (capacity > MAX_CAPACITY) {
throw std::length_error("cqueue max capacity exceeded");
} else {
mCapacity = (capacity == 0 ? MAX_CAPACITY : capacity);
}
mCapacity = (capacity == 0 ? MAX_CAPACITY : capacity);
}

/**
Expand All @@ -300,8 +299,8 @@ constexpr gto::cqueue<T, Allocator>::cqueue(cqueue &&other, const_alloc_referenc
if (alloc == other.mAllocator) {
swap(other);
} else {
cqueue q{other, alloc};
swap(q);
cqueue aux{other, alloc};
swap(aux);
}
}

Expand Down Expand Up @@ -333,9 +332,8 @@ template<std::copyable T, typename Allocator>
constexpr auto gto::cqueue<T, Allocator>::getCheckedIndex(size_type pos) const noexcept(false) {
if (pos >= mLength) {
throw std::out_of_range("cqueue access out-of-range");
} else {
return getUncheckedIndex(pos);
}
return getUncheckedIndex(pos);
}

/**
Expand Down Expand Up @@ -418,11 +416,13 @@ template<std::copyable T, typename Allocator>
constexpr void gto::cqueue<T, Allocator>::reserve(size_type n) {
if (n <= mReserved) {
return;
} else if (n > mCapacity) {
}

if (n > mCapacity) {
throw std::length_error("cqueue capacity exceeded");
} else {
resize(n);
}

resize(n);
}

/**
Expand All @@ -432,7 +432,9 @@ template<std::copyable T, typename Allocator>
constexpr void gto::cqueue<T, Allocator>::shrink_to_fit() {
if (mReserved == 0) {
return;
} else if (mLength == 0) {
}

if (mLength == 0) {
reset();
} else if (mLength == mReserved || mReserved <= MIN_ALLOCATE) {
return;
Expand Down Expand Up @@ -461,15 +463,15 @@ void gto::cqueue<T, Allocator>::resize(size_type len)
}
else {
// copy elements from mData to tmp
size_type i = 0;
size_type pos = 0;
try {
for (i = 0; i < mLength; ++i) {
size_type index = getUncheckedIndex(i);
allocator_traits::construct(mAllocator, tmp + i, mData[index]);
for (pos = 0; pos < mLength; ++pos) {
size_type index = getUncheckedIndex(pos);
allocator_traits::construct(mAllocator, tmp + pos, mData[index]);
}
} catch (...) {
while (i-- > 0) {
allocator_traits::destroy(mAllocator, tmp + i);
while (pos-- > 0) {
allocator_traits::destroy(mAllocator, tmp + pos);
}
allocator_traits::deallocate(mAllocator, tmp, len);
throw;
Expand Down

0 comments on commit fc075c9

Please sign in to comment.