diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 65994e1..5a41aed 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,14 +1,14 @@ -name: Static analysis +name: Static analysis_ on: push: - branch: + branches: - develop - master - main pull_request_target: - branch: + branches: - "*" jobs: diff --git a/CMakeLists.txt b/CMakeLists.txt index 06b1411..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) +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 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" 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() 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 diff --git a/source.cpp b/source.cpp index 229b936..b1ff1a2 100644 --- a/source.cpp +++ b/source.cpp @@ -1,3 +1,8 @@ -int main(int /*argc*/, char** /*argv*/){ +void func() { + int anotherUnused; +} + +int main(int /*argc*/, char** argv){ + int unused = 0; return 0; }