Skip to content

Commit 7c20e34

Browse files
authored
Fix a potential IndexOutOfRangeException in BufferUtils.GetStringPosition() (#1324)
when parameter "find" is an empty string. If we don't check for the length of parameter "find", then the first indexing of "findArray" raises an IndexOutOfRangeException.
1 parent 067d810 commit 7c20e34

File tree

2 files changed

+59
-34
lines changed

2 files changed

+59
-34
lines changed

src/sys/BufferUtils.cs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
1414
//-----------------------------------------------------------------------------
1515

16+
using System;
1617
using System.Text;
1718

1819
namespace SIPSorcery.Sys
@@ -30,50 +31,48 @@ public class BufferUtils
3031
/// <returns>The start position in the buffer of the requested string or -1 if not found.</returns>
3132
public static int GetStringPosition(byte[] buffer, int startPosition, int endPosition, string find, string end)
3233
{
33-
if (buffer == null || buffer.Length == 0 || find == null)
34+
if (buffer == null || buffer.Length == 0 || find == null || find.Length == 0)
3435
{
3536
return -1;
3637
}
37-
else
38-
{
39-
byte[] findArray = Encoding.UTF8.GetBytes(find);
40-
byte[] endArray = (end != null) ? Encoding.UTF8.GetBytes(end) : null;
4138

42-
int findPosn = 0;
43-
int endPosn = 0;
39+
byte[] findArray = Encoding.UTF8.GetBytes(find);
40+
byte[] endArray = (end != null) ? Encoding.UTF8.GetBytes(end) : null;
4441

45-
for (int index = startPosition; index < endPosition && index < buffer.Length; index++)
46-
{
47-
if (buffer[index] == findArray[findPosn])
48-
{
49-
findPosn++;
50-
}
51-
else
52-
{
53-
findPosn = 0;
54-
}
42+
int findPosn = 0;
43+
int endPosn = 0;
5544

56-
if (endArray != null && buffer[index] == endArray[endPosn])
57-
{
58-
endPosn++;
59-
}
60-
else
61-
{
62-
endPosn = 0;
63-
}
45+
for (int index = startPosition; index < endPosition && index < buffer.Length; index++)
46+
{
47+
if (buffer[index] == findArray[findPosn])
48+
{
49+
findPosn++;
50+
}
51+
else
52+
{
53+
findPosn = 0;
54+
}
6455

65-
if (findPosn == findArray.Length)
66-
{
67-
return index - findArray.Length + 1;
68-
}
69-
else if (endArray != null && endPosn == endArray.Length)
70-
{
71-
return -1;
72-
}
56+
if (endArray != null && buffer[index] == endArray[endPosn])
57+
{
58+
endPosn++;
59+
}
60+
else
61+
{
62+
endPosn = 0;
7363
}
7464

75-
return -1;
65+
if (findPosn == findArray.Length)
66+
{
67+
return index - findArray.Length + 1;
68+
}
69+
else if (endArray != null && endPosn == endArray.Length)
70+
{
71+
return -1;
72+
}
7673
}
74+
75+
return -1;
7776
}
7877

7978
public static bool HasString(byte[] buffer, int startPosition, int endPosition, string find, string end)

test/unit/sys/BufferUtilsUnitTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,32 @@ public void NotBeforeEndUnitTest()
5252
Assert.True(!hasFox, "The string was not found in the buffer.");
5353
}
5454

55+
[Fact]
56+
public void GetStringPositionWithEmptyFindUnitTest()
57+
{
58+
logger.LogDebug("--> {MethodName}", System.Reflection.MethodBase.GetCurrentMethod().Name);
59+
logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);
60+
61+
string sipMsg =
62+
"REGISTER sip:Blue Face SIP/2.0\r\n" +
63+
"Via: SIP/2.0/UDP 127.0.0.1:1720;branch=z9hG4bKlgnUQcaywCOaPcXR\r\n" +
64+
"Max-Forwards: 70\r\n" +
65+
"User-Agent: PA168S\r\n" +
66+
"From: \"user\" <sip:user@Blue Face>;tag=81swjAV7dHG1yjd5\r\n" +
67+
"To: \"user\" <sip:user@Blue Face>\r\n" +
68+
"Call-ID: [email protected]\r\n" +
69+
"CSeq: 15754 REGISTER\r\n" +
70+
"Contact: <sip:[email protected]:1720>\r\n" +
71+
"Expires: 30\r\n" +
72+
"Content-Length: 0\r\n\r\n";
73+
74+
byte[] sample = Encoding.ASCII.GetBytes(sipMsg);
75+
76+
int index = BufferUtils.GetStringPosition(sample, 0, Int32.MaxValue, string.Empty, null);
77+
78+
Assert.Equal(-1, index);
79+
}
80+
5581
[Fact]
5682
public void GetStringIndexUnitTest()
5783
{

0 commit comments

Comments
 (0)