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

オープン 初心者向け
#1,965 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@bnoordhuis がすでに取り組んでいます。

2026年12月1日 から。

  • #1966 @bnoordhuis による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c
領域
backend

調査の方向性

quickjs.c の JS_ToCStringLen2 を読み、割り当て失敗時の経路を fail ラベルまで追ってください。issue の説明にあるように、メモリ制限のあるランタイムと長い非 ASCII 文字列を使って再現してください。失敗時に値の参照が解放されることを確認し、ランタイムの解放時にリーク報告が表示されなくなることを検証してください。

索引モデルが 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時間
マージ済み PR(30日)
21

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

quickjs-ng/quickjs のほかの issue

quickjs-ng/quickjs の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。