Skip to content

Commit

Permalink
Several fixes per the PVS-Studio report.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpimenov authored and tatiana-yan committed Apr 30, 2020
1 parent cea59d6 commit 7518156
Show file tree
Hide file tree
Showing 48 changed files with 220 additions and 187 deletions.
8 changes: 7 additions & 1 deletion base/base_tests/cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ class SimpleMovableFunctor
public:
explicit SimpleMovableFunctor(std::vector<char> * v) : m_v(v) {}

// movable
SimpleMovableFunctor(SimpleMovableFunctor && other)
{
m_v = other.m_v;
other.m_v = nullptr;
}

SimpleMovableFunctor & operator=(SimpleMovableFunctor && other)
{
m_v = other.m_v;
other.m_v = nullptr;
return *this;
}

void operator() (char c)
{
m_v->push_back(c);
Expand Down
3 changes: 2 additions & 1 deletion base/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ inline void ForceUseValue(T const & t)
#define FORCE_USE_VALUE(x) ::base::impl::ForceUseValue(x)

#ifdef __GNUC__
#define PREDICT(x, prediction) __builtin_expect(x, prediction)
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
#define PREDICT(x, prediction) __builtin_expect(static_cast<long>(x), static_cast<long>(prediction))
#define PREDICT_TRUE(x) __builtin_expect((x) != 0, 1)
#define PREDICT_FALSE(x) __builtin_expect((x) != 0, 0)
#else
Expand Down
61 changes: 32 additions & 29 deletions base/rolling_hash.hpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,65 @@
#pragma once

#include "base/assert.hpp"
#include "base/base.hpp"
#include "base/bits.hpp"
#include "base/math.hpp"

#ifdef DEBUG
#include <queue>
#endif


template <typename T, typename HashT> class RabinKarpRollingHasher
{
public:
typedef T value_type;
typedef HashT hash_type;
using value_type = T;
using hash_type = HashT;

explicit RabinKarpRollingHasher(HashT multiplier)
: m_Hash(0), m_Multiplier(multiplier), m_RemoveMultiplier(0)
: m_multiplier(multiplier)
{
}

template <typename IterT> hash_type Init(IterT it, uint64_t windowSize)
template <typename Iter> hash_type Init(Iter it, uint64_t windowSize)
{
m_WindowSize = windowSize;
m_RemoveMultiplier = base::PowUint(m_Multiplier, m_WindowSize - 1);
ASSERT_GREATER(m_windowSize, 0, ());
m_windowSize = windowSize;
m_removeMultiplier = base::PowUint(m_multiplier, m_windowSize - 1);
#ifdef DEBUG
while (!m_Queue.empty()) m_Queue.pop();
while (!m_queue.empty()) m_queue.pop();
#endif
m_Hash = 0;
for (uint64_t i = 0; i < m_WindowSize; ++it, ++i)
m_hash = 0;
for (uint64_t i = 0; i < m_windowSize; ++it, ++i)
{
m_Hash = m_Hash * m_Multiplier + *it;
m_hash = m_hash * m_multiplier + *it;
#ifdef DEBUG
m_Queue.push(*it);
m_queue.push(*it);
#endif
}
return m_Hash;
return m_hash;
}

hash_type Scroll(T const remove, T const add)
{
ASSERT_NOT_EQUAL(m_RemoveMultiplier, 0, (m_Multiplier, m_WindowSize, remove, add));
ASSERT_NOT_EQUAL(m_WindowSize, 0, (m_Multiplier, remove, add));
ASSERT_NOT_EQUAL(m_removeMultiplier, 0, (m_multiplier, m_windowSize, remove, add));
ASSERT_NOT_EQUAL(m_windowSize, 0, (m_multiplier, remove, add));
#ifdef DEBUG
ASSERT_EQUAL(m_Queue.front(), remove, ());
m_Queue.pop();
m_Queue.push(add);
ASSERT_EQUAL(m_queue.front(), remove, ());
m_queue.pop();
m_queue.push(add);
#endif
m_Hash -= m_RemoveMultiplier * remove;
m_Hash = m_Hash * m_Multiplier + add;
return m_Hash;
m_hash -= m_removeMultiplier * remove;
m_hash = m_hash * m_multiplier + add;
return m_hash;
}

private:
hash_type m_Hash;
hash_type m_Multiplier;
hash_type m_RemoveMultiplier;
uint64_t m_WindowSize;
hash_type m_hash = 0;
hash_type m_multiplier = 1;
hash_type m_removeMultiplier = 1;
uint64_t m_windowSize = 1;

#ifdef DEBUG
std::queue<value_type> m_Queue;
std::queue<value_type> m_queue;
#endif
};

Expand All @@ -74,5 +77,5 @@ class RabinKarpRollingHasher64 : public RabinKarpRollingHasher<uint64_t, uint64_
: RabinKarpRollingHasher<uint64_t, uint64_t>(6364136223846793005ULL) {}
};

typedef RabinKarpRollingHasher32 RollingHasher32;
typedef RabinKarpRollingHasher64 RollingHasher64;
using RollingHasher32 = RabinKarpRollingHasher32;
using RollingHasher64 = RabinKarpRollingHasher64;
2 changes: 1 addition & 1 deletion coding/dd_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class DDVector
private:
ReaderType const * m_pReader;
size_type m_I;
mutable T m_Value;
mutable T m_Value = {};
mutable bool m_bValueRead;
#ifdef DEBUG
size_type m_Size;
Expand Down
2 changes: 2 additions & 0 deletions coding/files_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ class MappedFile

Handle(Handle && h) { Assign(std::move(h)); }

Handle & operator=(Handle && h) { Assign(std::move(h)); return *this; }

~Handle();

void Assign(Handle && h);
Expand Down
8 changes: 4 additions & 4 deletions coding/reader_streambuf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class BaseStreamBuf : public std::streambuf

class ReaderStreamBuf : public BaseStreamBuf
{
std::unique_ptr<Reader> m_p;
uint64_t m_pos, m_size;

public:
ReaderStreamBuf(std::unique_ptr<Reader> && p);
virtual ~ReaderStreamBuf();
Expand All @@ -29,7 +26,10 @@ class ReaderStreamBuf : public BaseStreamBuf
virtual std::streamsize xsgetn(char_type * s, std::streamsize n);
virtual int_type underflow();

char m_buf[1];
std::unique_ptr<Reader> m_p;
uint64_t m_pos = 0;
uint64_t m_size = 0;
char m_buf[1] = {};
};

class WriterStreamBuf : public BaseStreamBuf
Expand Down
4 changes: 2 additions & 2 deletions coding/zlib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ class ZLib

protected:
z_stream m_stream;
bool m_init;
unsigned char m_buffer[kBufferSize];
bool m_init = false;
unsigned char m_buffer[kBufferSize] = {};

DISALLOW_COPY_AND_MOVE(Processor);
};
Expand Down
1 change: 1 addition & 0 deletions drape_frontend/apply_feature_functors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ ApplyPointFeature::ApplyPointFeature(TileKey const & tileKey, TInsertShapeFn con
, m_hasArea(false)
, m_createdByEditor(false)
, m_obsoleteInEditor(false)
, m_isUGC(false)
, m_depthLayer(depthLayer)
, m_symbolDepth(dp::kMinDepth)
, m_symbolRule(nullptr)
Expand Down
4 changes: 2 additions & 2 deletions editor/editor_notes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ struct Note
std::string m_note;
};

inline bool operator==(Note const & a, Note const & b)
inline bool operator==(Note const & lhs, Note const & rhs)
{
return a.m_point == b.m_point && b.m_note == b.m_note;
return lhs.m_point == rhs.m_point && lhs.m_note == rhs.m_note;
}

class Notes : public std::enable_shared_from_this<Notes>
Expand Down
2 changes: 1 addition & 1 deletion editor/osm_editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class Editor final : public MwmSet::Observer

struct FeatureTypeInfo
{
FeatureStatus m_status;
FeatureStatus m_status = FeatureStatus::Untouched;
EditableMapObject m_object;
/// If not empty contains Feature's addr:street, edited by user.
std::string m_street;
Expand Down
7 changes: 7 additions & 0 deletions editor/xml_feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ XMLFeature::XMLFeature(XMLFeature const & feature)
// Don't validate feature: it should already be validated.
}

XMLFeature & XMLFeature::operator=(XMLFeature const & feature)
{
m_document.reset(feature.m_document);
// Don't validate feature: it should already be validated.
return *this;
}

bool XMLFeature::operator==(XMLFeature const & other) const
{
return ToOSMString() == other.ToOSMString();
Expand Down
3 changes: 3 additions & 0 deletions editor/xml_feature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class XMLFeature
XMLFeature(pugi::xml_document const & xml);
XMLFeature(pugi::xml_node const & xml);
XMLFeature(XMLFeature const & feature);

XMLFeature & operator=(XMLFeature const & feature);

// TODO: It should make "deep" compare instead of converting to strings.
// Strings comparison does not work if tags order is different but tags are equal.
bool operator==(XMLFeature const & other) const;
Expand Down
4 changes: 2 additions & 2 deletions generator/generate_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ struct GenerateInfo
// Current generated file name if --output option is defined.
std::string m_fileName;

NodeStorageType m_nodeStorageType;
OsmSourceType m_osmFileType;
NodeStorageType m_nodeStorageType = NodeStorageType::Memory;
OsmSourceType m_osmFileType = OsmSourceType::XML;
std::string m_osmFileName;

std::string m_bookingDataFilename;
Expand Down
2 changes: 1 addition & 1 deletion generator/osm_o5m_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class O5MSource
std::vector<char> m_stringBuffer;
size_t m_stringCurrentIndex;
StreamBuffer m_buffer;
size_t m_remainder;
size_t m_remainder = 0;
int64_t m_currentNodeRef = 0;
int64_t m_currentWayRef = 0;
int64_t m_currentRelationRef = 0;
Expand Down
4 changes: 2 additions & 2 deletions generator/relation_tags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class RelationTagsBase
void AddCustomTag(std::pair<std::string, std::string> const & p);
virtual void Process(RelationElement const & e) = 0;

uint64_t m_featureID;
OsmElement * m_current;
uint64_t m_featureID = 0;
OsmElement * m_current = nullptr;

private:
base::Cache<uint64_t, RelationElement> m_cache;
Expand Down
2 changes: 1 addition & 1 deletion geometry/nearby_points_sweeper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class NearbyPointsSweeper
{
if (m_x != rhs.m_x)
return m_x < rhs.m_x;
if (m_index < rhs.m_index)
if (m_index != rhs.m_index)
return m_index < rhs.m_index;
return m_priority < rhs.m_priority;
}
Expand Down
6 changes: 3 additions & 3 deletions geometry/screenbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ double constexpr kEndPerspectiveScale1 = 0.3e-5;
double constexpr kEndPerspectiveScale2 = 0.13e-5;

ScreenBase::ScreenBase()
: m_GlobalRect(m_Org, ang::AngleD(0), m2::RectD(-320, -240, 320, 240))
: m_Org(320, 240)
, m_GlobalRect(m_Org, ang::AngleD(0), m2::RectD(-320, -240, 320, 240))
, m_ClipRect(m2::RectD(0, 0, 640, 480))
, m_ViewportRect(0, 0, 640, 480)
, m_PixelRect(m_ViewportRect)
, m_Scale(0.1)
, m_Angle(0.0)
, m_Org(320, 240)
, m_3dFOV(kPerspectiveAngleFOV)
, m_3dNearZ(0.001)
, m_3dFarZ(0.0)
Expand All @@ -45,7 +45,7 @@ ScreenBase::ScreenBase(m2::RectI const & pxRect, m2::AnyRectD const & glbRect)
}

ScreenBase::ScreenBase(ScreenBase const & s, m2::PointD const & org, double scale, double angle)
: m_ViewportRect(s.m_ViewportRect), m_Scale(scale), m_Angle(angle), m_Org(org)
: m_Org(org), m_ViewportRect(s.m_ViewportRect), m_Scale(scale), m_Angle(angle)
{
UpdateDependentParameters();
}
Expand Down
5 changes: 4 additions & 1 deletion geometry/screenbase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ class ScreenBase
return (m_GtoP == src.m_GtoP) && (m_PtoG == src.m_PtoG);
}

private:
// Used when initializing m_GlobalRect.
m2::PointD m_Org;

protected:
// Update dependent parameters from base parameters.
// Must be called when base parameters changed.
Expand Down Expand Up @@ -169,7 +173,6 @@ class ScreenBase

double m_Scale;
ang::AngleD m_Angle;
m2::PointD m_Org;

double m_3dFOV;
double m_3dNearZ;
Expand Down
6 changes: 3 additions & 3 deletions geometry/simplification.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ void SimplifyDP(Iter first, Iter last, double epsilon, DistanceFn & distFn, Out

struct SimplifyOptimalRes
{
SimplifyOptimalRes() : m_PointCount(-1U) {}
SimplifyOptimalRes() = default;
SimplifyOptimalRes(int32_t nextPoint, uint32_t pointCount)
: m_NextPoint(nextPoint), m_PointCount(pointCount) {}

int32_t m_NextPoint;
uint32_t m_PointCount;
int32_t m_NextPoint = -1;
uint32_t m_PointCount = -1U;
};
} // namespace impl

Expand Down
2 changes: 1 addition & 1 deletion indexer/classificator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ namespace
vec_t const & m_rules;
drule::KeysT & m_keys;

bool m_added;
bool m_added = false;

void add_rule(int ft, iter_t i)
{
Expand Down
4 changes: 2 additions & 2 deletions indexer/classificator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class ClassifObject
}

using VisibleMask = std::bitset<scales::UPPER_STYLE_SCALE+1>;
void SetVisibilityOnScale(bool isVisible, int scale) { m_visibility[scale] = isVisible; }
void SetVisibilityOnScale(bool isVisible, int scale) { m_visibility.set(scale, isVisible); }

/// @name Policies for classificator tree serialization.
//@{
Expand Down Expand Up @@ -236,7 +236,7 @@ class Classificator

ClassifObject m_root;
IndexAndTypeMapping m_mapping;
uint32_t m_coastType;
uint32_t m_coastType = 0;

DISALLOW_COPY_AND_MOVE(Classificator);
};
Expand Down
2 changes: 1 addition & 1 deletion indexer/feature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class FeatureType
void ParseGeometryAndTriangles(int scale);

uint8_t m_header = 0;
std::array<uint32_t, feature::kMaxTypesCount> m_types;
std::array<uint32_t, feature::kMaxTypesCount> m_types = {};

FeatureID m_id;
FeatureParamsBase m_params;
Expand Down
4 changes: 2 additions & 2 deletions indexer/feature_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace feature
std::vector<std::string> ToObjectNames() const;

private:
Types m_types;
Types m_types = {};
size_t m_size = 0;

GeomType m_geomType = GeomType::Undefined;
Expand Down Expand Up @@ -279,7 +279,7 @@ class FeatureParams : public FeatureParamsBase
Base::Read(src, header);
}

Types m_types;
Types m_types = {};

private:
using Base = FeatureParamsBase;
Expand Down
Loading

0 comments on commit 7518156

Please sign in to comment.