Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OverflowException in LoggerMessageGenerator.GetNonRandomizedHashCode #110698

Closed
KalleOlaviNiemitalo opened this issue Dec 13, 2024 · 2 comments · Fixed by #111073
Closed

OverflowException in LoggerMessageGenerator.GetNonRandomizedHashCode #110698

KalleOlaviNiemitalo opened this issue Dec 13, 2024 · 2 comments · Fixed by #111073
Labels
area-Extensions-Logging in-pr There is an active PR which will close this issue when it is merged source-generator Indicates an issue with a source generator feature
Milestone

Comments

@KalleOlaviNiemitalo
Copy link

KalleOlaviNiemitalo commented Dec 13, 2024

Description

The source generator in Microsoft.Extensions.Logging.Abstractions throws OverflowException when it attempts to generate an EventId for some logger method names. Pronounceable names that trigger this error include Dzoggee, Juzzwenn, Bzuengdom, Covieeszu, Ennudkilw, Fawiinska, Haikshyke, Hyklyytja, Kirraphas, Nuthvausu, Orluavjee, Piaanwatz, Ponkpauij, Ruakszyrn, and Sazkwinuo.

Reproduction Steps

EventIdHashOverflow.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
  </ItemGroup>

</Project>

LoggerExtensions.cs:

using Microsoft.Extensions.Logging;

namespace EventIdHashOverflow
{
    internal static partial class LoggerExtensions
    {
        [LoggerMessage(Level = LogLevel.Information)]
        public static partial void Dzoggee(this ILogger logger);
    }
}

Attempt to build this project.

Expected behavior

The build should succeed.

Actual behavior

CSC : warning CS8785: Generator 'LoggerMessageGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'OverflowException' with message 'Negating the minimum value of a twos complement number is invalid.'.

LoggerExtensions.cs(8,36,8,43): error CS8795: Partial method 'LoggerExtensions.Dzoggee(ILogger)' must have an implementation part because it has accessibility modifiers.

Regression?

No response

Known Workarounds

Specify LoggerMessageAttribute.EventId instead of letting it be generated automatically.

Configuration

.NET SDK 9.0.101

Other information

OverflowException is thrown by the Math.Abs(Int32.MinValue) call here:

/// <summary>
/// Returns a non-randomized hash code for the given string.
/// We always return a positive value.
/// </summary>
internal static int GetNonRandomizedHashCode(string s)
{
uint result = 2166136261u;
foreach (char c in s)
{
result = (c ^ result) * 16777619;
}
return Math.Abs((int)result);
}

There is similar code in other source generators too:

Making GetNonRandomizedHashCode return 0 in this case would make every nonnegative int return value correspond to two values of uint result.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 13, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-logging
See info in area-owners.md if you want to be subscribed.

@tarekgh tarekgh added this to the 10.0.0 milestone Dec 13, 2024
@tarekgh tarekgh added source-generator Indicates an issue with a source generator feature and removed untriaged New issue has not been triaged by the area owner labels Dec 13, 2024
@KalleOlaviNiemitalo KalleOlaviNiemitalo changed the title OverflowException in LoggerMessageGenerator OverflowException in LoggerMessageGenerator.GetNonRandomizedHashCode Dec 14, 2024
@KalleOlaviNiemitalo
Copy link
Author

The workaround with LoggerMessageAttribute.EventId does not apply to the source generator in Microsoft.Extensions.Options. However, it looks like that one does not use the faulty GetNonRandomizedHashCode method when the language is C# 11 or higher; it uses file-local types instead. Upgrading the language version may then be a workaround there, but C# 7.3 is the highest version supported on .NET Framework.

@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Extensions-Logging in-pr There is an active PR which will close this issue when it is merged source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants