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

fix: don't mutate data in fromData, clone instead #602

Merged
merged 1 commit into from
Apr 3, 2022

Conversation

agilgur5
Copy link
Contributor

@agilgur5 agilgur5 commented Mar 1, 2022

Summary

always clone the input data in fromData so that the input is not mutated

Description

  • in the clear case, fromData would set this._data = pointGroups,
    which made the pointer the same as the one for the parameter

    • as this._data is internally mutated, this would cause the input
      parameter to also be mutated as a side-effect
  • this input mutation side-effect is likely unintended, so patched it so
    that the data is cloned in all cases, not only in the append case

    • in the clear case, this._data is set to an empty array by
      this.clear(), so this is equivalent to [].concat(pointGroups),
      which will result in them all being cloned

Misc Notes

Fixes agilgur5/react-signature-canvas#56
Credits to @Zwyx for discovering this in the above issue

Can see that issue for some manual testing I did around this cloning as well as how I found the bug here

- in the clear case, `fromData` would set `this._data = pointGroups`,
  which made the pointer the same as the one for the parameter
  - as `this._data` is internally mutated, this would cause the input
    parameter to also be mutated as a side-effect

- this input mutation side-effect is likely unintended, so patched it so
  that the data is cloned in _all_ cases, not only in the append case
  - in the clear case, `this._data` is set to an empty array by
    `this.clear()`, so this is equivalent to `[].concat(pointGroups)`,
    which will result in them all being cloned
Copy link
Collaborator

@UziTech UziTech left a comment

Choose a reason for hiding this comment

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

LGTM

@agilgur5
Copy link
Contributor Author

agilgur5 commented Apr 3, 2022

@UziTech just following up here as you approved this a month ago but never merged

@UziTech
Copy link
Collaborator

UziTech commented Apr 3, 2022

Sorry about that. This totally slipped through the cracks. Thanks for pinging me on this!

@UziTech UziTech merged commit e5057c5 into szimek:master Apr 3, 2022
github-actions bot pushed a commit that referenced this pull request Apr 3, 2022
## [4.0.4](v4.0.3...v4.0.4) (2022-04-03)

### Bug Fixes

* clone data in fromData ([#602](#602)) ([e5057c5](e5057c5))
@github-actions
Copy link

github-actions bot commented Apr 3, 2022

🎉 This PR is included in version 4.0.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Data given to fromData is mutated by the component
2 participants