Visitar URL original
Python: Fix `deepcopy` mutable default FP by tausbn · Pull Request #15397 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20240125083354/https://github.com/github/codeql/pull/15397
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

Python: Fix deepcopy mutable default FP #15397

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

tausbn
Copy link
Contributor

@tausbn tausbn commented Jan 22, 2024 •

Fixes #15317, #3462.
(Also related: #3502)

There are two issues with deepcopy here. Firstly, the deepcopy function itself has a mutable default value in its parameter _nil (set to the empty list by default).

Now, this value is never actually returned from deepcopy, as it is only used as a sentinel, but our analysis is not clever enough to see this. Thus, it thinks that this mutable default is returned, and hence the result of any call to deepcopy is a potential source.

To remedy this, I opted to simply exclude all sources that originate from within the standard library. It is very unlikely for any of the sources in the standard library to be legit.

Secondly, deepcopy — by virtue of being a function that we model as preserving values — admits data-flow through its calls, but this is not correct for the mutable default query, as it is here the identity of the default value in question that is important. Thus, we get spurious flow through deepcopy for this specific query.

To fix this, I added the target of any copy step as a barrier. It seems reasonable to me to generalise this to cover all copy steps, as these (presumably) do not preserve object identity.

Reviewing commit-by-commit is recommended.


I think this change is minor enough that no change note is required, but do let me know if you think otherwise!

Moves the existing tests into the `ModificationOfParameterWithDefault` subdirectory
which already contained a bunch more tests. In the process, I also removed some
duplicated test cases.
There are two issues with `deepcopy` here. Firstly, the `deepcopy` function itself
has a mutable default value in its parameter `_nil` (set to the empty list by default).

Now, this value is never actually returned from `deepcopy`, as it is only used as a
sentinel, but our analysis is not clever enough to see this. Thus, it thinks that this
mutable default is returned, and hence the result of any call to `deepcopy` is a
potential source.

To remedy this, I opted to simply exclude all sources that originate from within the
standard library. It is very unlikely for any of the sources in the standard library
to be legit.

Secondly, `deepcopy` -- by virtue of being a function that we model as preserving
values -- admits data-flow through its calls, but this is not correct for the mutable
default query, as it is here the _identity_ of the default value in question that is
important. Thus, we get spurious flow through `deepcopy` for this specific query.
Or, more generally, any copy step, as these presumably do not preserve
object identity.

(Arguably, `copy` could still be susceptible to interior mutability, but
I think that's outside the scope of this query anyway.)
@tausbn tausbn marked this pull request as ready for review January 22, 2024 16:58
@tausbn tausbn requested a review from a team as a code owner January 22, 2024 16:58
@tausbn tausbn added the no-change-note-required This PR does not need a change note label Jan 22, 2024
yoff
yoff previously approved these changes Jan 23, 2024
Copy link
Contributor

@yoff yoff left a comment

Choose a reason for hiding this comment

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

LGTM, I think the decisions made here are sound 👍

Comment on lines 39 to 40


Copy link
Contributor

Choose a reason for hiding this comment

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

You might consider removing these lines..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes. I left them in so as to not have to change a bunch of .expected files (since they all refer to functions_test.py), but I guess now that we're done with the review, I can clean the file up a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive: Modification of parameter with default for Python copy.deepcopy
2 participants