Skip to content

Commit 17efc67

Browse files
committed
Fix static analysis issues
1 parent 03ce0ed commit 17efc67

22 files changed

+56
-62
lines changed

.clang-tidy

+4-22
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
11
---
2-
# Checks: '*,clang-diagnostic-*,clang-analyzer-*'
3-
Checks: '*,-llvmlibc-*,-readability-braces-around-statements,-hicpp-braces-around-statements,-google-readability-braces-around-statements,-hicpp-no-array-decay,-fuchsia-default-arguments-calls,-modernize-use-trailing-return-type,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-bugprone-lambda-function-name,-hicpp-signed-bitwise,-cert-err60-cpp,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-init-variables,-cppcoreguidelines-no-malloc,-hicpp-no-malloc,-google-readability-todo,-modernize-pass-by-value,-cppcoreguidelines-pro-type-member-init,-hicpp-member-init,-hicpp-avoid-c-arrays,-cppcoreguidelines-avoid-c-arrays,-modernize-avoid-c-arrays,-abseil-string-find-startswith,-google-build-using-namespace,-cppcoreguidelines-owning-memory,-readability-else-after-return,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-readability-isolate-declaration,-cert-env33-c,-abseil-*'
4-
# Checks: '*,clang-diagnostic-*,clang-analyzer-*'
5-
# Checks: '*,-llvmlibc-restrict-system-libc-headers,-readability-braces-around-statements,-hicpp-braces-around-statements,-google-readability-braces-around-statements,-hicpp-no-array-decay,-fuchsia-default-arguments-calls,-modernize-use-trailing-return-type,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-bugprone-lambda-function-name,-hicpp-signed-bitwise,-cert-err60-cpp,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-init-variables,-cppcoreguidelines-no-malloc,-hicpp-no-malloc,-google-readability-todo,-modernize-pass-by-value,-cppcoreguidelines-pro-type-member-init,-hicpp-member-init,-hicpp-avoid-c-arrays,-cppcoreguidelines-avoid-c-arrays,-modernize-avoid-c-arrays,-abseil-string-find-startswith,-google-build-using-namespace,-cppcoreguidelines-owning-memory,-readability-else-after-return,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-readability-isolate-declaration'
2+
Checks: 'clang-diagnostic-*,clang-analyzer-*,modernize*,performance*,readability-*size*,readability*redundant*,-readability-redundant-member-init,-readability-redundant-access-specifiers,misc*,boost-use-to-string,cert*,google-runtime-member-string-references,google-explicit-constructor,-cert-err58-cpp,-modernize-concat-nested-namespaces,-modernize-use-nodiscard,-misc-non-private-member-variables-in-classes,-modernize-use-trailing-return-type,-misc-no-recursion,-misc-const-correctness,-misc-use-anonymous-namespace'
63
WarningsAsErrors: ''
74
HeaderFilterRegex: ''
85
AnalyzeTemporaryDtors: false
9-
FormatStyle: none
6+
FormatStyle: file
107
User: johannes
118
CheckOptions:
12-
- key: cert-dcl16-c.NewSuffixes
13-
value: 'L;LL;LU;LLU'
14-
- key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField
15-
value: '0'
16-
- key: cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors
17-
value: '1'
18-
- key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
19-
value: '1'
20-
- key: google-readability-braces-around-statements.ShortStatementLines
21-
value: '1'
22-
- key: google-readability-function-size.StatementThreshold
23-
value: '800'
24-
- key: google-readability-namespace-comments.ShortNamespaceLines
25-
value: '10'
26-
- key: google-readability-namespace-comments.SpacesBeforeComments
27-
value: '2'
289
- key: modernize-loop-convert.MaxCopySize
2910
value: '16'
3011
- key: modernize-loop-convert.MinConfidence
@@ -37,5 +18,6 @@ CheckOptions:
3718
value: llvm
3819
- key: modernize-use-nullptr.NullMacros
3920
value: 'NULL'
21+
- key: hicpp-signed-bitwise.IgnorePositiveIntegerLiterals
22+
value: '1'
4023
...
41-

.clangd

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
InlayHints:
2+
Enabled: Yes
3+
ParameterNames: No
4+
DeducedTypes: No
5+
Diagnostics:
6+
UnusedIncludes: Strict
7+
MissingIncludes: None

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
tool:
1717
- cppcheck
1818

19-
runs-on: ubuntu-22.04
19+
runs-on: ubuntu-24.04
2020

2121
steps:
2222
- uses: actions/checkout@v4
@@ -43,7 +43,7 @@ jobs:
4343
- name: configure
4444
run: |
4545
cmake -S . -B build \
46-
-DWERROR=ON -DBUILD_TESTS=ON \
46+
-DWERROR=ON -DBUILD_TESTS=OFF \
4747
-DBOOST_ROOT=boost_${BOOST_VERSION} \
4848
-DCMAKE_BUILD_TYPE=Release \
4949
-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE \

client/decoder/ogg_decoder.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#endif
3434
#include <cstdint>
3535
#include <cstring>
36+
#include <string>
3637

3738

3839

@@ -43,7 +44,7 @@ static constexpr auto LOG_TAG = "OggDecoder";
4344
namespace decoder
4445
{
4546

46-
OggDecoder::OggDecoder() : Decoder()
47+
OggDecoder::OggDecoder()
4748
{
4849
ogg_sync_init(&oy); /* Now we can read pages */
4950
}
@@ -244,7 +245,6 @@ SampleFormat OggDecoder::setHeader(msg::CodecHeader* chunk)
244245
if (comment.find("SAMPLE_FORMAT=") == 0)
245246
sampleFormat_.setFormat(comment.substr(comment.find('=') + 1));
246247
LOG(INFO, LOG_TAG) << "comment: " << comment << "\n";
247-
;
248248
++ptr;
249249
}
250250

client/player/alsa_player.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ class AlsaPlayer : public Player
8686
snd_pcm_uframes_t frames_;
8787
boost::asio::posix::stream_descriptor sd_;
8888
std::chrono::time_point<std::chrono::steady_clock> last_change_;
89-
std::recursive_mutex mutex_;
9089
boost::asio::steady_timer timer_;
9190

9291
std::optional<std::chrono::microseconds> buffer_time_;

client/player/player.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class Player
117117
template <typename T>
118118
void adjustVolume(char* buffer, size_t count, double volume)
119119
{
120-
T* bufferT = (T*)buffer;
120+
auto* bufferT = static_cast<T*>(buffer);
121121
for (size_t n = 0; n < count; ++n)
122122
bufferT[n] = endian::swap<T>(static_cast<T>(endian::swap<T>(bufferT[n]) * volume));
123123
}

client/player/pulse_player.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ vector<PcmDevice> PulsePlayer::pcm_list(const std::string& parameter)
145145

146146

147147
PulsePlayer::PulsePlayer(boost::asio::io_context& io_context, const ClientSettings::Player& settings, std::shared_ptr<Stream> stream)
148-
: Player(io_context, settings, stream), latency_(BUFFER_TIME), pa_ml_(nullptr), pa_ctx_(nullptr), playstream_(nullptr), proplist_(nullptr),
149-
server_(std::nullopt)
148+
: Player(io_context, settings, stream), latency_(BUFFER_TIME), last_chunk_tick_(0), pa_ml_(nullptr), pa_ctx_(nullptr), playstream_(nullptr),
149+
proplist_(nullptr), server_(std::nullopt)
150150
{
151151
auto params = utils::string::split_pairs_to_container<std::vector<std::string>>(settings.parameter, ',', '=');
152152
if (params.find("buffer_time") != params.end())

cmake/cppcheck.cmake

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ if(CMAKE_VERSION VERSION_EQUAL "3.19.0" OR CMAKE_VERSION VERSION_GREATER
4545
"--suppress=*:${CMAKE_BINARY_DIR}/_deps/*"
4646
"--suppress=unusedFunction"
4747
"--suppress=noExplicitConstructor"
48-
"--suppress=preprocessorErrorDirective")
48+
"--suppress=missingInclude"
49+
"--suppress=missingIncludeSystem"
50+
"--suppress=useInitializationList"
51+
"--suppress=shadowFunction"
52+
"--suppress=shadowVariable"
53+
"--suppress=stlIfStrFind"
54+
"--suppress=useStlAlgorithm"
55+
"--suppress=virtualCallInConstructor")
4956

5057
if(CPPCHECK_ARGS)
5158
set(CPPCHECK_ARGS_LIST ${CPPCHECK_ARGS})

common/daemon.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void Daemon::daemonize()
8585

8686
if (!group_.empty())
8787
{
88-
struct group* grp = getgrnam(group_.c_str());
88+
const struct group* grp = getgrnam(group_.c_str());
8989
if (grp == nullptr)
9090
throw SnapException("no such group \"" + group_ + "\"");
9191
user_gid = grp->gr_gid;

common/error_code.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ struct ErrorOr
126126
/// @return the moved error
127127
ErrorCode takeError()
128128
{
129-
return std::move(std::get<ErrorCode>(var));
129+
auto ec = std::move(std::get<ErrorCode>(var));
130+
return ec;
130131
}
131132

132133
private:

common/message/codec_header.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ namespace msg
3131
class CodecHeader : public BaseMessage
3232
{
3333
public:
34-
CodecHeader(const std::string& codecName = "", uint32_t size = 0)
34+
explicit CodecHeader(const std::string& codecName = "", uint32_t size = 0)
3535
: BaseMessage(message_type::kCodecHeader), payloadSize(size), payload(nullptr), codec(codecName)
3636
{
3737
if (size > 0)
38-
payload = (char*)malloc(size * sizeof(char));
38+
payload = static_cast<char*>(malloc(size * sizeof(char)));
3939
}
4040

4141
~CodecHeader() override

common/message/message.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct tv
116116
}
117117

118118
/// C'tor, construct from timeval @p tv
119-
tv(timeval tv) : sec(tv.tv_sec), usec(tv.tv_usec){};
119+
explicit tv(timeval tv) : sec(tv.tv_sec), usec(tv.tv_usec){};
120120
/// C'tor, construct from @p _sec and @p _usec
121121
tv(int32_t _sec, int32_t _usec) : sec(_sec), usec(_usec){};
122122

@@ -173,7 +173,7 @@ struct BaseMessage
173173
}
174174

175175
/// c'tor with message type @p type_
176-
BaseMessage(message_type type_) : type(type_), id(0), refersTo(0), size(0)
176+
explicit BaseMessage(message_type type_) : type(type_), id(0), refersTo(0), size(0)
177177
{
178178
}
179179

@@ -370,7 +370,7 @@ struct BaseMessage
370370
void readVal(std::istream& stream, char** payload, uint32_t& size) const
371371
{
372372
readVal(stream, size);
373-
*payload = (char*)realloc(*payload, size);
373+
*payload = static_cast<char*>(realloc(*payload, size));
374374
stream.read(*payload, size);
375375
}
376376

common/message/pcm_chunk.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***
22
This file is part of snapcast
3-
Copyright (C) 2014-2022 Johannes Pohl
3+
Copyright (C) 2014-2024 Johannes Pohl
44
55
This program is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -83,7 +83,7 @@ class PcmChunk : public WireChunk
8383

8484
// logd << ", from: " << format.frameSize()*idx << ", to: " << format.frameSize()*idx + format.frameSize()*result;
8585
if (outputBuffer != nullptr)
86-
memcpy((char*)outputBuffer, (char*)(payload) + format.frameSize() * idx_, format.frameSize() * result);
86+
memcpy(static_cast<char*>(outputBuffer), static_cast<char*>(payload) + format.frameSize() * idx_, format.frameSize() * result);
8787

8888
idx_ += result;
8989
// logd << ", new idx: " << idx << ", result: " << result << ", wireChunk->length: " << wireChunk->length << ", format.frameSize(): " <<

common/message/wire_chunk.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ namespace msg
3939
class WireChunk : public BaseMessage
4040
{
4141
public:
42-
WireChunk(uint32_t size = 0) : BaseMessage(message_type::kWireChunk), payloadSize(size), payload(nullptr)
42+
explicit WireChunk(uint32_t size = 0) : BaseMessage(message_type::kWireChunk), payloadSize(size), payload(nullptr)
4343
{
4444
if (size > 0)
45-
payload = (char*)malloc(size * sizeof(char));
45+
payload = static_cast<char*>(malloc(size * sizeof(char)));
4646
}
4747

4848
WireChunk(const WireChunk& wireChunk) : BaseMessage(message_type::kWireChunk), timestamp(wireChunk.timestamp), payloadSize(wireChunk.payloadSize)
4949
{
50-
payload = (char*)malloc(payloadSize);
50+
payload = static_cast<char*>(malloc(payloadSize));
5151
memcpy(payload, wireChunk.payload, payloadSize);
5252
}
5353

common/utils/file_utils.hpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#endif
3434

3535
#include <filesystem>
36-
#include <fstream>
3736
#include <stdexcept>
3837
#include <vector>
3938

@@ -64,15 +63,15 @@ static void do_chown(const std::string& file_path, const std::string& user_name,
6463

6564
if (!user_name.empty())
6665
{
67-
struct passwd* pwd = getpwnam(user_name.c_str());
66+
const struct passwd* pwd = getpwnam(user_name.c_str());
6867
if (pwd == nullptr)
6968
throw std::runtime_error("Failed to get uid");
7069
uid = pwd->pw_uid;
7170
}
7271

7372
if (!group_name.empty())
7473
{
75-
struct group* grp = getgrnam(group_name.c_str());
74+
const struct group* grp = getgrnam(group_name.c_str());
7675
if (grp == nullptr)
7776
throw std::runtime_error("Failed to get gid");
7877
gid = grp->gr_gid;

common/utils/string_utils.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ std::map<std::string, T> split_pairs_to_container(const std::string& s, char pai
7575
{
7676
std::map<std::string, T> result;
7777
auto keyValueList = split(s, pair_delim);
78-
for (auto& kv : keyValueList)
78+
for (const auto& kv : keyValueList)
7979
{
8080
auto pos = kv.find(key_value_delim);
8181
if (pos != std::string::npos)

server/control_session.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
// standard headers
2828
#include <functional>
2929
#include <memory>
30-
#include <optional>
3130
#include <string>
3231

3332

server/control_session_ws.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***
22
This file is part of snapcast
3-
Copyright (C) 2014-2023 Johannes Pohl
3+
Copyright (C) 2014-2024 Johannes Pohl
44
55
This program is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -74,7 +74,7 @@ void ControlSessionWebsocket::stop()
7474
void ControlSessionWebsocket::sendAsync(const std::string& message)
7575
{
7676
boost::asio::post(strand_,
77-
[this, self = shared_from_this(), msg = message]()
77+
[this, self = shared_from_this(), msg = message]() mutable
7878
{
7979
messages_.push_back(std::move(msg));
8080
if (messages_.size() > 1)
@@ -147,7 +147,7 @@ void ControlSessionWebsocket::on_read_ws(beast::error_code ec, std::size_t bytes
147147
if (!line.empty())
148148
{
149149
// LOG(DEBUG, LOG_TAG) << "received: " << line << "\n";
150-
if ((message_receiver_ != nullptr) && !line.empty())
150+
if (message_receiver_ != nullptr)
151151
{
152152
message_receiver_->onMessageReceived(shared_from_this(), line,
153153
[this](const std::string& response)

server/encoder/flac_encoder.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
#include "FLAC/stream_encoder.h"
2626

2727
// standard headers
28-
#include <stdio.h>
29-
#include <stdlib.h>
30-
#include <string.h>
28+
#include <cstdio>
29+
#include <cstdlib>
30+
#include <cstring>
3131

3232
namespace encoder
3333
{
3434

3535
class FlacEncoder : public Encoder
3636
{
3737
public:
38-
FlacEncoder(const std::string& codecOptions = "");
38+
explicit FlacEncoder(const std::string& codecOptions = "");
3939
~FlacEncoder() override;
4040
void encode(const msg::PcmChunk& chunk) override;
4141
std::string getAvailableOptions() const override;

server/encoder/ogg_encoder.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,19 @@ void OggEncoder::encode(const msg::PcmChunk& chunk)
8484
{
8585
if (sampleFormat_.sampleSize() == 1)
8686
{
87-
auto* chunkBuffer = reinterpret_cast<int8_t*>(chunk.payload);
87+
const auto* chunkBuffer = reinterpret_cast<int8_t*>(chunk.payload);
8888
for (int i = 0; i < frames; i++)
8989
buffer[channel][i] = chunkBuffer[sampleFormat_.channels() * i + channel] / 128.f;
9090
}
9191
else if (sampleFormat_.sampleSize() == 2)
9292
{
93-
auto* chunkBuffer = reinterpret_cast<int16_t*>(chunk.payload);
93+
const auto* chunkBuffer = reinterpret_cast<int16_t*>(chunk.payload);
9494
for (int i = 0; i < frames; i++)
9595
buffer[channel][i] = chunkBuffer[sampleFormat_.channels() * i + channel] / 32768.f;
9696
}
9797
else if (sampleFormat_.sampleSize() == 4)
9898
{
99-
auto* chunkBuffer = reinterpret_cast<int32_t*>(chunk.payload);
99+
const auto* chunkBuffer = reinterpret_cast<int32_t*>(chunk.payload);
100100
for (int i = 0; i < frames; i++)
101101
buffer[channel][i] = chunkBuffer[sampleFormat_.channels() * i + channel] / 2147483648.f;
102102
}

server/encoder/opus_encoder.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace
4444
template <typename T>
4545
void assign(void* pointer, T val)
4646
{
47-
T* p = (T*)pointer;
47+
T* p = static_cast<T*>(pointer);
4848
*p = val;
4949
}
5050
} // namespace
@@ -246,7 +246,7 @@ void OpusEncoder::encode(const SampleFormat& format, const char* data, size_t si
246246
if (encoded_.size() < size)
247247
encoded_.resize(size);
248248

249-
opus_int32 len = opus_encode(enc_, (opus_int16*)data, samples_per_channel, encoded_.data(), size);
249+
opus_int32 len = opus_encode(enc_, reinterpret_cast<const opus_int16*>(data), samples_per_channel, encoded_.data(), size);
250250
LOG(TRACE, LOG_TAG) << "Encode " << samples_per_channel << " frames, size " << size << " bytes, encoded: " << len << " bytes" << '\n';
251251

252252
if (len > 0)

server/encoder/pcm_encoder.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***
22
This file is part of snapcast
3-
Copyright (C) 2014-2021 Johannes Pohl
3+
Copyright (C) 2014-2024 Johannes Pohl
44
55
This program is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -42,7 +42,7 @@ namespace
4242
template <typename T>
4343
void assign(void* pointer, T val)
4444
{
45-
T* p = (T*)pointer;
45+
T* p = static_cast<T*>(pointer);
4646
*p = val;
4747
}
4848
} // namespace

0 commit comments

Comments
 (0)