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

Ensure correct integer promotion when adding #268

Merged
merged 3 commits into from
Apr 12, 2023
Merged

Conversation

jpcha2
Copy link
Contributor

@jpcha2 jpcha2 commented Jan 17, 2023

When adding multiple mz_uint32 values (or a mix of mz_uint32 and mz_uint64 values) to a mz_uint64 value, the mz_uint32 values should be promoted to mz_uint64 before the addition is done. To ensure this is the case, the mz_uint64 value should be placed first, or, if all values are mz_uint32, then the first one should be cast to mz_uint64.

Code Quality (Avoid Integer Overflow): Ensure correct order of integer size promotion when doing additions by placing the mz_uint64 value first or explicitly casting the first value to mz_uint64.
Additional parameter checks
@@ -203,6 +203,10 @@ tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_nex
size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start;

/* Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter). */
if ((!pOut_buf_start) || (!pOut_buf_next) || (!pIn_buf_size) || (!pOut_buf_size))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This might impact performance but YMMV

Copy link
Contributor

Choose a reason for hiding this comment

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

This part of the patch is wrong: pOut_buf_next can legitimately be NULL here, and this change results in decompression failure with valid files.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm hitting pOut_buf_start == NULL with tinfl_decompress called from tinfl_decompress_mem_to_heap

Copy link
Contributor

Choose a reason for hiding this comment

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

@uroni: I suggest reverting commit fe6330e

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see that in tinfl_decompress_mem_to_heap, the first iteration of the main for loop has pBuf set to NULL and the resulting call to tinfl_decompress will have both pOut_buf_start and pOut_buf_next set to NULL. tinfl_decompress responds to this with TINFL_STATUS_HAS_MORE_OUTPUT if the check is eliminated, which is correct behaviour. Calling this function with either buffer being NULL while the other is valid also results in a valid error code. Calling it with either buffer length pointer being NULL results in a read access violation before the test. I therefore think it would be best to:

  1. Remove the test for NULL for pOut_buf_start and pOut_buf_next, and,
  2. Rework the initialization to test for NULL for pIn_buf_size and pOut_buf_size.

@uroni uroni merged commit 9ae305f into richgel999:master Apr 12, 2023
jpcha2 added a commit to jpcha2/miniz that referenced this pull request Oct 9, 2023
Remove parameter check that breaks tinfl_decompress_mem_to_heap() (see discussion on pull request richgel999#268)
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.

3 participants