Skip to content

Commit c7d48f3

Browse files
committed
Fix possible resource leak in serialization.
1 parent e7ca6a0 commit c7d48f3

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/main/java/com/orangefunction/tomcat/redissessions/JavaSerializer.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ public byte[] attributesHashFrom(RedisSession session) throws IOException {
3030
attributes.put(key, session.getAttribute(key));
3131
}
3232

33-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
34-
try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos))) {
33+
byte[] serialized = null;
34+
35+
try (
36+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
37+
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos));
38+
) {
3539
oos.writeUnshared(attributes);
40+
41+
serialized = bos.toByteArray();
3642
}
3743

3844
MessageDigest digester = null;
@@ -41,18 +47,24 @@ public byte[] attributesHashFrom(RedisSession session) throws IOException {
4147
} catch (NoSuchAlgorithmException e) {
4248
log.error("Unable to get MessageDigest instance for MD5");
4349
}
44-
return digester.digest(bos.toByteArray());
50+
return digester.digest(serialized);
4551
}
4652

4753
@Override
4854
public byte[] serializeFrom(RedisSession session, SessionSerializationMetadata metadata) throws IOException {
49-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
50-
try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos))) {
55+
byte[] serialized = null;
56+
57+
try (
58+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
59+
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos));
60+
) {
5161
oos.writeObject(metadata);
5262
session.writeObjectData(oos);
63+
64+
serialized = bos.toByteArray();
5365
}
5466

55-
return bos.toByteArray();
67+
return serialized;
5668
}
5769

5870
@Override

0 commit comments

Comments
 (0)