Skip to content

Commit f4e6f6e

Browse files
rkyser-avasurerstam
authored andcommitted
CSHARP-3297: Handle incomplete regex patterns in JsonScanner
1 parent 666fee4 commit f4e6f6e

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/MongoDB.Bson/IO/JsonScanner.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ private static JsonToken GetRegularExpressionToken(JsonBuffer buffer)
369369
{
370370
case '/': state = RegularExpressionState.InOptions; break;
371371
case '\\': state = RegularExpressionState.InEscapeSequence; break;
372+
case -1: state = RegularExpressionState.Invalid; break;
372373
default: state = RegularExpressionState.InPattern; break;
373374
}
374375
break;

tests/MongoDB.Bson.Tests/IO/JsonScannerTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using MongoDB.Bson.IO;
1718
using Xunit;
1819

@@ -405,6 +406,20 @@ public void TestMinusZeroExponentMinusTwelve()
405406
Assert.Equal(',', buffer.Read());
406407
}
407408

409+
[Theory]
410+
[InlineData("/")]
411+
[InlineData("/pattern")]
412+
[InlineData("/pattern\\/")]
413+
public void TestMissingClosingSlash(string jsonRegex)
414+
{
415+
var buffer = new JsonBuffer(jsonRegex);
416+
417+
var exception = Record.Exception(() => JsonScanner.GetNextToken(buffer));
418+
419+
Assert.IsType<FormatException>(exception);
420+
Assert.StartsWith("Invalid JSON regular expression", exception.Message);
421+
}
422+
408423
[Fact]
409424
public void TestRegularExpressionEmpty()
410425
{

0 commit comments

Comments
 (0)