Skip to content

Commit 38b63c7

Browse files
committed
fix: improve error handling
1 parent eb1781d commit 38b63c7

2 files changed

Lines changed: 27 additions & 37 deletions

File tree

StructuredFieldValues.Tests/Rfc8941ParserTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ public void ParseStringFailsCorrectly(string data, int index, int lastIndex)
189189
Assert.Equal(lastIndex, index);
190190
}
191191

192+
[Theory]
193+
[InlineData("", 0, 0)]
194+
[InlineData("%", 0, 1)]
195+
[InlineData("%\"", 0, 2)]
196+
public void ParseDisplayStringFailsCorrectly(string data, int index, int lastIndex)
197+
{
198+
Assert.NotNull(Rfc8941Parser.ParseDisplayString(data, ref index, out _));
199+
Assert.Equal(lastIndex, index);
200+
}
201+
192202
[Theory]
193203
[InlineData("key1", 0, "key1", 4)]
194204
[InlineData("keyZ1", 0, "key", 3)]

StructuredFieldValues/Rfc8941Parser.cs

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,8 @@ internal static class Rfc8941Parser
313313
{
314314
CheckIndex(index);
315315
var spanLength = source.Length;
316-
if (spanLength - index < 1)
317-
{
318-
result = default;
319-
return new(index, "insufficient characters for inner list");
320-
}
321-
322-
if (source[index] != '(')
316+
if (spanLength - index < 1
317+
|| source[index] != '(')
323318
{
324319
result = default;
325320
return new(index, "missing opening parentheses");
@@ -769,16 +764,11 @@ internal static class Rfc8941Parser
769764
{
770765
CheckIndex(index);
771766
var spanLength = source.Length;
772-
if (spanLength - index < 1)
767+
if (spanLength - index < 1
768+
|| source[index] != '"')
773769
{
774770
result = "";
775-
return new(index, "insufficient characters for string");
776-
}
777-
778-
if (source[index] != '"')
779-
{
780-
result = "";
781-
return new(index, "missing opening double quote");
771+
return new(index, "missing opening double quote for string");
782772
}
783773

784774
++index;
@@ -896,33 +886,28 @@ internal static class Rfc8941Parser
896886
{
897887
CheckIndex(index);
898888
var spanLength = source.Length;
899-
if (spanLength - index < 1)
889+
if (spanLength - index < 1
890+
|| source[index] != '%')
900891
{
901892
result = DisplayString.Empty;
902-
return new(index, "insufficient characters for display string");
903-
}
904-
905-
var character = source[index];
906-
if (character != '%')
907-
{
908-
result = DisplayString.Empty;
909-
return new(index, "invalid leading display string character");
893+
return new(index, "missing leading display string character");
910894
}
911895

912896
++index;
913897

914-
if (source[index] != '"')
898+
if (spanLength - index < 1
899+
|| source[index] != '"')
915900
{
916901
result = DisplayString.Empty;
917-
return new(index, "missing opening double quote");
902+
return new(index, "missing opening double quote for display string");
918903
}
919904

920905
++index;
921906

922907
if (spanLength - index < 1)
923908
{
924909
result = DisplayString.Empty;
925-
return new(index, "insufficient characters for display string value");
910+
return new(index, "missing display string value");
926911
}
927912

928913
var initialIndex = index;
@@ -933,7 +918,7 @@ internal static class Rfc8941Parser
933918
{
934919
while (localIndex != spanLength)
935920
{
936-
character = source[localIndex];
921+
var character = source[localIndex];
937922
switch (character)
938923
{
939924
case '"':
@@ -1092,24 +1077,19 @@ internal static class Rfc8941Parser
10921077
{
10931078
CheckIndex(index);
10941079
var spanLength = source.Length;
1095-
if (spanLength - index < 2)
1096-
{
1097-
result = Array.Empty<byte>();
1098-
return new(index, "insufficient characters for byte sequence");
1099-
}
1100-
1101-
if (source[index] != ':')
1080+
if (spanLength - index < 1
1081+
|| source[index] != ':')
11021082
{
11031083
result = Array.Empty<byte>();
1104-
return new(index, "invalid opening byte sequence character");
1084+
return new(index, "missing opening byte sequence character");
11051085
}
11061086

11071087
var initialIndex = ++index;
11081088
var slice = source.Slice(initialIndex);
11091089
var length = slice.IndexOf(':');
11101090
if (length < 0)
11111091
{
1112-
index += spanLength - 1;
1092+
index = spanLength;
11131093
result = Array.Empty<byte>();
11141094
return new(index, "missing closing byte sequence character");
11151095
}

0 commit comments

Comments
 (0)