1
0
mirror of https://git.tukaani.org/xz.git synced 2026-10-31 19:34:55 +00:00

liblzma: Index decoder: Detect a too large allocation earlier

Previously, if an Index had more than PREALLOC_MAX Records, then
allocations were done in PREALLOC_DEFAULT chunks (512 Records).
That makes little sense in practice because it's likely that one
would run out of address space later. Return LZMA_MEM_ERROR error
pre-emptively if PREALLOC_MAX would be exceeded.
This commit is contained in:
Lasse Collin 2026-07-24 21:23:31 +03:00
parent 9be8d5c91f
commit a967036afb
No known key found for this signature in database
GPG Key ID: 38EE757D69184620
3 changed files with 10 additions and 5 deletions

View File

@ -427,11 +427,11 @@ lzma_index_end(lzma_index *i, const lzma_allocator *allocator)
} }
extern void extern bool
lzma_index_prealloc(lzma_index *i, lzma_vli records) lzma_index_prealloc(lzma_index *i, lzma_vli records)
{ {
if (records > PREALLOC_MAX) if (records > PREALLOC_MAX)
records = PREALLOC_MAX; return true;
// If index_decoder.c calls us with records == 0, it's decoding // If index_decoder.c calls us with records == 0, it's decoding
// an Index that has no Records. In that case the decoder won't call // an Index that has no Records. In that case the decoder won't call
@ -454,7 +454,7 @@ lzma_index_prealloc(lzma_index *i, lzma_vli records)
records = INDEX_GROUP_SIZE; records = INDEX_GROUP_SIZE;
i->prealloc = (size_t)(records); i->prealloc = (size_t)(records);
return; return false;
} }

View File

@ -38,7 +38,9 @@ extern uint32_t lzma_index_padding_size(const lzma_index *i);
/// Set for how many Records to allocate memory the next time /// Set for how many Records to allocate memory the next time
/// lzma_index_append() needs to allocate space for a new Record. /// lzma_index_append() needs to allocate space for a new Record.
/// This is used only by the Index decoder. /// This is used only by the Index decoder.
extern void lzma_index_prealloc(lzma_index *i, lzma_vli records); ///
/// \return false on success, true if records is too large (SIZE_MAX issue)
extern bool lzma_index_prealloc(lzma_index *i, lzma_vli records);
/// Round the variable-length integer to the next multiple of four. /// Round the variable-length integer to the next multiple of four.

View File

@ -123,7 +123,10 @@ index_decode(void *coder_ptr, const lzma_allocator *allocator,
// Tell the Index handling code how many Records this // Tell the Index handling code how many Records this
// Index has to allow it to allocate memory more efficiently. // Index has to allow it to allocate memory more efficiently.
lzma_index_prealloc(coder->index, coder->count); // If the Record count is so high that memory allocation would
// fail later, return LZMA_MEM_ERROR now.
if (lzma_index_prealloc(coder->index, coder->count))
return LZMA_MEM_ERROR;
ret = LZMA_OK; ret = LZMA_OK;
coder->sequence = coder->count == 0 coder->sequence = coder->count == 0