fix: Add `sanitize_for_db` helper to remove NULL bytes from strings and apply it to `code_snippet`, `title`, `message`, `suggestion`, and `ai_explanation` fields before saving to the database.

This commit is contained in:
vinland100 2026-01-30 15:23:38 +08:00
parent 18613d533f
commit 136e2d14da
1 changed files with 15 additions and 5 deletions

View File

@ -686,8 +686,18 @@ async def scan_repo_task(task_id: str, db_session_factory, user_config: dict = N
print(f"⚠️ 警告: 任务 {task_id} 中文件 {f_path} 的分析结果包含无效的问题格式: {issue}")
continue
# 辅助函数:清理字符串中 PostgreSQL 不支持的字符
def sanitize_for_db(text):
if text is None:
return None
if not isinstance(text, str):
text = str(text)
# 移除 NULL 字节 (PostgreSQL 不支持)
text = text.replace('\x00', '')
return text
line_num = issue.get("line", 1)
code_snippet = issue.get("code_snippet")
code_snippet = sanitize_for_db(issue.get("code_snippet"))
if not code_snippet or len(code_snippet.strip()) < 5:
try:
idx = max(0, int(line_num) - 1)
@ -704,11 +714,11 @@ async def scan_repo_task(task_id: str, db_session_factory, user_config: dict = N
column_number=issue.get("column"),
issue_type=issue.get("type", "maintainability"),
severity=issue.get("severity", "low"),
title=issue.get("title", "Issue"),
message=issue.get("description") or issue.get("title", "Issue"),
suggestion=issue.get("suggestion"),
title=sanitize_for_db(issue.get("title", "Issue")),
message=sanitize_for_db(issue.get("description") or issue.get("title", "Issue")),
suggestion=sanitize_for_db(issue.get("suggestion")),
code_snippet=code_snippet,
ai_explanation=issue.get("ai_explanation"),
ai_explanation=sanitize_for_db(issue.get("ai_explanation")),
status="open"
)
db.add(audit_issue)