Skip to content

Is DecodeManagedData supposed to return null? #152

@regalialong

Description

@regalialong

This actually tripped me up when trying to decode ETC_RGB4Crunched's cousin ETC2_RGBA8Crunched.

DecodeManagedData has the format switch to enter the code to actually decode the texture data for the format, but if your texture format isn't included, the size gets set to zero and then explicitly returned as null:

_ => 0
};
if (size == 0)
return null;
if (swizzler != null)
output = swizzler.PostprocessDeswizzle(output);
if (!useBgra)
TextureOperations.SwapRBComponents(output);
return output;
}

But the signature doesn't declare nullability anywhere:

public static byte[] DecodeManagedData(
byte[] data, TextureFormat format, int width, int height, bool useBgra = true, ISwizzler swizzler = null)
{

I did spot that the csproj disables nullability (old .NET compat?), so that's probably why this doesn't generate a lint when writing code like that normally would.

The problem is that this throws off Rider which doesn't warn callers about the possibility of a null return value, so when I try to write out the resulting data with an unsupported format, I try to write a null and crash my app.

I did spot that your code checks for null, so I assume it returning null is wanted.

Maybe add CanBeNullAttribute to suggest users to check for null even if nullability is disabled for your project?

You do also have the empty output Array at the top that you could return instead, then callers would need to check size instead (like your code already does), which at least wouldn't crash on access.

It's fine if you don't think this matters, might want to put this in xmldocs instead then, but it footgunned me the first time

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions