Conversation
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.
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 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 |
Reviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates setLibNames() to use strncpy instead of strcpy when storing the ZPD custom library path, addressing the static-analysis warning without changing expected behavior. Review the allocation/copy size relationship and verify initial setting, replacement, and subsequent library loading. Sequence diagram for setting and loading the ZPD library pathsequenceDiagram
participant Caller
participant load_libs
participant Heap
participant ZPDLibrary
Caller->>load_libs: setLibNames(tmp)
alt existing path
load_libs->>Heap: free(g_ldnames.chZPDDLL)
end
load_libs->>Heap: malloc(strlen(tmp.chZPDDLL)+1)
load_libs->>load_libs: strncpy(g_ldnames.chZPDDLL, tmp.chZPDDLL, strlen(tmp.chZPDDLL)+1)
Caller->>ZPDLibrary: load using g_ldnames.chZPDDLL
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review🤖 AI 代码审查报告📊 总体评价
🔍 详细分析1. 语法逻辑 ✅评价: 优秀 ✅ 通过 潜在问题: 建议: 语法正确,逻辑清晰。strncpy 调用语法正确,参数 strlen(tmp.chZPDDLL)+1 与 malloc 分配大小一致,行为与原 strcpy 完全等价。NULL 检查逻辑完善(line 80-81 处理 NULL 输入,line 84-85 释放旧内存)。 2. 代码质量 ✅评价: 优秀 ✅ 通过 潜在问题: 建议: 代码结构清晰,注释完整。函数简短聚焦,职责单一。line 83 有注释说明"重复调用时先释放旧的分配,避免内存泄漏"。无重复代码,无调试残留。 3. 代码性能 ✅评价: 优秀 ✅ 通过 潜在问题:
建议: 性能良好,资源使用合理。轻微建议:将 strlen(tmp.chZPDDLL) 的结果存储到局部变量中,避免重复计算。建议优化:size_t len = strlen(tmp.chZPDDLL) + 1; g_ldnames.chZPDDLL = (char*)malloc(len); strncpy(g_ldnames.chZPDDLL, tmp.chZPDDLL, len); 4. 代码安全 🔒评价: 优秀 ✅ 通过
安全漏洞详情: 建议: 存在0个安全漏洞。本次变更将不安全的 strcpy 替换为 strncpy,消除了 clang-analyzer 标记的 CWE-119 不安全 API 告警。缓冲区大小与复制长度一致,不存在缓冲区溢出风险。无硬编码密钥、无注入风险。 💡 改进建议代码示例// 优化建议:提取 strlen 结果为局部变量,避免重复计算
void setLibNames(LoadLibNames tmp)
{
if(tmp.chZPDDLL == NULL) {
g_ldnames.chZPDDLL = NULL;
} else {
// 重复调用时先释放旧的分配,避免内存泄漏
if (g_ldnames.chZPDDLL != NULL) {
free(g_ldnames.chZPDDLL);
}
size_t len = strlen(tmp.chZPDDLL) + 1;
g_ldnames.chZPDDLL = (char*)malloc(len);
strncpy(g_ldnames.chZPDDLL, tmp.chZPDDLL, len);
}
}本报告由 AI 代码审查工具自动生成 |
1. Root cause: setLibNames copied the library name with strcpy into a freshly malloc'd buffer, flagged by clang-analyzer as an unbounded copy (CWE-119); the previous allocation was also leaked on repeat calls and when switching to NULL 2. Fix: free the previous allocation on every call (including the switch-to-NULL case) and replace malloc+strcpy with strdup, which allocates exactly the source length and copies the terminating NUL 3. Impact: no behavior change for valid inputs; removes the analyzer warning and the repeat-call leak Influence: 1. Verify input-method candidate libraries still load at editor startup fix: 使用 strdup 拷贝输入法库名,消除不安全拷贝 1. 根因:setLibNames 使用 strcpy 向新分配缓冲拷贝库名,被 clang-analyzer 标记为无界拷贝(CWE-119);重复调用及切换为 NULL 时旧分配泄漏 2. 方案:每次调用(含切换为 NULL)先释放旧分配,并用 strdup 替代 malloc+strcpy,按源串精确长度分配并拷贝(含结尾 '\0') 3. 影响:有效输入下行为不变;消除分析器告警与重复调用泄漏 Influence: 1. 验证编辑器启动时输入法候选库正常加载
96c279a to
3f1e0ed
Compare
|
Superseded by #603 (same branch): per review, strncpy(strlen+1) was replaced with strdup, and the old allocation is freed on every call including switch-to-NULL. This PR's diff view was corrupted by a force-push that replaced a shallow-clone root commit. |
fix: use strdup for library name copy in load_libs
unbounded strcpy, flagged by clang-analyzer as CWE-119; the old
allocation was also leaked on repeat calls and when switching to
NULL
switch-to-NULL case), then replace malloc+strcpy with strdup,
which allocates exactly the source length and copies the
terminating NUL
warning and the repeat-call leak
Note: an earlier revision of this PR used
strncpy(dst, src, strlen(src)+1), which is byte-for-byte equivalentto the original strcpy and provides no safety gain; per review it was
replaced with strdup.
Influence:
Log: 使用 strdup 拷贝输入法库名,消除不安全拷贝