`JS_ToCStringLen2` leaks the string when the UTF-8 buffer allocation fails
维护者通常 1 天内回复
评估
调研方向
阅读 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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
quickjs-ng/quickjs 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
quickjs-ng/quickjs#1972 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
quickjs-ng/quickjs#1971 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 79/100
quickjs-ng/quickjs#1970 ·
维护者通常 1 天内回复
-
JS_SetPropertyInternal2 loses the value when a lazy property on the prototype runs out of memory可能已有人在做 @TheNetherWatcher 于 11 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
quickjs-ng/quickjs#1852 ·
维护者通常 1 天内回复
-
Object.prototype.propertyIsEnumerable converts `this` before the property key可能已有人在做 @SomSamantray 于 52 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
quickjs-ng/quickjs#1772 ·
维护者通常 1 天内回复
查看 quickjs-ng/quickjs 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
fastfetch-cli/fastfetch#2780 ·
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 72/100
elliotchance/vsql#2079 ·
维护者通常 1 天内回复
-
docs
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 61/100
维护者通常 13 天内回复
-
threadpool: a failed `pthread_cond_wait` makes Mlucas hang instead of stopping可能已有人在做 @leinardi 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 77/100
primesearch/Mlucas#398 ·