Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: missing else statement in ReactDOMServerFormatConfig.js #22309

Closed
justingrant opened this issue Sep 14, 2021 · 16 comments
Closed

Bug: missing else statement in ReactDOMServerFormatConfig.js #22309

justingrant opened this issue Sep 14, 2021 · 16 comments
Labels
Resolution: Stale Automatically closed due to inactivity Status: New

Comments

@justingrant
Copy link
Contributor

justingrant commented Sep 14, 2021

I found this bug while working on an unrelated PR #22064. Looks like there's a missing else, because the value set in line 978 is always overwritten in line 980. Given that children.toString() and children[0].toString() will return the same string for one-element arrays, I assume the best (for perf and bundle size) fix would be to remove the assignment inside the if block.

if (isArray(children)) {
invariant(
children.length <= 1,
'<textarea> can only have at most one child.',
);
value = '' + children[0];
}
value = '' + children;

Because the observable behavior is identical whether or not children is a scalar or a one-element array, I'm not sure a test could be written to verify a fix.

EDIT: I updated the text above after I learned that Array.prototype.toString() acts the same as Array.prototype.join(). JavaScript teaches me something new every day!

@justingrant justingrant added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Sep 14, 2021
@akgupta0777
Copy link
Contributor

@justingrant It is using an invariant in the if check. Invariant is an error handling guard which lets you use assertions in your code.
so invariant requires two arguments first one is the condition and second is error message to be thrown after condition gets violated.

In above case, As soon as length of children is more than 1 then it throws an error saying '<textarea> can only have at most one child.' and stops the execution right away and prevent overwriting of value.

@justingrant
Copy link
Contributor Author

In above case, As soon as length of children is more than 1 then it throws an error saying '<textarea> can only have at most one child.' and stops the execution right away and prevent overwriting of value.

I was actually thinking about the 1-element-in-array case. I'd assumed that coercing an Array to a string would yield something like '[object Array]'. But just now I learned that Array overrides toString and its implementation is equivalent to .join()! After 20 years of using JS, every time I think I've learned enough, I'm reminded that there are still surprises lurking.

So there's still a bug here, but it's a much minor one than I originally thought. The actual bug is that value = '' + children[0]; is unnecessary code and can be removed. If there's one element in the array, execution will fall through to line 980 and the original value will be overwritten with the same exact string.

@akgupta0777
Copy link
Contributor

Seriously JavaScript sometimes screw minds really well. Even today I also got to know that concatenation of an array with string results in a string with all array elements joined separated by ",".
Well yes in that case it is unnecessary to use line 978.

I will love to contribute on this issue and fix this bug.

@rickhanlonii
Copy link
Member

rickhanlonii commented Sep 15, 2021

Was added here, and does look like a mistake. cc @sebmarkbage

@rickhanlonii rickhanlonii added Status: New and removed Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug labels Sep 15, 2021
@sebmarkbage
Copy link
Collaborator

Yea, feel free to fix it to an else. Either solution works and might deopt differently but given that this whole branch warns anyway it doesn't matter.

@Hyperion101010
Copy link

Picking this up

@justingrant
Copy link
Contributor Author

justingrant commented Sep 24, 2021 via email

@amensum
Copy link

amensum commented Sep 24, 2021

Cool. I'd suggest removing the first assignment instead of adding an else. Save a few bytes of bundle size. Unless there's some perf reason not to do this.

Check #22409 pls

@Hyperion101010
Copy link

Check this @justingrant. I don't know why codesandbox fails is there any way I can get circle-ci report? Here #22431

@justingrant
Copy link
Contributor Author

Check this @justingrant. I don't know why codesandbox fails is there any way I can get circle-ci report? Here #22431

Probably a transient build issue. I made a suggestion over at #22431, and if you commit a change then CI should rebuild and hopefully it will work this time.

@amensum
Copy link

amensum commented Sep 26, 2021

@justingrant
I added the suggested comment to the code #22409

@Hyperion101010
Copy link

I added comments to the changes and re-uploaded them. Let's see how it rebuilds @justingrant

@amensum
Copy link

amensum commented Sep 27, 2021

@Hyperion101010

What's wrong with my PR? Why did you make a similar PR with identical code? If there is something wrong with my PR, I can make changes.

@Hyperion101010
Copy link

This issue hasn't been assigned to anyone so I think anyone is free to open PR for it @amensum

@stale
Copy link

stale bot commented Jan 8, 2022

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Jan 8, 2022
Copy link

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Stale Automatically closed due to inactivity Status: New
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants