-
Notifications
You must be signed in to change notification settings - Fork 281
fix: resolve alignment violations #3054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
uwezkhan
wants to merge
1
commit into
AOMediaCodec:main
from
uwezkhan:alignment-violations-alpha-reformat
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
|
|
||
| #include "avif/avif.h" // IWYU pragma: export | ||
|
|
||
| #include <string.h> // For memcpy, used by avifLoadU16/avifStoreU16. | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
@@ -19,6 +21,22 @@ extern "C" { | |
| #define AVIF_MIN(a, b) (((a) < (b)) ? (a) : (b)) | ||
| #define AVIF_MAX(a, b) (((a) > (b)) ? (a) : (b)) | ||
|
|
||
| // Alignment-safe load/store for 16-bit values from potentially unaligned byte pointers. | ||
| // Using memcpy avoids undefined behavior from casting uint8_t* to uint16_t* without | ||
| // alignment guarantees (C standard §6.3.2.3). Modern compilers optimize these to a single | ||
| // load/store instruction on platforms that support unaligned access (e.g., x86/x64). | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct. libavif takes care to use |
||
| static inline uint16_t avifLoadU16(const uint8_t * p) | ||
| { | ||
| uint16_t v; | ||
| memcpy(&v, p, sizeof(v)); | ||
| return v; | ||
| } | ||
|
|
||
| static inline void avifStoreU16(uint8_t * p, uint16_t v) | ||
| { | ||
| memcpy(p, &v, sizeof(v)); | ||
| } | ||
|
|
||
| // Used for debugging. Define AVIF_BREAK_ON_ERROR to catch the earliest failure during encoding or decoding. | ||
| #if defined(AVIF_BREAK_ON_ERROR) | ||
| static inline void avifBreakOnError() | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for reporting this issue. The libavi API assumes that the
uint8_t *buffers, if they contain high bit depth (> 8) pixels, point touint16_tarrays, so that it is safe to cast theuint8_t *buffers for high bit depth pixels touint16_t *. This is an API contract.I did not check this pull request in its entirety (because it is very long). Under this API contract, I believe none of the changes in this pull request are needed. Could you please go over your pull request and see if there are any changes that should be kept? Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can consider checking this API contract on entry to the relevant functions.
avifImagestructs are managed by libavif, so we can safely assume their pixels buffers are aligned.The
pixelsbuffers inavifRGBImagestructs may be allocated by libavif, but they can also point to buffers allocated by the caller. So I think it suffices to validate that thepixelsbuffers inavifRGBImagestructs are aligned.But this is an extremely unlikely issue in practice because it would be inconvenient to the callers themselves if they allocated unaligned buffers for high bit depth pixels.