R16: 专家漏洞评审 — 发现并修复嵌套COPYBOOK解析bug

评审方法:14项实机验证,非静态审查
  1. 非确定性输出检测 ✓ 5次运行值一致
  2. 边缘COBOL功能crash测试 (ALTER/ENTRY) ✓ 不崩溃
  3. 大规模程序性能 (500字段+250IF) ✓ 数秒完成
  4. 路径爆炸防护 (10IF in PERFORM UNTIL) ✓ 不爆炸
  5. 嵌套COPYBOOK解析 → 发现BUG并修复
  6. 嵌套IF深度  ✓
  7. 畸形JCL输入 (二进制/BOM/1000行延续) ✓ 不崩溃
  8. 注释中KEY字串误触发matching ✓ 不误报
  9. 变量名包含关键词子串FP ✓ WS-SORT-KEY不触发SORT
  10. 非COBOL输入 (中日文/HTML/二进制) ✓ 不误报
  11. OPEN I-O方向解析 ✓
  12. DataWriter JSON格式 ✓
  13. 跨运行隔离 ✓
  14. Config加载 ✓

修复: resolve_copybooks 增加递归参数+深度保护
  之前: COPY L1 -> L1.cpy含'COPY L2.'不被解析
  之后: 递归解析,上限10层防循环

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
NB-076
2026-06-22 10:49:18 +08:00
parent cbffb843fb
commit 9cefbdf114
2 changed files with 191 additions and 1 deletions
+6 -1
View File
@@ -92,7 +92,7 @@ def extract_procedure_division(source: str) -> str:
_COPYBOOK_EXTENSIONS = ['.cpy', '.cbl', '.cpb', '']
def resolve_copybooks(source: str, source_dir: str) -> str:
def resolve_copybooks(source: str, source_dir: str, _recursion_depth: int = 0) -> str:
"""Find COPY statements and replace with copybook content."""
_RE_COPY = re.compile(
r"^\s*COPY\s+(\w[\w-]*)(?:\s+REPLACING\s+(.+?))?\s*\.?\s*$",
@@ -113,7 +113,12 @@ def resolve_copybooks(source: str, source_dir: str) -> str:
found = p
break
if found:
if _recursion_depth > 10:
logger.warning(f"COPY circular dependency detected for {name}, skipping")
continue
cb = found.read_text(encoding='utf-8')
# Recursively resolve nested COPY inside the copybook
cb = resolve_copybooks(cb, source_dir, _recursion_depth + 1)
if m.group(2):
pairs = _RE_PAIR.findall(m.group(2))
for old, new in pairs: