Use size_t arithmetic for pixel offset calculations in reformat.c#3072
Open
uwezkhan wants to merge 1 commit intoAOMediaCodec:mainfrom
Open
Use size_t arithmetic for pixel offset calculations in reformat.c#3072uwezkhan wants to merge 1 commit intoAOMediaCodec:mainfrom
uwezkhan wants to merge 1 commit intoAOMediaCodec:mainfrom
Conversation
Collaborator
|
uwezkhan: Thank you for the pull request. We are in the middle of creating the libavif v1.4.0 release. I will review this pull request next week. |
wantehchang
reviewed
Mar 2, 2026
| float a; | ||
| if (state.rgb.channelBytes > 1) { | ||
| a = *((uint16_t *)(&rgb->pixels[offsetBytesA + (i * rgbPixelBytes) + (j * rgbRowBytes)])) / rgbMaxChannelF; | ||
| a = *((uint16_t *)(&rgb->pixels[offsetBytesA + ((size_t)i * rgbPixelBytes) + (j * rgbRowBytes)])) / rgbMaxChannelF; |
Collaborator
There was a problem hiding this comment.
Please run clang-format on this file:
clang-format -i --style=file src/reformat.c
or apply the following patch:
diff --git a/src/reformat.c b/src/reformat.c
index a487a724..09dacf91 100644
--- a/src/reformat.c
+++ b/src/reformat.c
@@ -322,7 +322,8 @@ avifResult avifImageRGBToYUV(avifImage * image, const avifRGBImage * rgb)
if (alphaMode != AVIF_ALPHA_MULTIPLY_MODE_NO_OP) {
float a;
if (state.rgb.channelBytes > 1) {
- a = *((uint16_t *)(&rgb->pixels[offsetBytesA + ((size_t)i * rgbPixelBytes) + (j * rgbRowBytes)])) / rgbMaxChannelF;
+ a = *((uint16_t *)(&rgb->pixels[offsetBytesA + ((size_t)i * rgbPixelBytes) + (j * rgbRowBytes)])) /
+ rgbMaxChannelF;
} else {
a = rgb->pixels[offsetBytesA + ((size_t)i * rgbPixelBytes) + (j * rgbRowBytes)] / rgbMaxChannelF;
}
@@ -807,8 +808,10 @@ static avifResult avifImageYUVAnyToRGBAnySlow(const avifImage * image,
unormV[1][0] = *((const uint16_t *)&vPlane[(uvJ * vRowBytes) + ((size_t)uvI * yuvChannelBytes) + vAdjCol]);
unormU[0][1] = *((const uint16_t *)&uPlane[(uvJ * uRowBytes) + ((size_t)uvI * yuvChannelBytes) + uAdjRow]);
unormV[0][1] = *((const uint16_t *)&vPlane[(uvJ * vRowBytes) + ((size_t)uvI * yuvChannelBytes) + vAdjRow]);
- unormU[1][1] = *((const uint16_t *)&uPlane[(uvJ * uRowBytes) + ((size_t)uvI * yuvChannelBytes) + uAdjCol + uAdjRow]);
- unormV[1][1] = *((const uint16_t *)&vPlane[(uvJ * vRowBytes) + ((size_t)uvI * yuvChannelBytes) + vAdjCol + vAdjRow]);
+ unormU[1][1] =
+ *((const uint16_t *)&uPlane[(uvJ * uRowBytes) + ((size_t)uvI * yuvChannelBytes) + uAdjCol + uAdjRow]);
+ unormV[1][1] =
+ *((const uint16_t *)&vPlane[(uvJ * vRowBytes) + ((size_t)uvI * yuvChannelBytes) + vAdjCol + vAdjRow]);
// clamp incoming data to protect against bad LUT lookups
for (int bJ = 0; bJ < 2; ++bJ) {
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change promotes several pixel offset multiplications in avifImageRGBToYUV and avifImageYUVAnyToRGBAnySlow to size_t before pointer arithmetic.
Currently, expressions such as:
(i * rgbPixelBytes)
are evaluated using 32-bit arithmetic before being promoted to size_t. While current AV1 frame dimension limits prevent overflow under normal conditions, performing these calculations directly in size_t ensures correctness and avoids reliance on implicit width constraints.