Skip to content

Commit

Permalink
fix: vsnprintf usage on macOS (#3079)
Browse files Browse the repository at this point in the history
* fix: vsnprintf usage on macOS

* chore: update changelog

* workaround code trimming issues
  • Loading branch information
vaind authored Jan 24, 2024
1 parent 947dac4 commit cabcf44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ If you have conflicts, you can opt-out by adding the following to your `csproj`:
- Moved the binding to MAUI events for breadcrumb creation from `WillFinishLaunching` to `FinishedLaunching`. This delays the initial instantiation of `app`. ([#3057](https://github.com/getsentry/sentry-dotnet/pull/3057))
- The SDK no longer adds the `WinUIUnhandledExceptionIntegration` on non Windows platforms ([#3055](https://github.com/getsentry/sentry-dotnet/pull/3055))
- The scope transaction is now correctly set for Otel transactions ([#3072](https://github.com/getsentry/sentry-dotnet/pull/3072))
- Native integration logging on macOS ([#3079](https://github.com/getsentry/sentry-dotnet/pull/3079))

### Dependencies

Expand All @@ -35,7 +36,7 @@ If you have conflicts, you can opt-out by adding the following to your `csproj`:

### Significant change in behavior

- Transactions' spans are no longer automatically finished with status `deadline_exceeded` by the transaction. This is now handled by the [Relay](https://github.com/getsentry/relay).
- Transactions' spans are no longer automatically finished with status `deadline_exceeded` by the transaction. This is now handled by the [Relay](https://github.com/getsentry/relay).
- Customers self hosting Sentry must use verion 22.12.0 or later ([#3013](https://github.com/getsentry/sentry-dotnet/pull/3013))

### API breaking Changes
Expand Down
26 changes: 13 additions & 13 deletions src/Sentry/Platforms/Native/CFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ internal static void SetValueIfNotNull(sentry_value_t obj, string key, double? v

public static bool Init(SentryOptions options)
{
_isWindows = System.OperatingSystem.IsWindows();
var cOptions = sentry_options_new();

// Note: DSN is not null because options.IsValid() must have returned true for this to be called.
Expand Down Expand Up @@ -123,7 +124,6 @@ public static bool Init(SentryOptions options)
sentry_options_set_database_path(cOptions, dir);
}

_isLinux = System.OperatingSystem.IsLinux();
if (options.DiagnosticLogger is null)
{
_logger?.LogDebug("Unsetting the current native logger");
Expand Down Expand Up @@ -363,7 +363,7 @@ internal struct sentry_value_t

// The logger we should forward native messages to. This is referenced by nativeLog() which in turn for.
private static IDiagnosticLogger? _logger;
private static bool _isLinux = false;
private static bool _isWindows = false;

// This method is called from the C library and forwards incoming messages to the currently set _logger.
// [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })] // error CS3016: Arrays as attribute arguments is not CLS-complian
Expand Down Expand Up @@ -408,9 +408,18 @@ private static void nativeLogImpl(int cLevel, IntPtr format, IntPtr args, IntPtr
try
{
// We cannot access C var-arg (va_list) in c# thus we pass it back to vsnprintf to do the formatting.
// For Linux, we must make a copy of the VaList to be able to pass it back...
if (_isLinux)
if (_isWindows)
{
var formattedLength = 1 + vsnprintf_windows(IntPtr.Zero, UIntPtr.Zero, format, args);
WithAllocatedPtr(formattedLength, buffer =>
{
vsnprintf_windows(buffer, (UIntPtr)formattedLength, format, args);
message = Marshal.PtrToStringAnsi(buffer);
});
}
else
{
// For Linux/macOS, we must make a copy of the VaList to be able to pass it back...
var argsStruct = Marshal.PtrToStructure<VaListLinux64>(args);
var formattedLength = 0;
WithMarshalledStruct(argsStruct, argsPtr =>
Expand All @@ -424,15 +433,6 @@ private static void nativeLogImpl(int cLevel, IntPtr format, IntPtr args, IntPtr
message = Marshal.PtrToStringAnsi(buffer);
}));
}
else
{
var formattedLength = 1 + vsnprintf_windows(IntPtr.Zero, UIntPtr.Zero, format, args);
WithAllocatedPtr(formattedLength, buffer =>
{
vsnprintf_windows(buffer, (UIntPtr)formattedLength, format, args);
message = Marshal.PtrToStringAnsi(buffer);
});
}
}
catch (Exception err)
{
Expand Down

0 comments on commit cabcf44

Please sign in to comment.