Skip to content
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

Fix potential overflow in GIF decoding #1036

Merged
merged 1 commit into from
Dec 20, 2019

Conversation

fintelia
Copy link
Contributor

Fixes: #877

That issue was caused by an overflow in the "frame delay" field between individual frames of the GIF frames when expressed in milliseconds. This translates to highly unrealistic frame rates of greater than 65 seconds per frame (i.e. over 1000x slower than a normal monitor refresh rate), but ideally should still not result in panics. This PR switches to using u32's to prevent the possibility of overflow while still allowing all delay values expressible by GIF images.

At the same time I noticed that there was some confusion about the actual units used to store frame delays in various places. I've renamed a bunch of places to "delay_ms" to be clear when milliseconds are in use.

Open questions: Is num_rational::Ratio really the right type to express the delay in milliseconds between frames of an animation?

Copy link
Member

@HeroicKatora HeroicKatora left a comment

Choose a reason for hiding this comment

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

Not the location I would suspected many bugs. Great!

src/gif.rs Outdated Show resolved Hide resolved
@HeroicKatora
Copy link
Member

Open questions: Is num_rational::Ratio really the right type to express the delay in milliseconds between frames of an animation?

Is it even used for any delay with numerator != 1?

@fintelia
Copy link
Contributor Author

fintelia commented Sep 16, 2019

Is it even used for any delay with numerator != 1?

No, but it would be necessary to exactly encode something like 60 Hz. Though an integer number of nanoseconds for frame delay might also have low enough error to not matter?

@HeroicKatora
Copy link
Member

Apparently it doesn't really matter for gif, the format itself doesn't allow for better timings, but there is no obvious disadvantage despite compile times either. Address one issue at a time I'd say, so let's simply keep that for now.

@fintelia
Copy link
Contributor Author

Sounds good. Also, this is a breaking change since I've renamed some things/changed the type. Should I be pushing this to the next branch?

@HeroicKatora
Copy link
Member

Right. That poses the question: Are the type changes necessary? We will have some arbitrary maximum supported frame delay in either case. While u16 is (obviously?) a bit too short in the long term other formats may/will have other limits and we shouldn't have to adjust the type every time. Maybe the pragmatic choice is to instead do:

Ratio::new(frame.delay.saturating_mul(10), 1)

and clearly document this saturation. We surely don't want to error on decoding this and it's the most accurate replacement value.

@HeroicKatora
Copy link
Member

Add: I just noted that Frame is completely private so we are of course free to change the internals to Ration<u32> without breaking the external interface. If delay_u32 were an addition instead of a replacement we could have the best of both worlds.

@HeroicKatora
Copy link
Member

@fintelia Do you think we can/should land this for 0.22.3?

@fintelia
Copy link
Contributor Author

This changes a couple methods on animation::Frame so it is technically a breaking change. Merging onto the next branch might be better?

@HeroicKatora
Copy link
Member

Ah right, yes, change the target to next in that case. OR we could maybe get away with a saturating multiplication and no type changes instead for an alternative 0.22 compatible change.

@HeroicKatora
Copy link
Member

HeroicKatora commented Dec 20, 2019

@fintelia Reminder that you wanted to rebase this on next and it's a partial blocker for comprehensive fuzzing.

@fintelia fintelia changed the base branch from master to next December 20, 2019 14:19
@fintelia
Copy link
Contributor Author

fintelia commented Dec 20, 2019

Rebased. If everything looks good, lets merge once CI passes

Copy link
Member

@HeroicKatora HeroicKatora left a comment

Choose a reason for hiding this comment

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

All concerns were over breaking the interface. The new names and types are more consistent so for next this is positive instead.

@fintelia fintelia merged commit 182de11 into image-rs:next Dec 20, 2019
@fintelia fintelia deleted the delay-overflow branch August 25, 2023 05:03
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.

2 participants