Skip to content

Commit

Permalink
Add cppcheck and fix errors (#114)
Browse files Browse the repository at this point in the history
Add workflow with cppcheck analysis and fix current issues indicated by the tool.
  • Loading branch information
asias91 authored Dec 10, 2024
1 parent 4b1c19d commit acc8c69
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/actions/run-cppcheck/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2024 Dynatrace LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: "Run static code analysis with cppcheck"
description: "Runs cppcheck on whole source code, excluding third-party"

runs:
using: "composite"
steps:
- name: "Install cppcheck"
shell: bash
run: sudo apt-get install -y cppcheck
- name: "Run cppcheck"
shell: bash
run: cppcheck -i $GITHUB_WORKSPACE/third_party $GITHUB_WORKSPACE --inline-suppr --error-exitcode=1
1 change: 1 addition & 0 deletions .github/workflows/build-and-test-debug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ jobs:

- name: "Run component tests"
uses: ./.github/actions/run-component-tests

31 changes: 31 additions & 0 deletions .github/workflows/cppcheck.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2023 Dynatrace LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: "Checkout and run static analysis"

on:
[push, pull_request]

jobs:
run-static-analysis:
runs-on: ${{ matrix.runner }}
strategy:
matrix:
runner: [ubuntu-latest]
steps:
- name: "Checkout source code of the repository"
uses: actions/checkout@v4
- name: "Run static code analysis with cppcheck"
uses: ./.github/actions/run-cppcheck

3 changes: 3 additions & 0 deletions liblogging/src/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ std::string vaFormat(const char* format, va_list args) noexcept {
int len{std::vsnprintf(result.data(), result.size() + 1, format, args)};

if (len <= 0) {
va_end(argsCopy);
return result;
}

if (len <= (int)result.size()) {
result.resize(len);
va_end(argsCopy);
return result;
}

result.resize(len);
len = std::vsnprintf(result.data(), result.size() + 1, format, argsCopy);

if (len <= 0) {
va_end(argsCopy);
return {};
}

Expand Down
1 change: 1 addition & 0 deletions libservice/test/AggregatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void PrintTo(const Service& service, std::ostream* os) {

class IpAddressCheckerMock : public IpAddressChecker {
public:
// cppcheck-suppress syntaxError ; sporadic false error occurred on following line
MOCK_METHOD(bool, isV4AddressExternal, (const in_addr&), (const));
MOCK_METHOD(bool, isV6AddressExternal, (const in6_addr&), (const));
};
Expand Down

0 comments on commit acc8c69

Please sign in to comment.