Static analysis tools for QGroundControl C++ code.
Detects unsafe patterns where activeVehicle() or getParameter() results are used without null checks.
# Analyze specific files
python3 tools/analyzers/vehicle_null_check.py src/Vehicle/*.cc
# Analyze directory recursively
python3 tools/analyzers/vehicle_null_check.py src/
# JSON output for CI/editor integration
python3 tools/analyzers/vehicle_null_check.py --json src/
# Show help
python3 tools/analyzers/vehicle_null_check.py --help| Pattern | Risk | Example |
|---|---|---|
unsafe_active_vehicle_direct |
High | activeVehicle()->method() |
unsafe_active_vehicle_use |
High | Variable used after activeVehicle() without check |
unsafe_get_parameter |
Medium | getParameter(...)->rawValue() |
src/Example.cc:42:15: warning: unsafe_active_vehicle_direct
activeVehicle()->parameterManager()->getParameter(...);
Suggestion: Add null check before using activeVehicle():
Vehicle *vehicle = MultiVehicleManager::instance()->activeVehicle();
if (!vehicle) return;
[
{
"file": "src/Example.cc",
"line": 42,
"column": 15,
"pattern": "unsafe_active_vehicle_direct",
"code": "activeVehicle()->parameterManager()->...",
"suggestion": "Add null check before using activeVehicle():..."
}
]Already configured in .pre-commit-config.yaml. Runs automatically on C++ files.
# Run manually on all files
pre-commit run vehicle-null-check --all-filesThe analyzer looks back 10 lines for null checks. If you have a valid null check that isn't detected, restructure your code to have the check closer to the usage, or add a comment explaining why it's safe.
// Safe: null check is visible to analyzer
Vehicle *vehicle = MultiVehicleManager::instance()->activeVehicle();
if (!vehicle) {
return;
}
vehicle->doSomething(); // OK - analyzer sees the check aboveThe analyzers use shared patterns from tools/common/:
patterns.py- Regex patterns for QGC code constructsfile_traversal.py- File discovery with proper filtering
- Create new Python script in
tools/analyzers/ - Import shared patterns from
tools.common.patterns - Use
find_cpp_files()fromtools.common.file_traversal - Support
--jsonoutput for editor integration - Add to
.pre-commit-config.yamlif appropriate