Skip to content

Audit avifImageYUVAnyToRGBAnySlow(): use signed arithmetic for UV sam…#3063

Open
rootvector2 wants to merge 3 commits intoAOMediaCodec:mainfrom
rootvector2:audit-yuv-signed-arithmetic
Open

Audit avifImageYUVAnyToRGBAnySlow(): use signed arithmetic for UV sam…#3063
rootvector2 wants to merge 3 commits intoAOMediaCodec:mainfrom
rootvector2:audit-yuv-signed-arithmetic

Conversation

@rootvector2
Copy link
Contributor

…pling offsets

@wantehchang
Copy link
Collaborator

Dexter.k: Thank you for the pull request. We are in the middle of creating the libavif v1.4.0 release. I will review this PR next week.

const uint8_t * ptrU8 = uPlane ? &uPlane[(uvJ * uRowBytes)] : NULL;
const uint8_t * ptrV8 = vPlane ? &vPlane[(uvJ * vRowBytes)] : NULL;
const uint8_t * ptrA8 = aPlane ? &aPlane[j * aRowBytes] : NULL;
const uint8_t * ptrY8 = &yPlane[(size_t)j * yRowBytes];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use uint64_t as size_t can be 32 bit on 32 bit platforms.

Copy link
Collaborator

@wantehchang wantehchang Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vincent: Please see my comment at #3097 (review), which explains why we can multiply rowBytes by height or an index that is less than height using the size_t type after the pixel buffer has been allocated successfully.

This is why libavif uses the size_t type in such multiplications. Before we allocate a pixel buffer, it is not safe to do these multiplications using the size_t type, so we need to either detect overflow or use the uin64_t type.

In the avifImageYUVAnyToRGBAnySlow() function (the subject of this pull request), we may multiply rowBytes by -1 to go back the the previous row, so we need to use the signed ptrdiff_t type in those places. We need to review the changes to this function carefully.

@rootvector2
Copy link
Contributor Author

@vrabaud done

@rootvector2
Copy link
Contributor Author

Reverted uint64_t back to size_t for the row pointer offset calculations. After going through your explanation on #3097, it makes sense — once the pixel buffer has been successfully allocated, size_t is guaranteed to be safe for rowBytes * index multiplications. The ptrdiff_t changes for the signed UV adjustments remain as-is. Let me know if anything else needs to be adjusted.

@rootvector2
Copy link
Contributor Author

@wantehchang any update

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants