Skip to content

Commit 7c48687

Browse files
Serialize addresses as strings (#1692)
* Serialize addresses as strings * Update CHANGELOG.md
1 parent 0b1f8df commit 7c48687

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Improve timestamp precision of transactions and spans ([#1680](https://github.com/getsentry/sentry-dotnet/pull/1680))
88
- Flatten AggregateException ([#1672](https://github.com/getsentry/sentry-dotnet/pull/1672))
99
- NOTE: This can affect grouping. You can keep the original behavior by setting the option `KeepAggregateException` to `true`.
10+
- Serialize stack frame addresses as strings. ([#1692](https://github.com/getsentry/sentry-dotnet/pull/1692))
1011

1112
### Features
1213

src/Sentry/Exceptions/SentryStackFrame.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger)
161161
writer.WriteBooleanIfNotNull("in_app", InApp);
162162
writer.WriteStringIfNotWhiteSpace("package", Package);
163163
writer.WriteStringIfNotWhiteSpace("platform", Platform);
164-
writer.WriteNumberIfNotNull("image_addr", ImageAddress.NullIfDefault());
165-
writer.WriteNumberIfNotNull("symbol_addr", SymbolAddress);
164+
writer.WriteStringIfNotWhiteSpace("image_addr", ImageAddress.NullIfDefault()?.ToHexString());
165+
writer.WriteStringIfNotWhiteSpace("symbol_addr", SymbolAddress?.ToHexString());
166166
writer.WriteStringIfNotWhiteSpace("instruction_addr", InstructionAddress);
167167
writer.WriteNumberIfNotNull("instruction_offset", InstructionOffset);
168168
writer.WriteStringIfNotWhiteSpace("addr_mode", AddressMode);
@@ -212,8 +212,8 @@ public static SentryStackFrame FromJson(JsonElement json)
212212
var inApp = json.GetPropertyOrNull("in_app")?.GetBoolean();
213213
var package = json.GetPropertyOrNull("package")?.GetString();
214214
var platform = json.GetPropertyOrNull("platform")?.GetString();
215-
var imageAddress = json.GetPropertyOrNull("image_addr")?.GetInt64() ?? 0;
216-
var symbolAddress = json.GetPropertyOrNull("symbol_addr")?.GetInt64();
215+
var imageAddress = json.GetPropertyOrNull("image_addr")?.GetAddressAsLong() ?? 0;
216+
var symbolAddress = json.GetPropertyOrNull("symbol_addr")?.GetAddressAsLong();
217217
var instructionAddress = json.GetPropertyOrNull("instruction_addr")?.GetString();
218218
var instructionOffset = json.GetPropertyOrNull("instruction_offset")?.GetInt64();
219219
var addressMode = json.GetPropertyOrNull("addr_mode")?.GetString();

src/Sentry/Internal/Extensions/JsonExtensions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,36 @@ public static void Deconstruct(this JsonProperty jsonProperty, out string name,
104104
return double.Parse(json.ToString()!);
105105
}
106106

107+
public static long? GetAddressAsLong(this JsonElement json)
108+
{
109+
// If the address is in json as a number, we can just use it.
110+
if (json.ValueKind == JsonValueKind.Number)
111+
{
112+
return json.GetInt64();
113+
}
114+
115+
// Otherwise it will be a string, but we need to convert it to a number.
116+
var s = json.GetString();
117+
if (s == null)
118+
{
119+
return null;
120+
}
121+
122+
// It should be in hex format, such as "0x7fff5bf346c0"
123+
#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER
124+
var substring = s[2..];
125+
#else
126+
var substring = s.Substring(2);
127+
#endif
128+
if (s.StartsWith("0x") &&
129+
long.TryParse(substring, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var result))
130+
{
131+
return result;
132+
}
133+
134+
throw new FormatException();
135+
}
136+
107137
public static string GetStringOrThrow(this JsonElement json) =>
108138
json.GetString() ?? throw new InvalidOperationException("JSON string is null.");
109139

src/Sentry/Internal/Extensions/MiscExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34

45
namespace Sentry.Internal.Extensions
56
{
@@ -11,5 +12,8 @@ internal static class MiscExtensions
1112
!EqualityComparer<T>.Default.Equals(value, default)
1213
? value
1314
: null;
15+
16+
public static string ToHexString(this long l) =>
17+
"0x" + l.ToString("x", CultureInfo.InvariantCulture);
1418
}
1519
}

test/Sentry.Tests/Protocol/Exceptions/SentryStackFrameTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public void SerializeObject_AllPropertiesSetToNonDefault_SerializesValidObject()
4646
"\"in_app\":true," +
4747
"\"package\":\"Package\"," +
4848
"\"platform\":\"Platform\"," +
49-
"\"image_addr\":3," +
50-
"\"symbol_addr\":4," +
49+
"\"image_addr\":\"0x3\"," +
50+
"\"symbol_addr\":\"0x4\"," +
5151
"\"instruction_addr\":\"0xffffffff\"," +
5252
"\"instruction_offset\":5," +
5353
"\"addr_mode\":\"rel:0\"" +

0 commit comments

Comments
 (0)