Skip to content
Browse files

Ensure unsafe memory allocation is marked as checked

  • Loading branch information...
1 parent bbdfb22 commit 8280b2485411496b4540fd66c32474e93d684aaa @DamianEdwards DamianEdwards committed Jan 11, 2013
Showing with 31 additions and 28 deletions.
  1. +31 −28 src/Microsoft.AspNet.SignalR.Core/Messaging/Cursor.cs
View
59 src/Microsoft.AspNet.SignalR.Core/Messaging/Cursor.cs
@@ -52,41 +52,44 @@ private static string MakeCursorSlow(IList<Cursor> cursors)
private static string MakeCursorFast(IList<Cursor> cursors)
{
- const int MAX_CHARS = 8 * 1024;
- char* pChars = stackalloc char[MAX_CHARS];
- char* pNextChar = pChars;
- int numCharsInBuffer = 0;
-
- // Start shoving data into the buffer
- for (int i = 0; i < cursors.Count; i++)
+ checked
{
- Cursor cursor = cursors[i];
- string escapedKey = cursor._escapedKey;
+ const int MAX_CHARS = 8 * 1024;
+ char* pChars = stackalloc char[MAX_CHARS];
+ char* pNextChar = pChars;
+ int numCharsInBuffer = 0;
- // comma + up to 16-char hex Id + pipe
- numCharsInBuffer += escapedKey.Length + 18;
-
- if (numCharsInBuffer > MAX_CHARS)
+ // Start shoving data into the buffer
+ for (int i = 0; i < cursors.Count; i++)
{
- return null; // we will overrun the buffer
- }
+ Cursor cursor = cursors[i];
+ string escapedKey = cursor._escapedKey;
- for (int j = 0; j < escapedKey.Length; j++)
- {
- *pNextChar++ = escapedKey[j];
- }
+ // comma + up to 16-char hex Id + pipe
+ numCharsInBuffer += escapedKey.Length + 18;
- *pNextChar++ = ',';
- int hexLength = WriteUlongAsHexToBuffer(cursor.Id, pNextChar);
+ if (numCharsInBuffer > MAX_CHARS)
+ {
+ return null; // we will overrun the buffer
+ }
- // Since we reserved 16 chars for the hex value, update numCharsInBuffer to reflect the actual number of
- // characters written by WriteUlongAsHexToBuffer.
- numCharsInBuffer += hexLength - 16;
- pNextChar += hexLength;
- *pNextChar++ = '|';
- }
+ for (int j = 0; j < escapedKey.Length; j++)
+ {
+ *pNextChar++ = escapedKey[j];
+ }
+
+ *pNextChar++ = ',';
+ int hexLength = WriteUlongAsHexToBuffer(cursor.Id, pNextChar);
- return (numCharsInBuffer == 0) ? String.Empty : new String(pChars, 0, numCharsInBuffer - 1); // -1 for final pipe
+ // Since we reserved 16 chars for the hex value, update numCharsInBuffer to reflect the actual number of
+ // characters written by WriteUlongAsHexToBuffer.
+ numCharsInBuffer += hexLength - 16;
+ pNextChar += hexLength;
+ *pNextChar++ = '|';
+ }
+
+ return (numCharsInBuffer == 0) ? String.Empty : new String(pChars, 0, numCharsInBuffer - 1); // -1 for final pipe
+ }
}
private static int WriteUlongAsHexToBuffer(ulong value, char* pBuffer)

0 comments on commit 8280b24

Please sign in to comment.
Something went wrong with that request. Please try again.