提交 8fbfdf37 编写于 作者: G Gonzalo Paniagua Javier

Fix CryptoStream.Dispose

	-Dispose will call TransformFinalBlock() for Read streams. Fixes bug #644648.
	-Dispose () will call Dispose (bool). Fixes bug #644654.
	-Stream.Close calls GC.SuppressFinalize(). Fixes bug #644660.
上级 28e9fa14
......@@ -105,6 +105,7 @@ namespace System.IO
public virtual void Close ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
[ComVisible (false)]
......
......@@ -109,19 +109,13 @@ namespace System.Security.Cryptography {
public void Clear ()
{
Dispose (true);
GC.SuppressFinalize (this); // not called in Stream.Dispose
Close ();
}
// LAMESPEC: A CryptoStream can be close in read mode
public override void Close ()
{
// only flush in write mode (bugzilla 46143)
if ((!_flushedFinalBlock) && (_mode == CryptoStreamMode.Write))
FlushFinalBlock ();
if (_stream != null)
_stream.Close ();
base.Close ();
}
public override int Read ([In,Out] byte[] buffer, int offset, int count)
......@@ -353,6 +347,21 @@ namespace System.Security.Cryptography {
protected override void Dispose (bool disposing)
{
if (!_disposed) {
if (disposing) {
// only flush in write mode (bugzilla 46143)
if (!_flushedFinalBlock) {
if (_mode == CryptoStreamMode.Write) {
FlushFinalBlock ();
} else {
// See bug #644648
_transform.TransformFinalBlock (new byte [0], 0, 0);
}
_flushedFinalBlock = true;
}
if (_stream != null)
_stream.Close ();
}
_disposed = true;
// always cleared for security reason
if (_workingBlock != null)
......
......@@ -1389,6 +1389,46 @@ namespace MonoTests.System.Security.Cryptography {
#endif
}
[Test]
public void ReadModeDispose_FinalBlock ()
{
using (SHA1 sha1 = SHA1.Create()) {
using (MemoryStream mem = new MemoryStream(new byte[] { 1, 2, 3 }, false))
using (CryptoStream cs = new CryptoStream(mem, sha1, CryptoStreamMode.Read))
{
}
byte b = sha1.Hash [0]; // This will throw if TransformFinalBlock not called in sha1
GC.KeepAlive (b); // just the warning...
}
}
[Test]
public void CustomDisposeCalled ()
{
using (MemoryStream mem = new MemoryStream(new byte[] { 1, 2, 3 }, false)) {
MyCryptoStream cs;
using (cs = new MyCryptoStream (mem, SHA1.Create()))
{
}
Assert.IsTrue (cs.DisposeCalled, "#1");
}
}
class MyCryptoStream : CryptoStream {
public bool DisposeCalled { get; private set;}
public MyCryptoStream(Stream stream, ICryptoTransform transform)
: base(stream, transform, CryptoStreamMode.Read)
{
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
DisposeCalled = true;
}
}
class ExpandTransform : ICryptoTransform {
public bool CanReuseTransform {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册