Skip to content

Commit 56bbc2c

Browse files
committed
Bug fix: NRE when writing non-existent keyed value to LTable.
1 parent e792251 commit 56bbc2c

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

Luaon.NET/Linq/LTableStore.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,13 @@ public LField FieldFromName(LValue name, bool allowsCreation)
115115
Debug.Assert(name != null && name.TokenType != LTokenType.Nil);
116116
if (name.TokenType == LTokenType.Integer) return FieldFromName((int)name, name, allowsCreation);
117117
// Named field
118-
if (fieldsDict.TryGetValue(name, out var field)) return field;
119-
return null;
118+
if (!fieldsDict.TryGetValue(name, out var field))
119+
{
120+
if (!allowsCreation) return null;
121+
field = new LField(name, LValue.Nil);
122+
Add(field);
123+
}
124+
return field;
120125
}
121126

122127
public bool RemoveByName(LValue name)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Luaon;
6+
using Luaon.Linq;
7+
using Xunit;
8+
9+
namespace XUnitTestProject1.Tests
10+
{
11+
public class RegressionTests
12+
{
13+
14+
/// <summary>
15+
/// UnexpectedCharacterException when parsing long Lua table (> LuaTableTextReader buffer size, i.e., 1024).
16+
/// </summary>
17+
[Fact]
18+
public void Issue1()
19+
{
20+
var longString1 = string.Join("", Enumerable.Repeat("0123456789ABCDEF", 64)); // 16 x 64 = 1024
21+
var longString2 = longString1 + "||" + longString1;
22+
var bigTable = new LTable
23+
{
24+
["Field1"] = longString1,
25+
["Field2"] = 12345,
26+
["Field2"] = longString2,
27+
};
28+
var serialized = bigTable.ToString(Formatting.Prettified);
29+
Assert.Contains(longString1, serialized);
30+
Assert.Contains(longString2, serialized);
31+
var bigTable2 = LToken.Parse(serialized);
32+
Assert.Equal(bigTable, bigTable2);
33+
}
34+
35+
}
36+
}

0 commit comments

Comments
 (0)