Skip to content

Commit 71c44d4

Browse files
java: Fix incremental mac finalization. Again.
1 parent 2f6e1cc commit 71c44d4

File tree

4 files changed

+10
-23
lines changed

4 files changed

+10
-23
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
v0.76.2
22

33
- Java: Fix lifetime management for bridged objects used with async functions; previously there was a window where they could get prematurely deallocated, leading to undefined behavior.
4+
5+
- Java: Simplify and fix the finalization of incremental mac streams.

java/shared/java/org/signal/libsignal/internal/NativeHandleGuard.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ protected void finalize() {
8888
}
8989
}
9090

91+
// A note on synchronization.
92+
//
93+
// close is synchronized to eliminate the race between it and finalize.
94+
//
95+
// All in the name of calling release exactly once.
9196
public abstract static class CloseableOwner extends SimpleOwner implements AutoCloseable {
9297
private boolean isClosed = false;
9398

@@ -96,7 +101,7 @@ protected CloseableOwner(long nativeHandle) {
96101
}
97102

98103
@Override
99-
public void close() {
104+
public synchronized void close() {
100105
if (isClosed) {
101106
return;
102107
}

java/shared/java/org/signal/libsignal/protocol/incrementalmac/IncrementalMacInputStream.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public int read(byte[] bytes, int offset, int length) throws IOException {
9494
}
9595

9696
@Override
97-
public synchronized void close() throws IOException {
97+
public void close() throws IOException {
9898
if (this.closed) {
9999
return;
100100
}
@@ -103,16 +103,6 @@ public synchronized void close() throws IOException {
103103
this.handleOwner.close();
104104
}
105105

106-
@Override
107-
@SuppressWarnings("deprecation")
108-
protected void finalize() throws Throwable {
109-
try {
110-
close();
111-
} finally {
112-
super.finalize();
113-
}
114-
}
115-
116106
// Read implementation for the READ_FROM_INPUT state.
117107
// In it, we are filling the currentChunk reading from the input channel.
118108
// Once there is a full chunk worth of data (or if we have reached the end of

java/shared/java/org/signal/libsignal/protocol/incrementalmac/IncrementalMacOutputStream.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void flush() throws IOException {
6565
}
6666

6767
@Override
68-
public synchronized void close() throws IOException {
68+
public void close() throws IOException {
6969
if (this.closed) {
7070
return;
7171
}
@@ -82,14 +82,4 @@ public synchronized void close() throws IOException {
8282
// problems on Android
8383
this.digestStream.close();
8484
}
85-
86-
@Override
87-
@SuppressWarnings("deprecation")
88-
protected void finalize() throws Throwable {
89-
try {
90-
close();
91-
} finally {
92-
super.finalize();
93-
}
94-
}
9585
}

0 commit comments

Comments
 (0)