Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit ec28f37

Browse files
committed
Add tests for Guid parsing
1 parent b78f25d commit ec28f37

File tree

2 files changed

+301
-1
lines changed

2 files changed

+301
-1
lines changed

src/ServiceStack.Text/Support/StringSegmentExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,14 +627,20 @@ public static Guid ParseGuid(this StringSegment value)
627627
if (JsConfig.UseSystemParseMethods)
628628
return Guid.Parse(value.Value);
629629

630+
if (value.Buffer == null)
631+
throw new ArgumentNullException();
632+
633+
if (value.Length == 0)
634+
throw new FormatException(BadFormat);
635+
630636
//Guid can be in one of 3 forms:
631637
//1. General `{dddddddd-dddd-dddd-dddd-dddddddddddd}` or `(dddddddd-dddd-dddd-dddd-dddddddddddd)` 8-4-4-4-12 chars
632638
//2. Hex `{0xdddddddd,0xdddd,0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}` 8-4-4-8x2 chars
633639
//3. No style `dddddddddddddddddddddddddddddddd` 32 chars
634640

635641
int i = value.Offset;
636642
int end = value.Offset + value.Length;
637-
while (JsonUtils.IsWhiteSpace(value.Buffer[i]) && i < end) i++;
643+
while (i < end && JsonUtils.IsWhiteSpace(value.Buffer[i])) i++;
638644

639645
if (i == end)
640646
throw new FormatException(BadFormat);
@@ -671,6 +677,9 @@ private static Guid ParseGeneralStyleGuid(StringSegment value, out int len)
671677
n++;
672678
len += 2;
673679
hasParentesis = true;
680+
681+
if (buf[8 + n] != '-')
682+
throw new FormatException(BadFormat);
674683
}
675684

676685
if (buf[8 + n] == '-')
@@ -679,6 +688,7 @@ private static Guid ParseGeneralStyleGuid(StringSegment value, out int len)
679688
|| buf[18 + n] != '-'
680689
|| buf[23 + n] != '-')
681690
throw new FormatException(BadFormat);
691+
682692
len += 4;
683693
dash = 1;
684694
}

0 commit comments

Comments
 (0)