-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Java: exclude writeReplace-defining classes from Serializable check
#18415
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
Merged
smowton
merged 4 commits into
github:main
from
smowton:smowton/feature/exclude-writereplace-from-serializable-checks
Jan 7, 2025
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
java/ql/src/change-notes/2025-01-06-write-replace-serializable.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: fix | ||
| --- | ||
| * Classes that define a `writeReplace` method are no longer flagged by the `java/missing-no-arg-constructor-on-serializable` query on the assumption they are unlikely to be deserialized using the default algorithm. |
1 change: 1 addition & 0 deletions
1
...ests/MissingVoidConstructorsOnSerializable/MissingVoidConstructorsOnSerializable.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| | Test.java:12:7:12:7 | A | This class is serializable, but its non-serializable super-class $@ does not declare a no-argument constructor. | Test.java:4:7:4:20 | NonSerialzable | NonSerialzable | |
1 change: 1 addition & 0 deletions
1
...y-tests/MissingVoidConstructorsOnSerializable/MissingVoidConstructorsOnSerializable.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Likely Bugs/Serialization/MissingVoidConstructorsOnSerializable.ql |
24 changes: 24 additions & 0 deletions
24
java/ql/test/query-tests/MissingVoidConstructorsOnSerializable/Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import java.io.ObjectStreamException; | ||
| import java.io.Serializable; | ||
|
|
||
| class NonSerialzable { | ||
|
|
||
| // Has no default constructor | ||
| public NonSerialzable(int x) { } | ||
|
|
||
| } | ||
|
|
||
| // BAD: Serializable but its parent cannot be instantiated | ||
| class A extends NonSerialzable implements Serializable { | ||
| public A() { super(1); } | ||
| } | ||
|
|
||
| // GOOD: writeReplaces itself, so unlikely to be deserialized | ||
| // according to default rules. | ||
| class B extends NonSerialzable implements Serializable { | ||
| public B() { super(2); } | ||
|
|
||
| public Object writeReplace() throws ObjectStreamException { | ||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.