analyze_memory_safety
Analyze C++ code for memory safety issues like leaks, dangling pointers, and unsafe operations to identify and fix vulnerabilities.
Instructions
分析 C++ 代码中的内存安全问题
参数:
code: 要分析的 C++ 代码
返回:
内存安全分析报告,包括潜在的内存泄漏、悬空指针、不安全操作等
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes |
Implementation Reference
- cpp_style_server.py:90-103 (handler)MCP tool handler and registration for 'analyze_memory_safety'. Instantiates MemorySafetyAnalyzer and delegates analysis, returning the formatted report. Includes input/output schema in docstring.@mcp.tool() def analyze_memory_safety(code: str) -> str: """ 分析 C++ 代码中的内存安全问题 参数: code: 要分析的 C++ 代码 返回: 内存安全分析报告,包括潜在的内存泄漏、悬空指针、不安全操作等 """ analyzer = get_memory_analyzer() issues, report = analyzer.analyze_memory_safety(code) return report
- Core implementation of memory safety analysis in MemorySafetyAnalyzer class. Uses regex patterns to detect raw pointers, manual memory ops, unsafe strings, leaks, etc., and generates detailed report.def analyze_memory_safety(self, code: str) -> Tuple[List[Dict], str]: """ 分析代码中的内存安全问题 Args: code: 要分析的 C++ 代码 Returns: (问题列表, 格式化的分析报告) """ issues = [] # 检查各种内存安全问题 issues.extend(self._check_raw_pointers(code)) issues.extend(self._check_manual_memory(code)) issues.extend(self._check_array_access(code)) issues.extend(self._check_string_operations(code)) issues.extend(self._check_resource_leaks(code)) issues.extend(self._check_double_delete(code)) issues.extend(self._check_dangling_pointers(code)) # 生成报告 report = self._generate_report(issues, code) return issues, report
- Singleton factory function providing the shared MemorySafetyAnalyzer instance used by the tool handler.def get_analyzer() -> MemorySafetyAnalyzer: """获取全局内存安全分析器实例""" global _analyzer if _analyzer is None: _analyzer = MemorySafetyAnalyzer() return _analyzer
- cpp_style_server.py:90-90 (registration)FastMCP decorator registering the analyze_memory_safety tool.@mcp.tool()