analyze_memory_safety
Analyze C++ code to detect memory safety issues like leaks, dangling pointers, and unsafe operations, providing a detailed report for secure coding.
Instructions
分析 C++ 代码中的内存安全问题
参数:
code: 要分析的 C++ 代码
返回:
内存安全分析报告,包括潜在的内存泄漏、悬空指针、不安全操作等
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes |
Implementation Reference
- cpp_style_server.py:90-104 (handler)MCP tool handler for 'analyze_memory_safety'. Registers the tool via @mcp.tool() decorator and implements the logic by delegating to MemorySafetyAnalyzer instance.@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 the memory safety analysis. Uses multiple helper methods with regex patterns to detect raw pointers, manual memory ops, unsafe strings, leaks, etc., and generates a formatted 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
- Factory function to get singleton instance of MemorySafetyAnalyzer, imported and used as get_memory_analyzer() in the server.# 全局实例 _analyzer = None def get_analyzer() -> MemorySafetyAnalyzer: """获取全局内存安全分析器实例""" global _analyzer if _analyzer is None: _analyzer = MemorySafetyAnalyzer() return _analyzer
- cpp_style_server.py:90-90 (registration)The @mcp.tool() decorator registers the analyze_memory_safety function as an MCP tool.@mcp.tool()