write me a linter to check forbidden function for c program in robust way (dont use…
- User
write me a linter to check forbidden function for c program in robust way (dont use regex), all defined function in project is allowed { "allow_funcs": ["malloc", "printf", "hello_*"] "excludes": ["folder", "file.c"] }
Assistant
Okay, this is a great challenge! Using a proper C parser is the robust way to go, as it understands C syntax, comments, strings, macros (to some extent), etc., which regex would struggle with immensely. We'll use the Python bindings for
libclang.Core Idea:
- Configuration: Load allowed functions (with wildcards) and exclusion rules from a JSON file.
- First Pass (Discover Project Functions):
- Traverse all
.cand.hfiles in the project (respecting exclusions). - Use
libclangto parse these files. - Identify all function definitions within the project.
- Add these function names to a dynamic set of allowed functions.
- Traverse all
- Second Pass (Linting):
- Traverse all
.cfiles in the project (respecting exclusions). - Use
libclangto parse these files. - Identify all function calls.
- For each function call, check if the called function's name is:
- In the set of project-defined functions.
- In the explicitly configured
allow_funcs(exact match). - Matches a wildcard pattern in
allow_funcs.
- If none of the above, report it as a forbidden function call.
- Traverse all
Prerequisites:
- Python 3
libclang: Install withpip install libclang. You might need to have Clang installed on your system, andlibclangmight need help finding the Clang shared library.
1. Configuration File (
linter_config.json)Create a file named
linter_config.json(or similar) in your project root:{ "allow_funcs": [ "malloc", "free", "printf", "fprintf", "scanf", "fopen", "fclose", "exit", "my_custom_lib_*" ], "excludes": [ "build/", "tests/mock_files/", "third_party_lib/some_specific_file.c" ], "source_dirs": ["src", "include", "."], "header_extensions": [".h", ".hpp"], "source_extensions": [".c", ".cpp"] }source_dirs: Directories to scan for source and header files.header_extensions,source_extensions: To identify relevant files.
2. The Linter Script (
c_linter.py)import os import json import fnmatch import sys from pathlib import Path import clang.cindex # --- libclang setup (might be needed on some systems) --- # Try to find libclang.so or libclang.dll # You might need to set CLANG_LIBRARY_PATH environment variable # or specify the path directly if it's not found automatically. # Example: clang.cindex.Config.set_library_file('/usr/lib/x86_64-linux-gnu/libclang-10.so') # common_paths = [ # '/usr/lib/llvm-14/lib/libclang.so.1', # '/usr/lib/llvm-13/lib/libclang.so.1', # '/usr/lib/llvm-12/lib/libclang.so.1', # '/usr/lib/llvm-11/lib/libclang.so.1', # '/usr/lib/llvm-10/lib/libclang.so.1', # '/Library/Developer/CommandLineTools/usr/lib/libclang.dylib', # macOS # # Add paths for Windows if necessary # ] # for path in common_paths: # if os.path.exists(path): # clang.cindex.Config.set_library_file(path) # print(f"Using libclang: {path}") # break # else: # try: # # Try loading without explicit path, hoping it's in LD_LIBRARY_PATH or PATH # clang.cindex.Config.set_library_file(clang.cindex.Config.get_filename()) # print(f"Using libclang: {clang.cindex.Config.get_filename()}") # except clang.cindex.LibclangError: # print("Warning: libclang not found or configured. Please set its path if parsing fails.") # print("You can try setting the CLANG_LIBRARY_PATH environment variable or use:") # print("clang.cindex.Config.set_library_file('/path/to/libclang.so')") # Attempt to load the library, will raise an exception if it fails. try: if clang.cindex.Config.loaded: print(f"libclang {clang.cindex.conf.lib.clang_getClangVersion()} already loaded.") else: # This might be necessary on systems where libclang isn't found automatically # For example, on Ubuntu with clang-14: # clang.cindex.Config.set_library_file('/usr/lib/x86_64-linux-gnu/libclang-14.so.1') # Or let it try to find it clang.cindex.Config.set_library_path(os.path.dirname(clang.cindex.Config.get_filename())) print(f"Successfully loaded libclang from default search path: {clang.cindex.Config.get_filename()}") except Exception as e: print(f"Error loading libclang: {e}", file=sys.stderr) print("Please ensure libclang is installed and its path is discoverable.", file=sys.stderr) print("You might need to set CLANG_LIBRARY_PATH or use clang.cindex.Config.set_library_file().", file=sys.stderr) sys.exit(1) class CFunctionLinter: def __init__(self, config_file_path): self.config_file_path = Path(config_file_path).resolve() self.project_root = self.config_file_path.parent try: with open(self.config_file_path, 'r') as f: self.config = json.load(f) except FileNotFoundError: print(f"Error: Config file not found at {self.config_file_path}", file=sys.stderr) sys.exit(1) except json.JSONDecodeError as e: print(f"Error: Could not parse config file {self.config_file_path}: {e}", file=sys.stderr) sys.exit(1) self.allow_funcs_exact = set(f for f in self.config.get("allow_funcs", []) if "*" not in f and "?" not in f) self.allow_funcs_patterns = [f for f in self.config.get("allow_funcs", []) if "*" in f or "?" in f] raw_excludes = self.config.get("excludes", []) self.excludes = [ (self.project_root / Path(p)).resolve() for p in raw_excludes] self.source_dirs = [ (self.project_root / Path(d)).resolve() for d in self.config.get("source_dirs", ["."])] self.header_extensions = tuple(self.config.get("header_extensions", [".h", ".hpp"])) self.source_extensions = tuple(self.config.get("source_extensions", [".c", ".cpp"])) self.project_defined_functions = set() self.clang_index = clang.cindex.Index.create() self.errors = [] def _is_excluded(self, file_path: Path) -> bool: """Checks if a file or directory path should be excluded.""" resolved_path = file_path.resolve() for excluded_path in self.excludes: if resolved_path == excluded_path or \ (excluded_path.is_dir() and excluded_path in resolved_path.parents): return True # Handle file exclusion specifically if excluded_path is a file if not excluded_path.is_dir() and resolved_path == excluded_path: return True return False def _collect_project_files(self): """Collects all relevant source and header files, respecting exclusions.""" project_files = [] for src_dir_path in self.source_dirs: if not src_dir_path.exists(): print(f"Warning: Source directory {src_dir_path} does not exist. Skipping.", file=sys.stderr) continue if self._is_excluded(src_dir_path): continue for root, dirs, files in os.walk(src_dir_path, topdown=True): current_dir_path = Path(root).resolve() # Filter out excluded directories dirs[:] = [d for d in dirs if not self._is_excluded(current_dir_path / d)] for file in files: file_path = current_dir_path / file if not self._is_excluded(file_path): if file.endswith(self.header_extensions) or file.endswith(self.source_extensions): project_files.append(file_path) return project_files def _parse_file(self, file_path: Path): """Parses a file using libclang.""" # Try to infer include paths from common project structures # This is a simplification; a real build system would provide these. # Add project root and 'include' directories relative to project root. args = ['-x', 'c' if file_path.suffix == '.c' else 'c++'] # Basic language flag # Add standard include directories that might be relevant # common_includes = ['/usr/include', '/usr/local/include'] # Example for Linux # for inc_path in common_includes: # args.append(f'-I{inc_path}') # Add project-specific include dirs, if any exist and are configured or standard possible_include_dirs = [ self.project_root / "include", self.project_root / "inc" ] for inc_dir in self.source_dirs: # Also consider source_dirs as potential include roots if inc_dir.is_dir(): possible_include_dirs.append(inc_dir) for inc_dir in possible_include_dirs: if inc_dir.exists() and inc_dir.is_dir(): args.append(f'-I{str(inc_dir.resolve())}') # Add the directory of the current file as an include path args.append(f'-I{str(file_path.parent.resolve())}') try: # You might need to pass more specific compiler flags for your project # e.g., -std=c11, -I/path/to/project/includes # For robustness, clang needs to know how to resolve includes. # Adding common include paths or project-specific ones is crucial. tu = self.clang_index.parse(str(file_path), args=args) # Translation Unit for diag in tu.diagnostics: if diag.severity >= clang.cindex.Diagnostic.Error: # Log Clang errors but try to continue; it's often resilient print(f"Clang Error: {diag.spelling} in {diag.location.file}:{diag.location.line}", file=sys.stderr) return tu except clang.cindex.TranslationUnitLoadError as e: print(f"Error parsing {file_path}: {e}", file=sys.stderr) return None def _find_function_definitions(self, cursor): """Recursively finds function definitions.""" if cursor.kind == clang.cindex.CursorKind.FUNCTION_DECL and cursor.is_definition(): # Ensure the definition is in one of our project files, not from a system header if cursor.location.file and Path(cursor.location.file.name).resolve().is_relative_to(self.project_root): self.project_defined_functions.add(cursor.spelling) for child in cursor.get_children(): self._find_function_definitions(child) def _is_function_allowed(self, func_name: str) -> bool: if not func_name: # Should not happen with valid CALL_EXPR return True if func_name in self.project_defined_functions: return True if func_name in self.allow_funcs_exact: return True for pattern in self.allow_funcs_patterns: if fnmatch.fnmatch(func_name, pattern): return True return False def _find_forbidden_calls(self, cursor, file_path: Path): """Recursively finds function calls and checks them.""" # We are interested in CALL_EXPR (function calls) if cursor.kind == clang.cindex.CursorKind.CALL_EXPR: # cursor.spelling gives the name of the function as written # cursor.referenced.spelling gives the canonical name of the function being called (more reliable) func_name = cursor.spelling if cursor.referenced: # If clang resolved the reference func_name = cursor.referenced.spelling # Check if the call is from within the project files (not expanded from a system macro to a system function) # This check can be tricky if macros are involved. The primary check is still on the function name. is_in_project_file = cursor.location.file and \ Path(cursor.location.file.name).resolve().is_relative_to(self.project_root) if func_name and is_in_project_file and not self._is_function_allowed(func_name): relative_file_path = file_path.relative_to(self.project_root) self.errors.append( f"{relative_file_path}:{cursor.location.line}:{cursor.location.column}: " f"Forbidden function call: '{func_name}'" ) for child in cursor.get_children(): self._find_forbidden_calls(child, file_path) def lint(self): """Performs the full linting process.""" self.errors = [] self.project_defined_functions = set() # Reset for fresh run print("Phase 1: Discovering project-defined functions...") project_files = self._collect_project_files() if not project_files: print("No source/header files found to process based on configuration.", file=sys.stderr) return for file_path in project_files: # print(f" Scanning for definitions: {file_path.relative_to(self.project_root)}") tu = self._parse_file(file_path) if tu: self._find_function_definitions(tu.cursor) print(f"Found {len(self.project_defined_functions)} project-defined functions.") # For debugging: print(", ".join(sorted(list(self.project_defined_functions)))) print("\nPhase 2: Linting for forbidden function calls...") source_files_to_lint = [f for f in project_files if f.name.endswith(self.source_extensions)] for file_path in source_files_to_lint: # print(f" Linting: {file_path.relative_to(self.project_root)}") tu = self._parse_file(file_path) if tu: self._find_forbidden_calls(tu.cursor, file_path) if self.errors: print("\nForbidden function calls found:") for error in self.errors: print(error) return False # Indicate failure else: print("\nNo forbidden function calls found. ✨") return True # Indicate success def main(): if len(sys.argv) < 2: config_path_arg = "linter_config.json" print(f"No config file specified, defaulting to '{config_path_arg}' in current directory.") else: config_path_arg = sys.argv[1] config_file = Path(config_path_arg) if not config_file.is_file(): print(f"Error: Linter configuration file '{config_file}' not found.", file=sys.stderr) # Try to locate it relative to the script itself, if not an absolute path script_dir_config = Path(__file__).parent / config_path_arg if script_dir_config.is_file(): print(f"Found config at: {script_dir_config}") config_file = script_dir_config else: sys.exit(1) linter = CFunctionLinter(config_file) success = linter.lint() sys.exit(0 if success else 1) if __name__ == "__main__": main()
3. Example Project Structure & Files:
my_c_project/ ├── linter_config.json ├── c_linter.py ├── src/ │ ├── main.c │ ├── utils.c │ └── helper/ │ └── specific_helper.c ├── include/ │ ├── utils.h │ └── helper/ │ └── specific_helper.h └── build/ (this will be excluded) └── (some build artifacts)Example
src/main.c:#include <stdio.h> // printf is allowed #include <stdlib.h> // malloc is allowed #include "utils.h" // project-defined functions will be allowed #include "helper/specific_helper.h" // This is a project defined function, so it's allowed void project_main_function(void) { printf("Inside project_main_function\n"); } int main() { project_main_function(); // Allowed (project-defined) char *mem = malloc(100); // Allowed (config) if (mem) { printf("Memory allocated.\n"); // Allowed (config) free(mem); // Allowed (config) } process_data(5); // Allowed (project-defined in utils.c) helper_do_something(); // Allowed (project-defined in specific_helper.c) // Forbidden calls // system("ls"); // Should be forbidden if "system" is not in allow_funcs // strcpy(mem, "test"); // Should be forbidden if "strcpy" is not in allow_funcs my_custom_lib_init(); // Allowed (matches "my_custom_lib_*" in config) return 0; }Example
include/utils.h:#ifndef UTILS_H #define UTILS_H void process_data(int x); void util_another_func(void); // Declaration #endifExample
src/utils.c:#include "utils.h" #include <stdio.h> // For printf // Definition void process_data(int x) { printf("Processing data: %d\n", x); util_another_func(); // Call to another project defined func } // Definition void util_another_func(void) { printf("Inside util_another_func\n"); } // This should be forbidden unless "dangerous_function" is allowed void some_internal_call(void) { // dangerous_function(); }Example
include/helper/specific_helper.h:#ifndef SPECIFIC_HELPER_H #define SPECIFIC_HELPER_H void helper_do_something(void); #endifExample
src/helper/specific_helper.c:#include "helper/specific_helper.h" #include <stdio.h> void helper_do_something(void) { printf("Helper is doing something.\n"); // Let's add a forbidden call here for testing // puts("This might be forbidden"); } void my_custom_lib_init(void) { printf("Custom lib init called.\n"); } void my_custom_lib_close(void) { printf("Custom lib close called.\n"); }
How to Run:
- Save
c_linter.pyandlinter_config.jsonin your project's root directory (or adjust paths). - Make sure your project files (
.c,.h) are in place. - Ensure
libclangis installed (pip install libclang) and its shared library is findable.- On Linux, you might need
sudo apt-get install libclang-dev(or a specific version likelibclang-14-dev). - On macOS, Xcode Command Line Tools usually provide it.
- You might need to uncomment and adjust the
clang.cindex.Config.set_library_file(...)line inc_linter.pyif Python can't findlibclangautomatically.
- On Linux, you might need
- Run from your project root:
python c_linter.py linter_config.jsonor iflinter_config.jsonis in the current dir:python c_linter.py
Key Robustness Points:
- Actual Parsing:
libclangbuilds an Abstract Syntax Tree (AST). It understands C syntax, comments, string literals, etc., so it won't incorrectly flagprintf("call_forbidden_func();");. - Function Definitions vs. Declarations: It correctly distinguishes between a function being defined (
cursor.is_definition()) and just declared or called. - Function Calls: It identifies
CALL_EXPRnodes in the AST.cursor.referenced.spellingis quite reliable for getting the canonical name of the called function. - Wildcards:
fnmatchis used for simple shell-style wildcards, not regex. - Exclusions: Path-based exclusions are handled for both files and directories.
- Project-Defined Functions: The two-pass system ensures that any function defined anywhere in your non-excluded project files is automatically allowed.
Important Considerations & Potential Improvements:
- Include Paths (
-I): Forlibclangto work correctly, especially with#includedirectives for project headers, it needs to know where to find them. The script attempts to add common locations (project root,include/,inc/, current file's directory). For complex projects, you might need to pass more-Iflags, perhaps extracted from your build system (e.g.,compile_commands.json). - Macros:
- If a macro expands to a forbidden function call,
libclangwill usually see the expanded call and flag it. - If a macro looks like a function call but doesn't expand to one (e.g.,
MY_ASSERT(x)), it won't be aCALL_EXPRand won't be flagged by this linter (which is correct, as it's not a C function call).
- If a macro expands to a forbidden function call,
- Function Pointers: Detecting which specific function is called through a function pointer at static analysis time is generally very hard or impossible. This linter will see the call via the pointer variable. If
ptr_to_func()is called, it will check if "ptr_to_func" is an allowed name. It won't (easily) know ifptr_to_funccurrently points toforbidden_func. - C++: The script has basic support for C++ file extensions and the
-x c++flag. C++ has more complex call mechanisms (methods, templates, operators) that might require more sophisticated AST traversal and checking. - Performance: For very large projects, parsing every file twice might be slow.
libclangis relatively fast, but it's still I/O and CPU bound. - Error Reporting for Clang: The script prints Clang parsing errors to
stderr. If Clang cannot parse a file due to syntax errors or missing headers, the linting for that file might be incomplete.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.