-
Notifications
You must be signed in to change notification settings - Fork 669
fix(targa): Protection against corrupt, mis-sized palette #5165
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
Binary file not shown.
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.
I would naively assume upon reading this conditional that either the addition, or direction of the inequality, was wrong. Or maybe that it should be
if (first + number_of_potential_values > length) { fail }instead? Could it not be the case that a file uses start = 200 and length 256 and would still be valid for an 8bpp image - assuming that the image never used values > 56?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.
Your question forced me to admit that my understanding here was shaky, so I did some research and I think our code is wrong (and always has been). Here's what I learned, assuming I understand it correctly now.
As you probably know, TARGA files come from Truevision, who made early PC frame buffers / accelerators. The hardware palette registers were a shared resource, and often the OS or other hardware features wanted to reserve the first 16 or whatever and not have those change, nor store them in the file. The 'start' value is the destination offset into a hardware LUT where your palette should be copied.
Near as I can tell -- and contrary to our buggy code! -- the indices in the image pixels are absolute, not relative to the palette's start value. So if the (first+length) is more than can be addressed by the bpp, something has gone awry, because those high indices can never be specified in the pixels.
It seems that our current code in decode_pixel is wrong, it's adding the indices in the file to the start offset, but I now believe that it shouldn't be doing that. In fact, since we only allocate and store the ones beginning with 'start', what we should be doing is subtracting the start value to index into our own array.
Of course, it's been decades since Truevision boards, so anybody making TGA files is not worried about overwriting reserved LUT entries in the hardware, and thus there is no reason to reserve palette value and have the start value be anything other than 0. This is probably why we've never discovered this issue before.
I will revise to fix these bugs as well.
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.
My copy of the Targa spec actually doesn't say whether the pixel values are absolute or relative indices. But I spot-checked some other software to see what they did, and it seems they all treat it as absolute. I suppose the only files you'd find with a nonzero start value are ones that date from the era where people were intending the image to run on a specific hardware board (because the number of palette registers you'd want to not disturb wasn't the same for all board models), and that's the reason we never came across this condition in practice.