Skip to content

Commit 1af3296

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

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

Luaon.NET/Linq/LTableStore.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,23 @@ public LField FieldFromName(int name, LValue nameRef, bool allowsCreation)
9595
}
9696

9797
// Get the positional field
98-
int positionalIndex = 0;
98+
var positionalIndex = 0;
9999
foreach (var i in Items)
100100
{
101101
if (i.Name != null) continue;
102102
positionalIndex++;
103103
if (name == positionalIndex) return i;
104104
}
105-
#if NETSTANDARD2_0
106-
Debug.Fail("Should not execute to here.");
107-
#else
108-
Debug.Assert(false);
109-
#endif
105+
106+
// Create the positional field
107+
if (allowsCreation)
108+
{
109+
// Use implicit index if possible.
110+
var f = name == positionalIndex + 1 ? new LField(LValue.Nil) : new LField(name, LValue.Nil);
111+
this.Add(f);
112+
return f;
113+
}
114+
110115
return null;
111116
}
112117

@@ -119,7 +124,7 @@ public LField FieldFromName(LValue name, bool allowsCreation)
119124
{
120125
if (!allowsCreation) return null;
121126
field = new LField(name, LValue.Nil);
122-
Add(field);
127+
this.Add(field);
123128
}
124129
return field;
125130
}

0 commit comments

Comments
 (0)