Checker for bare new/delete/malloc/calloc/realloc/free usage.
| 81 | |
| 82 | |
| 83 | class BareAllocationChecker(FileContentChecker): |
| 84 | """Checker for bare new/delete/malloc/calloc/realloc/free usage.""" |
| 85 | |
| 86 | def __init__(self) -> None: |
| 87 | self.violations: dict[str, list[tuple[int, str]]] = {} |
| 88 | |
| 89 | def should_process_file(self, file_path: str) -> bool: |
| 90 | if not file_path.endswith((".cpp", ".h", ".hpp", ".ino", ".cpp.hpp")): |
| 91 | return False |
| 92 | |
| 93 | if any(file_path.endswith(excluded) for excluded in EXCLUDED_FILES): |
| 94 | return False |
| 95 | |
| 96 | if "third_party" in file_path or "thirdparty" in file_path: |
| 97 | return False |
| 98 | |
| 99 | normalized = file_path.replace("\\", "/") |
| 100 | if any(normalized.endswith(suffix) for suffix in _WHITELISTED_SUFFIXES): |
| 101 | return False |
| 102 | |
| 103 | return True |
| 104 | |
| 105 | def check_file_content(self, file_content: FileContent) -> list[str]: |
| 106 | violations: list[tuple[int, str]] = [] |
| 107 | in_multiline_comment = False |
| 108 | |
| 109 | for line_number, line in enumerate(file_content.lines, 1): |
| 110 | stripped = line.strip() |
| 111 | |
| 112 | # Track multi-line comment state |
| 113 | if "/*" in line: |
| 114 | in_multiline_comment = True |
| 115 | if "*/" in line: |
| 116 | in_multiline_comment = False |
| 117 | continue |
| 118 | |
| 119 | if in_multiline_comment: |
| 120 | continue |
| 121 | |
| 122 | # Skip single-line comment lines |
| 123 | if stripped.startswith("//"): |
| 124 | continue |
| 125 | |
| 126 | # Check for suppression comment on this line |
| 127 | if _SUPPRESSION_OK in line or _SUPPRESSION_OKAY in line: |
| 128 | continue |
| 129 | |
| 130 | # Fast first pass: skip expensive regex/string-strip if line contains |
| 131 | # none of the allocation keywords (allows false positives, no false negatives) |
| 132 | if not ( |
| 133 | "new" in line |
| 134 | or "delete" in line |
| 135 | or "malloc" in line |
| 136 | or "calloc" in line |
| 137 | or "realloc" in line |
| 138 | or "free" in line |
| 139 | ): |
| 140 | continue |
no outgoing calls
no test coverage detected