Skip to content

Commit 9bf9358

Browse files
CopilotPranavSenthilnathaneiriktsarpalis
authored
Fix buffer slicing in (U)Int128 property name serialization (#116868)
* Initial plan for issue * Fix buffer slicing in (U)Int128 property name serialization Co-authored-by: PranavSenthilnathan <12225508+PranavSenthilnathan@users.noreply.github.com> * Add regression tests for Int128/UInt128 property name serialization Co-authored-by: PranavSenthilnathan <12225508+PranavSenthilnathan@users.noreply.github.com> * Address PR feedback: simplify test assertions and remove junk data checks Co-authored-by: PranavSenthilnathan <12225508+PranavSenthilnathan@users.noreply.github.com> * Add JSON content validation to Int128/UInt128 dictionary tests Co-authored-by: PranavSenthilnathan <12225508+PranavSenthilnathan@users.noreply.github.com> * Update src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Value.WriteTests.cs * Replace multiple assertions with single E2E validation in Int128 dictionary test Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com> * Address PR feedback: remove implementation details and add JSON content validation Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com> * Replace Assert.Contains with Assert.Equal for E2E JSON validation in Int128/UInt128 tests Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com> * Restore multi-key dictionaries in Int128/UInt128 tests as requested Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com> * Use raw string literals for JSON assertions in Int128/UInt128 tests Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com> * Remove superficial comments from Int128/UInt128 dictionary tests Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PranavSenthilnathan <12225508+PranavSenthilnathan@users.noreply.github.com> Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com> Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
1 parent 32d3dc7 commit 9bf9358

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int128Converter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, Int128 val
7373
{
7474
Span<byte> buffer = stackalloc byte[MaxFormatLength];
7575
Format(buffer, value, out int written);
76-
writer.WritePropertyName(buffer);
76+
writer.WritePropertyName(buffer.Slice(0, written));
7777
}
7878

7979
internal override Int128 ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options)

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt128Converter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, UInt128 va
7373
{
7474
Span<byte> buffer = stackalloc byte[MaxFormatLength];
7575
Format(buffer, value, out int written);
76-
writer.WritePropertyName(buffer);
76+
writer.WritePropertyName(buffer.Slice(0, written));
7777
}
7878

7979
internal override UInt128 ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options)

src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Value.WriteTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,44 @@ public static void TimeOnly_Write_Success(string value, string? expectedValue =
187187
string json = JsonSerializer.Serialize(ts);
188188
Assert.Equal($"\"{expectedValue ?? value}\"", json);
189189
}
190+
191+
[Fact]
192+
public static void Int128_AsDictionaryKey_SerializesCorrectly()
193+
{
194+
// Regression test for https://github.com/dotnet/runtime/issues/116855
195+
var dict = new Dictionary<Int128, string>
196+
{
197+
[0] = "Zero",
198+
[1] = "One",
199+
[-1] = "MinusOne",
200+
[Int128.MaxValue] = "Max",
201+
[Int128.MinValue] = "Min"
202+
};
203+
204+
string json = JsonSerializer.Serialize(dict);
205+
Assert.Equal("""{"0":"Zero","1":"One","-1":"MinusOne","170141183460469231731687303715884105727":"Max","-170141183460469231731687303715884105728":"Min"}""", json);
206+
207+
var deserialized = JsonSerializer.Deserialize<Dictionary<Int128, string>>(json);
208+
Assert.Equal(dict, deserialized);
209+
}
210+
211+
[Fact]
212+
public static void UInt128_AsDictionaryKey_SerializesCorrectly()
213+
{
214+
// Regression test for https://github.com/dotnet/runtime/issues/116855
215+
var dict = new Dictionary<UInt128, string>
216+
{
217+
[0] = "Zero",
218+
[1] = "One",
219+
[UInt128.MaxValue] = "Max"
220+
};
221+
222+
string json = JsonSerializer.Serialize(dict);
223+
Assert.Equal("""{"0":"Zero","1":"One","340282366920938463463374607431768211455":"Max"}""", json);
224+
225+
var deserialized = JsonSerializer.Deserialize<Dictionary<UInt128, string>>(json);
226+
Assert.Equal(dict, deserialized);
227+
}
190228
#endif
191229
}
192230
}

0 commit comments

Comments
 (0)