Avoid passing null altICC pointers to memcmp() (#3379)
avifSameGainMapAltMetadata() calls memcmp() on the two altICC payloads
even when they are empty, which is the default for gain maps created
with avifGainMapCreate(). memcmp(nullptr, nullptr, 0) is undefined
behavior (C99 7.1.4: pointer arguments shall have valid values even for
a zero length) and is flagged by UBSan's nonnull-attribute check when
encoding any grid whose cells carry default gain maps.
Skip the comparison when the size is zero.
Co-authored-by: krishna28238-arch <krishna28238-arch@users.noreply.github.com>
diff --git a/src/gainmap.c b/src/gainmap.c
index eee9e7b..1b3df43 100644
--- a/src/gainmap.c
+++ b/src/gainmap.c
@@ -479,7 +479,7 @@
avifBool avifSameGainMapAltMetadata(const avifGainMap * a, const avifGainMap * b)
{
- if (a->altICC.size != b->altICC.size || memcmp(a->altICC.data, b->altICC.data, a->altICC.size) != 0 ||
+ if (a->altICC.size != b->altICC.size || (a->altICC.size > 0 && memcmp(a->altICC.data, b->altICC.data, a->altICC.size) != 0) ||
a->altColorPrimaries != b->altColorPrimaries || a->altTransferCharacteristics != b->altTransferCharacteristics ||
a->altMatrixCoefficients != b->altMatrixCoefficients || a->altYUVRange != b->altYUVRange || a->altDepth != b->altDepth ||
a->altPlaneCount != b->altPlaneCount || a->altCLLI.maxCLL != b->altCLLI.maxCLL || a->altCLLI.maxPALL != b->altCLLI.maxPALL) {