Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Maple2.File.Parser/Maple2.File.Parser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageTags>MapleStory2, File, Parser, m2d, xml</PackageTags>
<!-- Use following lines to write the generated files to disk. -->
<EmitCompilerGeneratedFiles Condition=" '$(Configuration)' == 'Debug' ">true</EmitCompilerGeneratedFiles>
<PackageVersion>2.4.1</PackageVersion>
<PackageVersion>2.4.2</PackageVersion>
<TargetFramework>net8.0</TargetFramework>
<PackageReadmeFile>README.md</PackageReadmeFile>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
24 changes: 24 additions & 0 deletions Maple2.File.Parser/TableParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Maple2.File.Parser;

public class TableParser {
private readonly M2dReader xmlReader;
private readonly XmlSerializer featureSettingSerializer;
private readonly XmlSerializer featureSerializer;
private readonly XmlSerializer nameSerializer;
private readonly XmlSerializer bankTypeSerializer;
private readonly XmlSerializer chatStickerSerializer;
Expand Down Expand Up @@ -112,6 +114,8 @@ public class TableParser {

public TableParser(M2dReader xmlReader, string language) {
this.xmlReader = xmlReader;
featureSettingSerializer = new XmlSerializer(typeof(FeatureSetting));
featureSerializer = new XmlSerializer(typeof(FeatureRoot));
nameSerializer = new XmlSerializer(typeof(StringMapping));
bankTypeSerializer = new XmlSerializer(typeof(BankTypeRoot));
chatStickerSerializer = new XmlSerializer(typeof(ChatStickerRoot));
Expand Down Expand Up @@ -219,6 +223,26 @@ public TableParser(M2dReader xmlReader, string language) {
// };
}

public IEnumerable<(string Type, Setting Setting)> ParseFeatureSetting() {
XmlReader reader = xmlReader.GetXmlReader(xmlReader.GetEntry("table/feature_setting.xml"));
var data = featureSettingSerializer.Deserialize(reader) as FeatureSetting;
Debug.Assert(data != null);

foreach (Setting setting in data.setting) {
yield return (setting.type, setting);
}
Comment on lines +228 to +233
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Guard deserialization and collection nullability in release builds.

Debug.Assert on Line 229 and Line 239 won’t protect Release builds; data.setting (Line 231) and data.feature (Line 241) can still throw NullReferenceException.

🛠️ Proposed defensive fix
 public IEnumerable<(string Type, Setting Setting)> ParseFeatureSetting() {
     XmlReader reader = xmlReader.GetXmlReader(xmlReader.GetEntry("table/feature_setting.xml"));
-    var data = featureSettingSerializer.Deserialize(reader) as FeatureSetting;
-    Debug.Assert(data != null);
+    FeatureSetting data = featureSettingSerializer.Deserialize(reader) as FeatureSetting
+        ?? throw new InvalidDataException("Failed to deserialize table/feature_setting.xml");

-    foreach (Setting setting in data.setting) {
+    foreach (Setting setting in data.setting ?? Enumerable.Empty<Setting>()) {
         yield return (setting.type, setting);
     }
 }

 public IEnumerable<(string Name, Feature Feature)> ParseFeature() {
     XmlReader reader = xmlReader.GetXmlReader(xmlReader.GetEntry("table/feature.xml"));
-    var data = featureSerializer.Deserialize(reader) as FeatureRoot;
-    Debug.Assert(data != null);
+    FeatureRoot data = featureSerializer.Deserialize(reader) as FeatureRoot
+        ?? throw new InvalidDataException("Failed to deserialize table/feature.xml");

-    foreach (Feature feature in data.feature) {
+    foreach (Feature feature in data.feature ?? Enumerable.Empty<Feature>()) {
         yield return (feature.name, feature);
     }
 }

Also applies to: 238-243

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Maple2.File.Parser/TableParser.cs` around lines 228 - 233, After
deserializing with featureSettingSerializer.Deserialize into a FeatureSetting,
don't rely on Debug.Assert; explicitly check that 'data' is not null and that
'data.setting' and 'data.feature' are not null before iterating. In the code
paths around featureSettingSerializer.Deserialize / FeatureSetting, add null
checks (and either throw a clear exception such as
InvalidOperationException/InvalidDataException or return/yield break) or treat
the collections with a null-coalescing fallback (e.g., use
Enumerable.Empty<Setting>() / Enumerable.Empty<Feature>()) so iterating over
data.setting and data.feature cannot cause a NullReferenceException in Release
builds.

}

public IEnumerable<(string Name, Feature Feature)> ParseFeature() {
XmlReader reader = xmlReader.GetXmlReader(xmlReader.GetEntry("table/feature.xml"));
var data = featureSerializer.Deserialize(reader) as FeatureRoot;
Debug.Assert(data != null);

foreach (Feature feature in data.feature) {
yield return (feature.name, feature);
}
}

public IEnumerable<(int Id, BankType BankType)> ParseBankType() {
XmlReader reader = xmlReader.GetXmlReader(xmlReader.GetEntry("table/banktype.xml"));
var data = bankTypeSerializer.Deserialize(reader) as BankTypeRoot;
Expand Down
11 changes: 11 additions & 0 deletions Maple2.File.Tests/TableParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ public static void ClassInit(TestContext context) {
_parser = new TableParser(TestUtils.XmlReader, "en");
}

[TestMethod]
public void TestFeatureParser() {
foreach ((_, _) in _parser.ParseFeatureSetting()) {
continue;
}

foreach ((_, _) in _parser.ParseFeature()) {
continue;
}
}


[TestMethod]
public void TestColorPaletteParser() {
Expand Down
Loading