`JS_ToCStringLen2` leaks the string when the UTF-8 buffer allocation fails

未关闭 适合新手
#1,965 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@bnoordhuis 已经在做这个了。

开始于 2026年12月1日。

  • #1966 来自 @bnoordhuis —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c
领域
backend

调研方向

阅读 quickjs.c 中的 JS_ToCStringLen2,并跟踪分配失败路径直到 fail 标签。按照 issue 中的描述,使用内存受限的 runtime 和一个较长的非 ASCII 字符串重现问题。确保失败时释放值引用,然后验证释放 runtime 时不再出现泄漏报告。

由索引模型根据 Issue 内容生成。

描述

In JS_ToCStringLen2 (quickjs.c), val = js_force_tostring(ctx, val1) takes a reference. When the string is not plain ASCII, the function allocates the UTF-8 copy with js_alloc_string; if that allocation fails, it jumps to fail:

str_new = js_alloc_string(ctx, len + count, 0);
if (!str_new)
    goto fail;

Same for the wide-character branch, js_alloc_string(ctx, len * 3, 0). The fail label only clears *plen and returns NULL; JS_FreeValue(ctx, val) is never called on this path, so the string keeps one reference.

Reproduction: a runtime with a memory limit set just above its current use, then JS_ToCStringLen2 on a long non-ASCII string (for example 'é'.repeat(32768)). With assertions on, JS_FreeRuntime reports the string under "String leaks" and aborts; without them the string stays until the runtime is freed.

A fix: free val before returning on the failure path, e.g.

fail:
    JS_FreeValue(ctx, val);   /* val is JS_EXCEPTION when js_force_tostring failed; freeing it is a no-op */
    if (plen)
        *plen = 0;
    return NULL;
主要语言
C
星标
3.9k
派生
389
平均合并
2 天 2 小时
30 天内合并 PR
21

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

quickjs-ng/quickjs 的其他 Issue

查看 quickjs-ng/quickjs 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。