Skip to content
This repository
Browse code

Ensure unsafe memory allocation is marked as checked

  • Loading branch information...
commit 8280b2485411496b4540fd66c32474e93d684aaa 1 parent bbdfb22
Damian Edwards DamianEdwards authored

Showing 1 changed file with 31 additions and 28 deletions. Show diff stats Hide diff stats

  1. +31 28 src/Microsoft.AspNet.SignalR.Core/Messaging/Cursor.cs
59 src/Microsoft.AspNet.SignalR.Core/Messaging/Cursor.cs
@@ -52,41 +52,44 @@ private static string MakeCursorSlow(IList<Cursor> cursors)
52 52
53 53 private static string MakeCursorFast(IList<Cursor> cursors)
54 54 {
55   - const int MAX_CHARS = 8 * 1024;
56   - char* pChars = stackalloc char[MAX_CHARS];
57   - char* pNextChar = pChars;
58   - int numCharsInBuffer = 0;
59   -
60   - // Start shoving data into the buffer
61   - for (int i = 0; i < cursors.Count; i++)
  55 + checked
62 56 {
63   - Cursor cursor = cursors[i];
64   - string escapedKey = cursor._escapedKey;
  57 + const int MAX_CHARS = 8 * 1024;
  58 + char* pChars = stackalloc char[MAX_CHARS];
  59 + char* pNextChar = pChars;
  60 + int numCharsInBuffer = 0;
65 61
66   - // comma + up to 16-char hex Id + pipe
67   - numCharsInBuffer += escapedKey.Length + 18;
68   -
69   - if (numCharsInBuffer > MAX_CHARS)
  62 + // Start shoving data into the buffer
  63 + for (int i = 0; i < cursors.Count; i++)
70 64 {
71   - return null; // we will overrun the buffer
72   - }
  65 + Cursor cursor = cursors[i];
  66 + string escapedKey = cursor._escapedKey;
73 67
74   - for (int j = 0; j < escapedKey.Length; j++)
75   - {
76   - *pNextChar++ = escapedKey[j];
77   - }
  68 + // comma + up to 16-char hex Id + pipe
  69 + numCharsInBuffer += escapedKey.Length + 18;
78 70
79   - *pNextChar++ = ',';
80   - int hexLength = WriteUlongAsHexToBuffer(cursor.Id, pNextChar);
  71 + if (numCharsInBuffer > MAX_CHARS)
  72 + {
  73 + return null; // we will overrun the buffer
  74 + }
81 75
82   - // Since we reserved 16 chars for the hex value, update numCharsInBuffer to reflect the actual number of
83   - // characters written by WriteUlongAsHexToBuffer.
84   - numCharsInBuffer += hexLength - 16;
85   - pNextChar += hexLength;
86   - *pNextChar++ = '|';
87   - }
  76 + for (int j = 0; j < escapedKey.Length; j++)
  77 + {
  78 + *pNextChar++ = escapedKey[j];
  79 + }
  80 +
  81 + *pNextChar++ = ',';
  82 + int hexLength = WriteUlongAsHexToBuffer(cursor.Id, pNextChar);
88 83
89   - return (numCharsInBuffer == 0) ? String.Empty : new String(pChars, 0, numCharsInBuffer - 1); // -1 for final pipe
  84 + // Since we reserved 16 chars for the hex value, update numCharsInBuffer to reflect the actual number of
  85 + // characters written by WriteUlongAsHexToBuffer.
  86 + numCharsInBuffer += hexLength - 16;
  87 + pNextChar += hexLength;
  88 + *pNextChar++ = '|';
  89 + }
  90 +
  91 + return (numCharsInBuffer == 0) ? String.Empty : new String(pChars, 0, numCharsInBuffer - 1); // -1 for final pipe
  92 + }
90 93 }
91 94
92 95 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.