Unparsed heredocs during string and list interpolation error recovery strand PM_LEX_HEREDOC

未关闭
#4,362 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
c, ruby

调研方向

Start with the error-recovery paths in parse_string_part, parse_interpolated_symbol_array, and parse_interpolated_string_array, where the issue says PM_TOKEN_HEREDOC_START is sent to parser_lex instead of parse_heredoc. Trace how PM_LEX_EMBEXPR and PM_LEX_HEREDOC are managed on parser->lex_modes, then reproduce the supplied input and check that parsed node locations stay within the source length. Add regression coverage for the interpolation forms listed in the issue.

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

描述

Fuzzing find on bdbd482dc.

When parse_statements(parser, PM_CONTEXT_EMBEXPR, ...) inside an interpolated string, regexp, backtick, %W[...] string array, or %I[...] symbol array stops early via context_recoverable (because it encounters a closing token belonging to an outer enclosing context, such as )), PM_LEX_EMBEXPR remains active on parser->lex_modes. The enclosing string/list loop then calls parse_string_part (or the token switch in parse_interpolated_symbol_array / parse_interpolated_string_array) to drain the remaining tokens of the unclosed interpolation.

When one of those remaining tokens is PM_TOKEN_HEREDOC_START, parse_string_part, parse_interpolated_symbol_array, and parse_interpolated_string_array hit their default: error-recovery branch and call parser_lex(parser) instead of parse_heredoc. This strands PM_LEX_HEREDOC on parser->lex_modes, which later rewinds parser->next_start backwards in the middle of parsing a subsequent construct and underflows uint32_t location.length.

Reproduction

(Interactive simulation in Prism Loupe)

#include "prism.h"
#include <inttypes.h>
#include <stdio.h>
#include <string.h>

static pm_visit_result_t
check_bounds(const pm_node_t *node, void *data) {
    size_t source_len = *(const size_t *) data;
    uint64_t end = (uint64_t) node->location.start + (uint64_t) node->location.length;

    if (end > source_len) {
        printf(
            "%-16s start=%" PRIu32 " length=%" PRIu32 " (0x%08" PRIx32 ") end=%" PRIu64 " > source_len=%zu\n",
            pm_node_type(node->type),
            node->location.start,
            node->location.length,
            node->location.length,
            end,
            source_len
        );
    }

    return PM_VISIT_DESCEND;
}

int main(void) {
    const char *src = "->x:\"#{)<<\"\n#{f\n\"#{a}\"C{";
    size_t len = strlen(src);

    pm_arena_t *arena = pm_arena_new();
    pm_parser_t *parser = pm_parser_new(arena, (const uint8_t *) src, len, NULL);
    pm_node_t *root = pm_parse(parser);

    pm_visit_node(root, check_bounds, &len);

    pm_parser_free(parser);
    pm_arena_free(arena);
    return 0;
}

Building and running against libprism.a:

cc -Iinclude check.c build/libprism.a -o check && ./check

Output on the 24-byte input (0..24):

PM_CALL_NODE     start=22 length=4294967286 (0xfffffff6) end=4294967308 > source_len=24
PM_BLOCK_NODE    start=23 length=4294967285 (0xfffffff5) end=4294967308 > source_len=24
主要语言
C
星标
1k
派生
202
平均合并
5 小时 55 分钟
30 天内合并 PR
85

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

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

ruby/prism 的其他 Issue

查看 ruby/prism 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

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