fix: replace unsafe strcpy with memcpy in setLibNames - #398
Conversation
1. Root cause: setLibNames() used strcpy() which does not bound the copy length, flagged by clang-analyzer as CWE-119 risk 2. Fix: extract size_t len = strlen(tmp.chDocumentPr) + 1, use memcpy with len for bounded copy, reuse len for malloc too 3. Impact: no behavior change, memcpy copies identical content including null terminator, eliminates redundant strlen call Influence: 1. Verify application startup loads dynamic library path correctly 2. Verify document processing functions work as expected 3. No regression expected as copy semantics are identical fix: 替换 setLibNames 中不安全的 strcpy 为 memcpy 1. 根因:setLibNames() 使用 strcpy() 进行拷贝,该函数不限制拷贝 长度,被 clang-analyzer 标记为 CWE-119 安全风险 2. 方案:提取 size_t len = strlen(tmp.chDocumentPr) + 1 变量, 使用 memcpy 配合 len 做有界拷贝,len 同时复用于 malloc 3. 影响:无行为变化,memcpy 拷贝内容与 strcpy 完全一致(含 null 终止符),同时消除冗余 strlen 调用 Influence: 1. 验证应用启动时动态库路径正确加载 2. 验证文档处理功能正常工作 3. 拷贝语义完全一致,预期无回归 PMS: BUG-212
There was a problem hiding this comment.
Sorry @pengfeixx, you've used your own review budget of 250,000 diff characters for the last 7 days.
You can request another review in 3 days and 15 hours by commenting @sourcery-ai review. Upgrade to get a review now.
Reviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates setLibNames() to compute the document-library path length once, allocate the exact buffer size, and replace unbounded strcpy with a length-bounded memcpy while preserving existing behavior and error handling. Sequence diagram for bounded library path copyingsequenceDiagram
participant Caller
participant setLibNames
participant Memory
Caller->>setLibNames: setLibNames(tmp)
setLibNames->>setLibNames: strlen(tmp.chDocumentPr) + 1
setLibNames->>Memory: malloc(len)
alt allocation succeeds
setLibNames->>Memory: memcpy(g_ldnames.chDocumentPr, tmp.chDocumentPr, len)
else allocation fails
setLibNames-->>Caller: return
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto reviewAI 代码审查报告
总体评分
总体评价: 代码审查通过 本次变更将 漏洞统计
漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个 变更文件
变更内容分析变更说明本次 PR 的目的是将 代码 diff--- a/reader/load_libs.c
+++ b/reader/load_libs.c
@@ -115,12 +115,12 @@ void setLibNames(LoadLibNames tmp)
g_ldnames.chDocumentPr = NULL;
} else {
fprintf(stderr, "INFO: Copying library path: %s\n", tmp.chDocumentPr);
- g_ldnames.chDocumentPr = (char*)malloc(strlen(tmp.chDocumentPr)+1);
+ size_t len = strlen(tmp.chDocumentPr) + 1;
+ g_ldnames.chDocumentPr = (char*)malloc(len);
if (!g_ldnames.chDocumentPr) {
fprintf(stderr, "ERROR: Failed to allocate memory for library path\n");
return;
}
- strcpy(g_ldnames.chDocumentPr,tmp.chDocumentPr);
+ memcpy(g_ldnames.chDocumentPr, tmp.chDocumentPr, len);
}
}四维度详细分析维度1:语法逻辑(25/25)✓
分析要点:
预存在建议(非本次 PR 引入):
维度2:代码质量(25/25)✓
分析要点:
维度3:代码性能(20/20)✓
分析要点:
维度4:代码安全(30/30)✓
分析要点:
OCR 审查结果OCR(OpenCodeReview)对变更文件进行了专业代码审查,发现以下问题:
改进建议建议修复预存在的内存泄漏问题文件: 在 void setLibNames(LoadLibNames tmp)
{
fprintf(stderr, "INFO: Setting library names\n");
if(tmp.chDocumentPr == NULL) {
fprintf(stderr, "WARNING: Received NULL document printer library path\n");
free(g_ldnames.chDocumentPr); // 释放旧内存
g_ldnames.chDocumentPr = NULL;
} else {
fprintf(stderr, "INFO: Copying library path: %s\n", tmp.chDocumentPr);
size_t len = strlen(tmp.chDocumentPr) + 1;
free(g_ldnames.chDocumentPr); // 释放旧内存
g_ldnames.chDocumentPr = (char*)malloc(len);
if (!g_ldnames.chDocumentPr) {
fprintf(stderr, "ERROR: Failed to allocate memory for library path\n");
return;
}
memcpy(g_ldnames.chDocumentPr, tmp.chDocumentPr, len);
}
}审查结论本次 PR 的目的是将
本次变更未引入任何新的安全漏洞或代码质量问题。OCR 审查发现的内存泄漏问题为预存在问题,建议在后续迭代中修复。 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: lzwind, pengfeixx The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
fix: replace unsafe strcpy with memcpy in setLibNames
the copy length, flagged by clang-analyzer as CWE-119 risk
memcpy with len for bounded copy, reuse len for malloc too
including null terminator, eliminates redundant strlen call
Influence:
fix: 替换 setLibNames 中不安全的 strcpy 为 memcpy
长度,被 clang-analyzer 标记为 CWE-119 安全风险
使用 memcpy 配合 len 做有界拷贝,len 同时复用于 malloc
null 终止符),同时消除冗余 strlen 调用
Influence:
PMS: BUG-212
Summary by Sourcery
Bug Fixes:
setLibNameswith a length-bounded copy to address the identified memory-safety risk.