From 38c04255474f4b4fe3af2bff67bdf9eab4c6fd8b Mon Sep 17 00:00:00 2001 From: Jacob Domagala Date: Sun, 21 Nov 2021 21:18:52 +0100 Subject: [PATCH 01/10] Update test.yml --- .github/workflows/test.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5a1d13b..d26a156 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,6 @@ name: Static analysis -on: [pull_request_target] +on: [push, pull_request_target] jobs: static_analysis: @@ -9,7 +9,15 @@ jobs: steps: - uses: actions/checkout@v2 + - name: Checkout PR tree + uses: actions/checkout@v2 + with: + ref: ${{github.event.pull_request.head.ref}} + repository: ${{github.event.pull_request.head.repo.full_name}} + path: pr_tree + - name: Run static analysis uses: JacobDomagala/StaticAnalysis@make-it-work-on-fork with: verbose: true + root_dir: pr_tree From eab3184875444d6432bfa99d65feac01a808ab5f Mon Sep 17 00:00:00 2001 From: JacobDTest <94798389+JacobDTest@users.noreply.github.com> Date: Mon, 22 Nov 2021 13:52:15 +0100 Subject: [PATCH 02/10] Update source.cpp --- source.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source.cpp b/source.cpp index 229b936..23d8647 100644 --- a/source.cpp +++ b/source.cpp @@ -1,3 +1,4 @@ -int main(int /*argc*/, char** /*argv*/){ +int main(int /*argc*/, char** argv){ + int unused = 0; return 0; } From 3b07425bfc92644d6f737dee18ca60aa52023853 Mon Sep 17 00:00:00 2001 From: JacobDTest <94798389+JacobDTest@users.noreply.github.com> Date: Sun, 28 Nov 2021 22:21:09 +0100 Subject: [PATCH 03/10] Update source.cpp --- source.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source.cpp b/source.cpp index 23d8647..b1ff1a2 100644 --- a/source.cpp +++ b/source.cpp @@ -1,3 +1,7 @@ +void func() { + int anotherUnused; +} + int main(int /*argc*/, char** argv){ int unused = 0; return 0; From db34471ba0d287cc6248cc83844b47ecb43b7b2a Mon Sep 17 00:00:00 2001 From: Jacob Domagala Date: Sun, 5 Dec 2021 18:45:14 +0100 Subject: [PATCH 04/10] Update yml --- .github/workflows/test.yml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 65994e1..266a79c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,13 +2,13 @@ name: Static analysis on: push: - branch: + branches: - develop - master - main pull_request_target: - branch: + branches: - "*" jobs: @@ -24,3 +24,10 @@ jobs: verbose: true exclude_dir: lib cmake_args: -DCMAKE_BUILD_TYPE=Debug + cppcheck_args: '--enable=all --suppress=missingInclude' + clang_tidy_args: '-checks=-*,-fuchsia-*,-google-*,-zircon-*,-abseil-*,-modernize-use-trailing-return-type,-llvm*,-hicpp-uppercase-literal-suffix,-readability-uppercase-literal-suffix' + + - run: | + echo "inputs.cppcheck_args: ${{ inputs.cppcheck_args }}" + echo "env.INPUT_CPPCHECK_ARGS: ${{ env.INPUT_CPPCHECK_ARGS }}" + shell: bash From 092c109822fae78ce6cc44700912a4b394236b99 Mon Sep 17 00:00:00 2001 From: Jacob Domagala Date: Sun, 5 Dec 2021 20:38:46 +0100 Subject: [PATCH 05/10] Use clang_tidy_args input --- .github/workflows/test.yml | 1 + ci/test.sh | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 ci/test.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 65994e1..71c0231 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,3 +24,4 @@ jobs: verbose: true exclude_dir: lib cmake_args: -DCMAKE_BUILD_TYPE=Debug + clang_tidy_args: -checks='*,fuchsia-*,google-*,zircon-*,abseil-*,modernize-use-trailing-return-type,llvm*,hicpp-uppercase-literal-suffix,readability-uppercase-literal-suffix' diff --git a/ci/test.sh b/ci/test.sh deleted file mode 100644 index 6e7fbe2..0000000 --- a/ci/test.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -echo "test" From 2d1dc24585a595da86e005bbb756388b3e01dfef Mon Sep 17 00:00:00 2001 From: Jacob Domagala Date: Sun, 5 Dec 2021 20:38:46 +0100 Subject: [PATCH 06/10] Use clang_tidy_args input --- .github/workflows/test.yml | 2 ++ ci/test.sh | 3 --- 2 files changed, 2 insertions(+), 3 deletions(-) delete mode 100644 ci/test.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 65994e1..ce77b98 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,3 +24,5 @@ jobs: verbose: true exclude_dir: lib cmake_args: -DCMAKE_BUILD_TYPE=Debug + cppcheck_args: --enable=all --suppress=missingInclude + clang_tidy_args: -checks='*,fuchsia-*,google-*,zircon-*,abseil-*,modernize-use-trailing-return-type,llvm*,hicpp-uppercase-literal-suffix,readability-uppercase-literal-suffix' diff --git a/ci/test.sh b/ci/test.sh deleted file mode 100644 index 6e7fbe2..0000000 --- a/ci/test.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -echo "test" From d959198ea9be04be3405ef81775a84d3cecd4374 Mon Sep 17 00:00:00 2001 From: JacobDTest <94798389+JacobDTest@users.noreply.github.com> Date: Sat, 5 Feb 2022 17:35:57 +0100 Subject: [PATCH 07/10] Update test.yml Signed-off-by: JacobDomagala --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b81faa7..5a41aed 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: Static analysis +name: Static analysis_ on: push: From 3884fc4b151e88280545958d1924fe2fc651e189 Mon Sep 17 00:00:00 2001 From: JacobDomagala Date: Wed, 26 Feb 2025 10:02:55 +0100 Subject: [PATCH 08/10] Add python file with static alaysis issues Signed-off-by: JacobDomagala --- file_with_errors.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 file_with_errors.py diff --git a/file_with_errors.py b/file_with_errors.py new file mode 100644 index 0000000..1516851 --- /dev/null +++ b/file_with_errors.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +""" +Static Analysis Checker using Pylint + +Usage: + python static_analysis.py [ ...] +""" + +import sys +from pylint import lint +from pylint.reporters.text import TextReporter +import io + +def run_pylint(files): + """Run pylint on the list of files and capture the output.""" + # Capture the output in a string buffer + output = io.StringIO() + reporter = TextReporter(output) + + # Run pylint on the provided files + # You can adjust the options list below as needed + args = files + results = lint.Run(args, reporter=reporter, exit=False) + + # Get the output as a string + output_str = output.getvalue() + output.close() + return results.linter.msg_status, output_str + +def main(): + if len(sys.argv) < 2: + print("Usage: {} [ ...]".format(sys.argv[0])) + sys.exit(1) + + files = sys.argv[1:] + exit_code, report = run_pylint(files) + + print("Pylint Report:") + print(report) + print("Exit Code:", exit_code) + + # Exit with the pylint status code to reflect analysis result + sys.exit(exit_code) + +if __name__ == "__main__": + main() From 7f96f91262738b92583827a654672cc344e7e2b2 Mon Sep 17 00:00:00 2001 From: JacobDomagala Date: Sun, 2 Mar 2025 23:23:21 +0100 Subject: [PATCH 09/10] Add new file Signed-off-by: JacobDomagala --- CMakeLists.txt | 2 +- new_file.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 new_file.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 06b1411..037016b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,4 +2,4 @@ cmake_minimum_required(VERSION 3.13) project(Test) -add_executable(TestApp source.cpp) +add_executable(TestApp source.cpp new_file.cpp) diff --git a/new_file.cpp b/new_file.cpp new file mode 100644 index 0000000..53d4b57 --- /dev/null +++ b/new_file.cpp @@ -0,0 +1,66 @@ +#include +#include + +// Class to process data, with intentional issues for static analysis checks. +class DataProcessor { +public: + // Constructor allocates memory, but the destructor doesn't free it. + DataProcessor(int size) : size_(size) { + data_ = new int[size_]; // Dynamic allocation (potential memory leak) + } + + // Destructor intentionally missing delete[] to trigger memory leak warning. + ~DataProcessor() { + // Memory cleanup omitted intentionally. + } + + // Process data by filling the array. + void processData() { + // Potential issue: if size_ is 0, this loop does nothing, + // and accessing data_[0] later may be unsafe. + for (int i = 0; i < size_; ++i) { + data_[i] = i * 2; + } + if (size_ > 0) { + std::cout << "First element: " << data_[0] << std::endl; + } + } + + // Returns an element at the given index. + int getElement(int index) { + // Improper bounds checking: if index is invalid, returns -1. + // A static analyzer might flag this for potential misuse. + if (index < 0 || index >= size_) { + return -1; + } + return data_[index]; + } + +private: + int* data_; + int size_; +}; + +int main() { + // Create a processor with a valid size. + DataProcessor processor(10); + processor.processData(); + + // Retrieve an element within bounds. + int val = processor.getElement(5); + std::cout << "Element at index 5: " << val << std::endl; + + // Intentional bug: using an uninitialized variable. + int uninitialized; + std::cout << "Uninitialized value: " << uninitialized << std::endl; + + // Intentional potential division by zero. + int a = 10, b = 0; + if (b == 0) { + std::cerr << "Warning: Division by zero avoided" << std::endl; + } else { + std::cout << "Division result: " << a / b << std::endl; + } + + return 0; +} \ No newline at end of file From cfd2594aa67bacebf49befc8ca2fbfaee9821aed Mon Sep 17 00:00:00 2001 From: JacobDomagala Date: Mon, 3 Mar 2025 22:00:02 +0100 Subject: [PATCH 10/10] Add another Signed-off-by: JacobDomagala --- CMakeLists.txt | 2 +- another.cpp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 another.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 037016b..267adcd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,4 +2,4 @@ cmake_minimum_required(VERSION 3.13) project(Test) -add_executable(TestApp source.cpp new_file.cpp) +add_executable(TestApp source.cpp new_file.cpp another.cpp) diff --git a/another.cpp b/another.cpp new file mode 100644 index 0000000..166408c --- /dev/null +++ b/another.cpp @@ -0,0 +1,36 @@ +#include + +int evaluate(int x) { + // Create a large number of branches. + if (x == 0) return 0; +#define BRANCH(n) else if (x == n) return n; + BRANCH(1) + BRANCH(2) + BRANCH(3) + BRANCH(4) + BRANCH(5) + BRANCH(6) + BRANCH(7) + BRANCH(8) + BRANCH(9) + BRANCH(10) + BRANCH(11) + BRANCH(12) + BRANCH(13) + BRANCH(14) + BRANCH(15) + BRANCH(16) + BRANCH(17) + BRANCH(18) + BRANCH(19) + BRANCH(20) + // Duplicate or extend this macro to add many more branches... +#undef BRANCH + return -1; +} + +int main() { + int result = evaluate(15); + printf("Result: %d\n", result); + return 0; +} \ No newline at end of file