Skip to content

Lifted null comparison decompiles to HasValue/GetValueOrDefault with a '? true : false' #4136

Description

@siegfriedpammer

Input code

SixLabors.ImageSharp 4.1.1, SixLabors.ImageSharp.Formats.Png.PngEncoderCore.WriteXmpChunk (and
WriteExifChunk, same shape):

ilspycmd ~/.cache/nugetfuzz/sixlabors.imagesharp/4.1.1/lib/net8.0/SixLabors.ImageSharp.dll \
    -t SixLabors.ImageSharp.Formats.Png.PngEncoderCore

Erroneous output

Current master (dbf23c6):

byte[] data = meta.XmpProfile.Data;
int? num = data?.Length;
if ((num ?? 0) == 0)
{
    return;
}

With #4091 (6fcb387) the lifted comparison is expanded and wrapped in a no-op conditional:

byte[] data = meta.XmpProfile.Data;
int? num = data?.Length;
if ((!num.HasValue || num.GetValueOrDefault() == 0) ? true : false)
{
    return;
}

Two separate things happen here: the ?? form is lost, and the resulting bool expression gains a
? true : false.

Other instances from the same corpus run (top-200 nuget.org packages, #4091 against its merge base):

  • Swashbuckle.AspNetCore.SwaggerGen 10.2.3, XmlCommentsRequestBodyFilter:
    if ((num2 ?? 1) == 0 || num == 0) becomes
    if ((num2.HasValue && num2.GetValueOrDefault() == 0) || num == 0)
  • OpenTelemetry.Exporter.OpenTelemetryProtocol serializer:
    ((statusCode ?? StatusCode.Unset) != StatusCode.Unset) becomes
    (statusCode.HasValue && statusCode.GetValueOrDefault() != StatusCode.Unset)

Details

  • Product in use: ICSharpCode.Decompiler, built from source
  • Version in use: not reproducible on master (dbf23c6); measured at 6fcb387 against merge base
    712ad1a. Three types in the corpus are affected; the output remains correct.

Filed by an AI agent (Claude) on Siegfried's behalf.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DecompilerThe decompiler engine itselfOutputOutput code formatting; hyperlinks/tooltips on output code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions