Skip to content

Commit 018283f

Browse files
committed
Fixed bugs that caused failures when handling malformed data under certain circumstances.
Extended the high-performance zero-allocation (stack-only) model to include .NET Standard 2.0.
1 parent a5f21e9 commit 018283f

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

Solidsoft.Reply.Parsers.Gs1Ai/EntityResolver.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3711,7 +3711,15 @@ private static void ResolveEntity(
37113711
break;
37123712
}
37133713

3714-
extractedValue.TrimEnd('\0').CopyTo(value);
3714+
// value is sized appropraietly for GS1 elemnts, but it is possible for malformed data to be
3715+
// longer, so in this case we
3716+
var extractdValueLength = extractedValue.Length;
3717+
if (extractdValueLength > value.Length) {
3718+
extractedValue.Slice(0, value.Length).TrimEnd('\0').CopyTo(value);
3719+
}
3720+
else {
3721+
extractedValue.TrimEnd('\0').CopyTo(value);
3722+
}
37153723

37163724
parameters[0] = (int)entity;
37173725
parameters[1] = numberOfDecimalPlaces;

Solidsoft.Reply.Parsers.Gs1Ai/Extensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,11 @@ internal static bool Gs1CheckCharactersPairIsValid(this ReadOnlySpan<char> key)
816816
var checkCharacterRefValue = 0;
817817

818818
for (var idx = keyData.Length - 1; idx >= 0; idx--) {
819-
checkCharacterRefValue += Invariants.IndexOf(keyData[idx]) * PrimeNumbers[keyData.Length - idx - 1];
819+
var indexOfChar = Invariants.IndexOf(keyData[idx]);
820+
if (indexOfChar == -1) return false;
821+
var primeNumberIndex = keyData.Length - idx - 1;
822+
if (primeNumberIndex >= PrimeNumbers.Length) return false;
823+
checkCharacterRefValue += indexOfChar * PrimeNumbers[primeNumberIndex];
820824
}
821825

822826
checkCharacterRefValue %= 1021;

Solidsoft.Reply.Parsers.Gs1Ai/Parser.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,11 @@ private static void DoParse(
413413
{
414414
processResolvedEntity?.Invoke(
415415
#if NET6_0_OR_GREATER
416-
new string(workingBuffer.ToArray()).Resolve(workingBuffer[..2].ToString(), position));
416+
new string(workingBuffer.ToArray())
417+
.Resolve(workingBuffer[..(workingBuffer.Length >= 2 ? 2 : workingBuffer.Length)].ToString(), position));
417418
#else
418-
new string(workingBuffer.ToArray()).Resolve(workingBuffer.Slice(0, 2).ToString(), position));
419+
new string(workingBuffer.ToArray())
420+
.Resolve(workingBuffer.Slice(0, workingBuffer.Length >= 2 ? 2 : workingBuffer.Length).ToString(), position));
419421
#endif
420422
}
421423

Solidsoft.Reply.Parsers.Gs1Ai/Solidsoft.Reply.Parsers.Gs1Ai.csproj

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
<PackageReleaseNotes>
2121
The following changes were made in this release.
2222

23-
1) Fixed bug when parsing malformed GS1 data.
24-
25-
2) Fixed a bug that caused a failure looking up AIs in a dictionary under certain circumstances.
23+
1) Fixed bugs that caused failures when handling malformed data under certain circumstances.
2624

27-
3) Introduced a high-performance zero-allocation (stack-only) model. This change introduces a new ParseEx method and an associated Delegate that receives a ref struct. These new features support the highest performance and are neccessary to acheive zero-allocation when parsing valid data. NB. memory will be allocated if the parser detects any errors.
25+
2) Extended the high-performance zero-allocation (stack-only) model to include .NET Standard 2.0.
2826
</PackageReleaseNotes>
2927
<Description>
3028
A comprehensive validating parser for GS1 Application Identifiers (AIs).
@@ -33,7 +31,7 @@
3331
--------------
3432
$(PackageReleaseNotes)
3533
</Description>
36-
<Version>1.1.0</Version>
34+
<Version>1.1.1</Version>
3735
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
3836
<RootNamespace>$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
3937
<NeutralLanguage>en</NeutralLanguage>

0 commit comments

Comments
 (0)