News  [SoftwareSite

Latest News
Older News
RSS Feed
 
Complete Projects
Useful Classes
Top Downloads
Message Board
AllAPI.net
 
Send Comments
Software License
Mentalis.org Buttons
Donate
 
Forums -> Security Library Forum
 
SecureNetworkStream.BeginWrite/EndWrite race condition causing NullReferenceException (fix)  
by Martin Plante [plantem at xceedsoft dot com]
posted on 2005/01/06

I found a problem with SecureNetworkStream.BeginWrite. If the callback passed to "Socket.BeginSend" is called quickly, it may end-up calling SecureNetworkStream.EndWrite BEFORE the following line in BeginWrite gets called. This results in WriteResult being set back to null BEFORE "WriteResult.AsyncResult" is returned, which results in a NullReferenceException. The fix is to work with a local copy of WriteResult within BeginWrite.

Before:
WriteResult = new TransferItem(new byte[size], 0, size, new AsyncResult(callback, state, null), DataType.ApplicationData);
Array.Copy(buffer, offset, WriteResult.Buffer, 0, size);
try {
Socket.BeginSend(WriteResult.Buffer, 0, size, SocketFlags.None, new AsyncCallback(OnBytesSent), (int)0);
return WriteResult.AsyncResult;
} catch {
throw new IOException();
}

After:
TransferItem localResult = new TransferItem(new byte[size], 0, size, new AsyncResult(callback, state, null), DataType.ApplicationData);
WriteResult = localResult;
Array.Copy(buffer, offset, localResult.Buffer, 0, size);
try {
Socket.BeginSend(localResult.Buffer, 0, size, SocketFlags.None, new AsyncCallback(OnBytesSent), (int)0);
return localResult.AsyncResult;
} catch {
throw new IOException();
}

Makes sense?

by Pieter Philippaerts [Pieter at mentalis dot org]
posted on 2005/01/06

Thanks Martin! I've uploaded the corrected version.

by Priya [priya at pivotsolutions dot com]
posted on 2005/01/10

So do you think this exception can close the socket connection??

 

Copyright © 2002-2007, The Mentalis.org Team. All rights reserved.
This site is located at http://www.mentalis.org/
Send comments to the webmaster.