Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

C parser creates a self-referencing superclass when rb_define_class_under reuses a C variable

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

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
c, ruby
领域
documentation

调研方向

从 RDoc::Parser::C#handle_class_module 和 RDoc::Store#resolve_c_superclasses 开始,然后使用 example.c 代码片段和 rdoc --format=ri 重现该故障。检查映射如何影响 RDoc::NormalClass#ancestors。完成的标准是 RDoc 生成显示 Example::Socket < Socket 的文档,且不会发生 stack overflow。

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

描述

Related to #1776 because both ultimately create a self-referencing RDoc superclass. This is an independent C-parser reproducer with one non-symlinked file; source-path deduplication does not fix it.

Summary

RDoc's C parser creates a self-referencing superclass and crashes with
SystemStackError: stack level too deep when a C extension reuses a variable
as both the superclass argument and the assignment target of
rb_define_class_under.

Confirmed against both kgio 1.1.0 and 2.7.0, but the problem can be reproduced
with one C file.

Minimal reproduction

Create example.c:

#include <ruby.h>

void Init_example(void)
{
  VALUE mExample = rb_define_module("Example");
  VALUE cSocket = rb_const_get(rb_cObject, rb_intern("Socket"));
  cSocket = rb_define_class_under(mExample, "Socket", cSocket);
}

Run:

$ rdoc --version
8.0.0

$ rdoc --format=ri example.c
Parsing sources...
100% [ 1/ 1]  example.c
uh-oh! RDoc had a problem:
stack level too deep

run with --debug for full backtrace

This results in:

$ rdoc --debug --format=ri --force-output --output=doc example.c
Parsing sources...
100% [ 1/ 1]  example.c
stack level too deep
/path/to/rdoc/lib/rdoc/code_object/class_module.rb:780:in 'RDoc::ClassModule#superclass'
	/path/to/rdoc/lib/rdoc/code_object/normal_class.rb:13:in 'RDoc::NormalClass#ancestors'
	/path/to/rdoc/lib/rdoc/code_object/normal_class.rb:18:in 'RDoc::NormalClass#ancestors'
	... same NormalClass#ancestors frame repeated 10,073 more times ...
	/path/to/rdoc/lib/rdoc/store.rb:522:in 'block in RDoc::Store#complete'
	/path/to/rdoc/lib/rdoc/store.rb:522:in 'Array#each'
	/path/to/rdoc/lib/rdoc/store.rb:522:in 'RDoc::Store#complete'
	/path/to/rdoc/lib/rdoc/rdoc.rb:529:in 'RDoc::RDoc#document'
	/path/to/rdoc/exe/rdoc:20:in '<main>'

This is reproduced with rdoc 8.0, RDoc 7.2.0, master. Not an issue for YARD.

Expected behavior

RDoc should generate documentation for:

Example::Socket < Socket

It should not crash.

Why the C code is valid

Before rb_define_class_under is called, cSocket points to the top-level
::Socket class. The right-hand side is evaluated before the assignment, so
the new Example::Socket class correctly receives ::Socket as its
superclass. The returned class is then assigned back to cSocket.

This is the pattern used by kgio, for example:

cSocket = rb_const_get(rb_cObject, rb_intern("Socket"));
cSocket = rb_define_class_under(mKgio, "Socket", cSocket);

Probable cause

RDoc::Parser::C#handle_class_module does not resolve the preceding
rb_const_get, so it initially records the superclass as the C variable name
"cSocket".

After creating Example::Socket, the parser registers:

cSocket -> Example::Socket

RDoc::Store#resolve_c_superclasses later resolves the superclass variable
"cSocket" through that mapping and assigns Example::Socket as its own
superclass.

RDoc::NormalClass#ancestors then recursively calls
superclass.ancestors without encountering a terminating superclass.

At minimum, resolve_c_superclasses should avoid assigning a class as its own
superclass. Ideally, the C parser should recognize the preceding
rb_const_get and preserve Socket as the superclass before replacing the
variable mapping.

主要语言
Ruby
星标
930
派生
465
平均合并
3 天 10 小时
30 天内合并 PR
27

贡献指南

打开贡献指南

从这里开始

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

ruby/rdoc 的其他 Issue

查看 ruby/rdoc 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

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