Skip to content

Commit 409382b

Browse files
overheadhunterAnthony Scarpino
authored and
Anthony Scarpino
committed
8280703: CipherCore.doFinal(...) causes potentially massive byte[] allocations during decryption
Reviewed-by: ascarpino
1 parent cb8a82e commit 409382b

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/java.base/share/classes/com/sun/crypto/provider/CipherCore.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -812,10 +812,13 @@ int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output,
812812
if (outputCapacity < estOutSize) {
813813
cipher.save();
814814
}
815-
// create temporary output buffer if the estimated size is larger
816-
// than the user-provided buffer.
817-
internalOutput = new byte[estOutSize];
818-
offset = 0;
815+
if (outputCapacity < estOutSize || padding != null) {
816+
// create temporary output buffer if the estimated size is larger
817+
// than the user-provided buffer or a padding needs to be removed
818+
// before copying the unpadded result to the output buffer
819+
internalOutput = new byte[estOutSize];
820+
offset = 0;
821+
}
819822
}
820823

821824
byte[] outBuffer = (internalOutput != null) ? internalOutput : output;

0 commit comments

Comments
 (0)