Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: Static analysis
name: Static analysis_

on:
push:
branch:
branches:
- develop
- master
- main

pull_request_target:
branch:
branches:
- "*"

jobs:
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
36 changes: 36 additions & 0 deletions another.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>

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;
}
3 changes: 0 additions & 3 deletions ci/test.sh

This file was deleted.

46 changes: 46 additions & 0 deletions file_with_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
"""
Static Analysis Checker using Pylint

Usage:
python static_analysis.py <file1.py> [<file2.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: {} <python_file.py> [<python_file2.py> ...]".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()
66 changes: 66 additions & 0 deletions new_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
#include <vector>

// 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;
}
7 changes: 6 additions & 1 deletion source.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
Loading