From ff35a829b527c05db354c40875439a454d3239b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sat, 1 Jan 2022 18:23:52 +0100 Subject: [PATCH] fix: check for null after popping the string enumerator level --- .../PacketConditionalIndexAttributeGenerator.cs | 5 +++++ .../PacketContextListAttributeGenerator.cs | 5 +++++ .../PacketGreedyIndexAttributeGenerator.cs | 5 +++++ .../PacketIndexAttributeGenerator.cs | 5 +++++ .../PacketListIndexAttributeGenerator.cs | 5 +++++ .../ConverterDeserializationGenerator.cs | 14 ++++++++++++++ .../PacketConverterGenerator.cs | 5 ----- 7 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketConditionalIndexAttributeGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketConditionalIndexAttributeGenerator.cs index a83eba2317e52d8cc1538cd97bd81a97bc3a05b8..20985488437e6e63e7346b70e96941ccce57ac35 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketConditionalIndexAttributeGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketConditionalIndexAttributeGenerator.cs @@ -288,6 +288,11 @@ public class PacketConditionalIndexAttributeGenerator : IParameterGenerator generator.PopLevel(); } + if (!packetInfo.Parameters.IsLast) + { + generator.ValidateNotLast(parameter.Name); + } + // end is last token if body if (parameter.IsOptional()) { diff --git a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketContextListAttributeGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketContextListAttributeGenerator.cs index 9e99a4c2850c7e7240f9ba311892006541cc2bdf..4eda16dded4e1ecdff72f4e28532e024e4c8ce9f 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketContextListAttributeGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketContextListAttributeGenerator.cs @@ -125,6 +125,11 @@ public class PacketContextListAttributeGenerator : IParameterGenerator generator.AssignLocalVariable(parameter); + if (!packetInfo.Parameters.IsLast) + { + generator.ValidateNotLast(parameter.Name); + } + // end is last token if body if (parameter.IsOptional()) { diff --git a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketGreedyIndexAttributeGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketGreedyIndexAttributeGenerator.cs index e9593e016303eab37580e8e174279305cbe34d1b..3ff00b3f981cf20285526b8b8bb2e804cd0075ab 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketGreedyIndexAttributeGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketGreedyIndexAttributeGenerator.cs @@ -97,6 +97,11 @@ public class PacketGreedyIndexAttributeGenerator : IParameterGenerator generator.PopLevel(); } + if (!packetInfo.Parameters.IsLast) + { + generator.ValidateNotLast(parameter.Name); + } + // end is last token if body if (parameter.IsOptional()) { diff --git a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketIndexAttributeGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketIndexAttributeGenerator.cs index 004eb3ff8a19823634d958f3431fe6d527aeeab8..71d434dbfa0d33c5edbab7ba8e2cb4e8fdc835d0 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketIndexAttributeGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketIndexAttributeGenerator.cs @@ -138,6 +138,11 @@ public class PacketIndexAttributeGenerator : IParameterGenerator generator.PopLevel(); } + if (!packetInfo.Parameters.IsLast) + { + generator.ValidateNotLast(parameter.Name); + } + // end is last token if body if (parameter.IsOptional()) { diff --git a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketListIndexAttributeGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketListIndexAttributeGenerator.cs index cc84e4746845bf3e47339eb7d79979873d0a7de9..21c7e7817be4c709b22bb5fa3a31b26261b764e4 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketListIndexAttributeGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketListIndexAttributeGenerator.cs @@ -129,6 +129,11 @@ public class PacketListIndexAttributeGenerator : IParameterGenerator generator.AssignLocalVariable(parameter, false); + if (!packetInfo.Parameters.IsLast) + { + generator.ValidateNotLast(parameter.Name); + } + // end is last token if body if (parameter.IsOptional()) { diff --git a/Core/NosSmooth.PacketSerializersGenerator/ConverterDeserializationGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/ConverterDeserializationGenerator.cs index 47ca5560cff928d80497bb477fb4a4cc1cd10c91..d44f713b8d348b1cf525b8c8b4f34c618fc4401b 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/ConverterDeserializationGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/ConverterDeserializationGenerator.cs @@ -164,4 +164,18 @@ if ({nullableVariableName} is null) {{ _textWriter.Indent--; _textWriter.WriteLine("}"); } + + /// + /// Validates that the string enumerator is currently not at the last token. + /// + /// The parameter that is being converted. + public void ValidateNotLast(string parameterName) + { + _textWriter.WriteLine($"if ({_stringEnumeratorVariable}.IsOnLastToken() ?? false)"); + _textWriter.WriteLine("{"); + _textWriter.Indent++; + _textWriter.WriteLine($"return new PacketEndNotExpectedError(this, \"{parameterName}\");"); + _textWriter.Indent--; + _textWriter.WriteLine("}"); + } } \ No newline at end of file diff --git a/Core/NosSmooth.PacketSerializersGenerator/PacketConverterGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/PacketConverterGenerator.cs index 16d4f98d8c11962ff3cf09a765ecfa7f30af7729..4e8ee20f605c359a012c26caa8fcf6a874f15bed 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/PacketConverterGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/PacketConverterGenerator.cs @@ -120,11 +120,6 @@ private IResultError? CheckDeserializationResult(Result result, string pro return new PacketParameterSerializerError(this, property, result); }} - if (!last && (stringEnumerator.IsOnLastToken() ?? false)) - {{ - return new PacketEndNotExpectedError(this, property); - }} - return null; }} }}"