Skip to content

Fix TypeError in CheckStringCoercion typeName() for null-prototype objects in DEV - #37900

Open
prasad2512 wants to merge 1 commit into
react:mainfrom
prasad2512:fix-check-string-coercion-type-name-37881
Open

prasad2512 wants to merge 1 commit into
react:mainfrom
prasad2512:fix-check-string-coercion-type-name-37881

Conversation

@prasad2512

Copy link
Copy Markdown

Summary

Fixes #37881

In development mode (__DEV__), typeName(value) in packages/shared/CheckStringCoercion.js constructs a human-readable diagnostic type string when a prop, key, attribute, or style value throws during string coercion ('' + value).

Currently, typeName(value) accesses (value as any).constructor.name:

const type =
  (hasToStringTag && (value as any)[Symbol.toStringTag]) ||
  (value as any).constructor.name ||
  'Object';

When value is created without a prototype (e.g. Object.create(null) or { __proto__: null }), (value as any).constructor is undefined. Accessing .name on undefined causes an unhandled runtime error:

TypeError: Cannot read properties of undefined (reading 'name')
    at typeName (packages/shared/CheckStringCoercion.js)

This crashes React's internal DEV helper before React can log the diagnostic error message and troubleshooting recommendations.

This PR safely checks for constructor existence before accessing .name:

const constructor = (value as any).constructor;
const type =
  (hasToStringTag && (value as any)[Symbol.toStringTag]) ||
  (constructor && constructor.name) ||
  'Object';

When constructor is undefined or null, it gracefully falls back to 'Object'.

How did you test this change?

  • Added a regression test in packages/react-dom/src/__tests__/ReactIdentity-test.js verifying that passing a null-prototype object with a throwing valueOf does not crash typeName(), properly prints the diagnostic error message (The provided key is an unsupported type Object. This value must be coerced to a string before using it here.), and throws the expected coercion exception.

@meta-cla

meta-cla Bot commented Oct 19, 2026

Copy link
Copy Markdown

Hi @prasad2512!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

A size report will appear here when the build finishes.

Generated by sizebot against c55c1b5

@prasad2512
prasad2512 force-pushed the fix-check-string-coercion-type-name-37881 branch from d81879c to 4c7b4a2 Compare October 19, 2026 17:52
@meta-cla meta-cla Bot added the CLA Signed label Oct 21, 2026
@meta-cla

meta-cla Bot commented Oct 21, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@prasad2512
prasad2512 force-pushed the fix-check-string-coercion-type-name-37881 branch from 4c7b4a2 to c55c1b5 Compare October 28, 2026 02:09
@prasad2512

Copy link
Copy Markdown
Author

Hi @eps1lon, rebased onto latest main with CLA verified. Includes regression test in ReactIdentity-test for null-prototype objects in typeName(). Ready for review when convenient. Thanks!

@prasad2512
prasad2512 requested a review from willbyers1 October 28, 2026 02:11

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: TypeError in CheckStringCoercion typeName() for null-prototype objects in DEV

2 participants