From 136e2d14da31b846aa097fb6553ad72fe980b92a Mon Sep 17 00:00:00 2001 From: vinland100 Date: Fri, 30 Jan 2026 15:23:38 +0800 Subject: [PATCH] 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. --- backend/app/services/scanner.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/backend/app/services/scanner.py b/backend/app/services/scanner.py index eef9487..c8bbc03 100644 --- a/backend/app/services/scanner.py +++ b/backend/app/services/scanner.py @@ -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)