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

safety: refactor iffoutput.cpp for memory safety #4144

Merged
merged 1 commit into from
Feb 19, 2024

Conversation

lgritz
Copy link
Collaborator

@lgritz lgritz commented Feb 8, 2024

Static analysis has been yelling about code in iffoutput.cpp possibly overrunning buffers, and frankly the code in this module is so confusing that I can't tell if it's correct or not. (It's 13 years old, hasn't been touched for a long time.)

In an ideal world, this would all be rewritten to use spans so the buffer lengths are known and checked. But I don't really have the time or inclination to rewrite it -- after all, iff is not a very important file format, though I do think it's used enough that we can't drop it entirely. The payoff from a full rewrite is marginal. So I came up with the following compromise, embodied by this PR:

I'm making spans of the regions we're ultimately reading from and writing to, passing those down the chain of function calls, and so even though the actual operations are total pointer arithmetic spaghetti, we can use those spans to verify that the pointers are still within the span bounds any time we read or write through them. We do it with assertions that are only active for debug builds, so there shouldn't be any measurable performance hit. But the assertions will be active in CI for both the static and dynamic analysis tests, so should catch any latent bugs.

Static analysis has been yelling about code in iffoutput.cpp possibly
overrunning buffers, and frankly the code in this module is so
confusing that I can't tell if it's correct or not. (It's 13 years
old, hasn't been touched for a long time.)

In an ideal world, this would all be rewritten to use spans so the
buffer lengths are known and checked. But I don't really have the time
or inclination to rewrite it -- after all, iff is not a very important
file format, though I do think it's used enough that we can't drop it
entirely.  The payoff from a full rewrite is marginal. So I came up
with the following compromise, embied by this PR:

I'm making spans of the regions we're ultimately reading from and
writing to, passing those down the chain of function calls, and so
even though the actual operations are total pointer arithmetic
spaghetti, we can use those spans to verify that the pointers are
still within the span bounds any time we read or write through
them. We do it with assertions that are only active for debug builds,
so there shouldn't be any measurable performance hit. But the
assertions will be active in CI for both the static and dynamic
analysis tests, so should catch any latent bugs.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
@lgritz
Copy link
Collaborator Author

lgritz commented Feb 19, 2024

no objections -> merging

@lgritz lgritz merged commit b8bb38e into AcademySoftwareFoundation:master Feb 19, 2024
24 of 25 checks passed
@lgritz lgritz deleted the lg-iffspan branch February 20, 2024 01:48
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.

1 participant