-
Notifications
You must be signed in to change notification settings - Fork 117
Description
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:
AssetsTools.NET/AssetsTools.NET.Texture/TextureFile.cs
Lines 569 to 583 in d7c25c7
| _ => 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:
AssetsTools.NET/AssetsTools.NET.Texture/TextureFile.cs
Lines 455 to 458 in d7c25c7
| 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