-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18415 from smowton/smowton/feature/exclude-writer…
…eplace-from-serializable-checks Java: exclude `writeReplace`-defining classes from `Serializable` check
- Loading branch information
Showing
5 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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:21 | NonSerializable | NonSerializable | |
1 change: 1 addition & 0 deletions
1
...y-tests/MissingVoidConstructorsOnSerializable/MissingVoidConstructorsOnSerializable.qlref
This file contains 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 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 NonSerializable { | ||
|
||
// Has no default constructor | ||
public NonSerializable(int x) { } | ||
|
||
} | ||
|
||
// BAD: Serializable but its parent cannot be instantiated | ||
class A extends NonSerializable implements Serializable { | ||
public A() { super(1); } | ||
} | ||
|
||
// GOOD: writeReplaces itself, so unlikely to be deserialized | ||
// according to default rules. | ||
class B extends NonSerializable implements Serializable { | ||
public B() { super(2); } | ||
|
||
public Object writeReplace() throws ObjectStreamException { | ||
return null; | ||
} | ||
} |