Skip to content

Return the class name atom from JS_GetClassName() - #1641

Merged
saghul merged 4 commits into
quickjs-ng:masterfrom
nordstjernen-web:fix-getclassname-atom
Aug 20, 2026
Merged

saghul merged 4 commits into
quickjs-ng:masterfrom
nordstjernen-web:fix-getclassname-atom

Conversation

@andreasrosdal

Copy link
Copy Markdown
Contributor

JS_GetClassName() duplicates and returns rt->class_array[class_id].class_id — the numeric JSClassID — where a JSAtom is expected.

JSAtom JS_GetClassName(JSRuntime *rt, JSClassID class_id)
{
    if (JS_IsRegisteredClass(rt, class_id)) {
        return JS_DupAtomRT(rt, rt->class_array[class_id].class_id);

The two fields are adjacent in JSClass and both 32-bit, so the mistake compiles cleanly, but the value handed to JS_DupAtomRT() is a class id reinterpreted as an atom: for a class id below JS_ATOM_END it bumps the refcount of an unrelated predefined atom and returns that, and above it indexes the runtime atom array with no relation to the class.

Return .class_name instead.

🤖 Generated with Claude Code

https://claude.ai/code/session_014mv33YvfHz7t9mmkituBnn


Generated by Claude Code

JS_GetClassName() duplicates and returns rt->class_array[class_id].class_id,
the numeric JSClassID, where a JSAtom is expected. The two fields are
adjacent in JSClass and both 32-bit, so the mistake compiles cleanly, but
the value handed to JS_DupAtomRT() is a class id reinterpreted as an atom:
for a class id below JS_ATOM_END it bumps the refcount of an unrelated
predefined atom and returns it, and above that it indexes into the runtime
atom array out of any relation to the class.

Return .class_name instead.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014mv33YvfHz7t9mmkituBnn
@saghul

saghul commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Can you please add a test for this? Looks like something we could test for in api-test.c

Cover the API in api-test.c: the name of a class registered from C, the
names of a spread of built-in classes reached through JS_GetClassID(), and
JS_ATOM_NULL for class ids with no class behind them.

Both name checks fail against the bug they guard against, where the class
id was returned in place of the class name atom.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QbX6zyXo3DgDtS3rRPCxBY
@andreasrosdal
andreasrosdal force-pushed the fix-getclassname-atom branch from 4bc3068 to 6dacaf5 Compare August 7, 2026 08:18
Returning the class id produced a valid-looking atom either way: an
unrelated predefined atom for a small id, or the id spelled out in decimal
for a large one. Rather than trusting a handful of named built-ins to cover
both shapes, walk every registered class id and check the name is neither
the id in decimal nor anything else starting with a digit. A few internal
classes are deliberately unnamed, so an empty name is allowed.

Also checks that two classes sharing a name share the interned atom, and
that the name does not depend on which context asks.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01K6eRbuuuCujKgQkrHgvMrc
@andreasrosdalw

Copy link
Copy Markdown
Contributor

Test added.

@saghul saghul left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please fix the conflicts?

@andreasrosdal

Copy link
Copy Markdown
Contributor Author

Conflicts fixed.

@saghul
saghul merged commit 94c303f into quickjs-ng:master Aug 20, 2026
128 checks passed
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
quickjs-ng 0.16.2

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed
* Fix JS_NewObjectFrom by @bnoordhuis in quickjs-ng/quickjs#1659
* Fix Proxy ownKeys trap accepting a primitive result by @saghul in quickjs-ng/quickjs#1637
* Fix arity of os.chdir and std.setenv by @bnoordhuis in quickjs-ng/quickjs#1662
* Fix object leak in JS_NewCConstructor by @bnoordhuis in quickjs-ng/quickjs#1670
* Fix SIGBUS on strict-alignment platforms: align cpool_offset to 8 bytes by @vom513 in quickjs-ng/quickjs#1666
* Fix TypedArray.prototype.with() value conversion order by @sr5434 in quickjs-ng/quickjs#1667
* Do not leak the old prototype in JS_AddIntrinsicDOMException() by @andreasrosdal in quickjs-ng/quickjs#1642
* Return the class name atom from JS_GetClassName() by @andreasrosdal in quickjs-ng/quickjs#1641
* Always call the sort comparator, even for identical values by @andreasrosdal in quickjs-ng/quickjs#1645
* Fix rejection of escaped reserved punctuators in v-flag \q by @sr5434 in quickjs-ng/quickjs#1671
* Add interrupt checks to Array prototype methods by @bnoordhuis in quickjs-ng/quickjs#1674
* Propagate exceptions from the get/set descriptor accessors by @saghul in quickjs-ng/quickjs#1638
* Check target extensibility via IsExtensible() in the proxy invariant checks by @saghul in quickjs-ng/quickjs#1636

## New Contributors
* @vom513 made their first contribution in quickjs-ng/quickjs#1666
* @sr5434 made their first contribution in quickjs-ng/quickjs#1667

**Full Changelog**: https://github.com/quickjs-ng/quickjs/compare/v0.16.1...v0.16.2</pre>
  <p>View the full release notes at <a href="https://github.com/quickjs-ng/quickjs/releases/tag/v0.16.2">https://github.com/quickjs-ng/quickjs/releases/tag/v0.16.2</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!17324
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants