Skip to content

Conversation

@ivan-prodanov
Copy link

@ivan-prodanov ivan-prodanov commented Oct 27, 2025

Summary

Fix shims for .NET Standard 2.0: correct pointer-based Encoding overload usage and add guards for empty spans.

Problem

GetBytes/GetString used span pinning paths that misbehave on empty spans under .NET Standard 2.0. Problem occurs when logging empty string with exception

logger.Log(LogLevel.Error, new InvalidOperationException("WTF"), string.Empty);

The line above is logged in .NET 8 clients, but not in .net framework clients - the Shims throw IndexOutOfRange exception because the message is empty, but the shim attempts to get first character.

Fix

  • Added early-return guards for zero-length inputs.
  • Used fixed-pointer overloads to call Encoding.GetBytes/GetString safely.

Tests

  • Modified unit tests to run both under net framework and net core.
  • Added unit tests covering empty spans, non-empty spans, and boundary sizes.
  • All existing tests pass.

@ivan-prodanov ivan-prodanov changed the title fix netstandard shims Fix: .NET Standard 2.0 Shims unsafe span overloads Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant