100 Commits
Author SHA1 Message Date
Tobias Stoeckmann daf210f18a 7zip: Fix max bidding check
The 7zip bidder can return 48, so maximum check of 32 is too small.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-12 20:42:29 +02:00
Tobias Stoeckmann 1cd388fc60 memory: Adjust seek logic
The seek implementations for files return ARCHIVE_FATAL if seek fails to
adjust the position. ARCHIVE_FAILED is used in case of pipes etc.

Also, if a file seek fails, the position is not adjusted.

Adjust the memory implementations to do the same.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-11 21:34:28 +02:00
Tobias Stoeckmann 73412046b7 lrzip: Return matching bits in bidding
Actually follow the bidder comment and return amount of bits. Has the
nice advantage of removing the int/ssize_t handling, even though it was
safe anyway.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-10 19:52:30 +02:00
Tobias Stoeckmann 98546092b9 Fix memory leaks on open error paths
If open functions fail to store client data, free internally allocated
memory before returning an error code.

Writer open functions must check magic themselves.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-10 19:48:26 +02:00
Tobias Stoeckmann f815fc44e6 lz4/zstd: Count bits in bidder, not bytes
All other filters count bits as bidding result. Do so here as well.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-07 22:40:33 +02:00
Tobias Stoeckmann 978a9184ec lz4/zstd: Always check upper bid byte limit
If upstream filter provides huge amount of bytes, e.g. if archive is
fully stored in memory, bidding accesses too many bytes. This could in
turn overflow int return value, possibly leading to signed integer
overflows.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-07 22:27:58 +02:00
Tobias Stoeckmann c2c5fffe1c zstd: Remove redundant errno.h include
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-07 22:27:33 +02:00
Tobias Stoeckmann af46e0e7b2 format: Unify support_format styles
Use same style across all formats for easier audits.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-03 19:57:25 +02:00
Tobias Stoeckmann cb3cd6f09c format: Unify support_format error handling
Always return r to unify style for easier audits.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-03 19:57:25 +02:00
Tobias Stoeckmann d42b4c1999 format: Forward errors/warnings in support_format
If registering a format parser bidder fails or triggers a warning,
forward the information instead of silently turning it into ARCHIVE_OK.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-03 19:57:25 +02:00
Tobias Stoeckmann a2b7dea95f format: Test error handling in support_format
Make sure that all formats return a warning if registered multiple
times.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-03 19:57:25 +02:00
Tobias Stoeckmann daac5d8de0 program: Avoid signature length overflow
If a signature is too large, the bid value could overflow INT_MAX. With
its current implementation, it could even lead to a signed integer
overflow, which is undefined behavior.

Disallow huge signatures, which most likely won't impact anyone.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-02 22:12:56 +02:00
Tobias Stoeckmann 7c7d41c13c Remove bidder name
The name was only needed to find the appropriate bidder for a filter.
Since this has been resolved by accessing the last bidder, since in these
situations the bidder is added first, then the filter, the name is not
needed anymore.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-02 15:13:35 +02:00
Tobias Stoeckmann 50b585bb55 append_filter: Use last bidder for filter
Appending a filter implies appending a bidder. Always use the last added
bidder, since it was just added. This avoids desync situations where
previously added bidders could erroneously match.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-02 15:13:33 +02:00
Tobias Stoeckmann bf1f5ba79e append_filter: Verify that bidder and filter match
Make sure that appending a filter always matches its supposed bidder.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-08-02 15:12:59 +02:00
Tobias Stoeckmann 38fbee2e21 rar5: Improve varint handling
A varint field can actually have up to 10 bytes to resemble whole 64 bit
of a value. Use checked arithmetic to detect integer overflows.

Also, treat a varint which never has a byte without continuation bit as
faulty.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-20 19:03:22 +02:00
Tobias Stoeckmann 5a2330788e Generally use f for filter
If there is no good reason to deviate from the rule, always use "f" for
filter just as we use "a" for archive.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-20 08:55:51 +02:00
Tobias Stoeckmann 88df4c92f7 filter: Use f as filter variable name
- If filter is meant, use "f" instead of "self" or "filter".
- If bidder is meant, use "b" instead of "self".

Matches writer filter style.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-20 08:55:51 +02:00
Tobias Stoeckmann 12ba7fdf05 Unify filter data access names
Use "struct filter *filter" pattern, with filter being the name of the
filter. This matches the format parser pattern.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-20 08:55:51 +02:00
Tobias Stoeckmann 11b87598ce shar: Merge format setters
Clarify where the differences between the format setters are.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-20 08:55:51 +02:00
Tobias Stoeckmann 9f62f96798 Unify filter data handling
- Declare access variable at the beginning
- Remove unneeded cast

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-20 08:55:51 +02:00
Tobias Stoeckmann 6c3bab2480 Unify format data access names
It's generally the pattern "struct format *format". Unify for easier
readability.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-20 08:55:50 +02:00
Tobias Stoeckmann a631287e4c Unify format data handling
- Declare access variable at the beginning
- Remove unneeded cast (see warc parser, which never did it)
- Remove inline cast function
- Avoid unneeded brackets to unify style

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-20 08:55:25 +02:00
Tobias Stoeckmann 29dd9c909b write: Use uint64_t for __archive_write_nulls
Even though int64_t would be the correct data type because no more data
than INT64_MAX can be written, use the easier solution for format
writers for now.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-19 23:08:26 +02:00
Tobias Stoeckmann 0543fc1320 write: Do not write more than INT64_MAX bytes
Never try to write more than INT64_MAX bytes to avoid integer overflows.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-19 23:08:26 +02:00
Tobias Stoeckmann 91fa66073e tar: Verify that huge files are not written
Files which cannot fit into a stream should never return success, which
can happen due to truncation on 32 bit systems.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-19 23:07:44 +02:00
Tobias Stoeckmann 8827bc3016 warc: Avoid isdigit in version number check
The isdigit function is locale-dependent, which means that characters
outside of 0-9 range can return true here, e.g. "power of 2" for a few
locales on Windows.

Test for ASCII character range from '0' to '9' instead.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-19 13:46:06 +02:00
Tobias Stoeckmann 66e5b3e5d7 uuencode: Fix typo
Add missing "encode" to magic error message.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-17 19:02:35 +02:00
Tobias Stoeckmann 727da55204 filter: Unify add_filter style
This makes it easier to spot differences, which might require more
attention.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-17 19:02:35 +02:00
Tobias Stoeckmann 421b6c267d filter: Check __archive_write_allocate_filter result
If allocation of filter fails, gracefully handle it instead of
eventually triggering a NULL pointer dereference.

Allocate the filter in last step, because it's rather difficult to
cleanly revert in case of error.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-17 19:02:35 +02:00
Tobias Stoeckmann b2a38b8845 xz: Merge more common code into common_setup
Reduces complexity of individual functions.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-17 19:02:35 +02:00
Tobias Stoeckmann dfc4357a74 filter: Simplify archive handling
Use archive for magic check and logging directly.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-17 19:02:35 +02:00
Tobias Stoeckmann 42584eb62f compress: Allocate data while adding filter
This syncs compress write filter with other write filters.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-17 19:02:27 +02:00
Tobias Stoeckmann c2ebb602e9 filter: Set function pointers in add_filter
Set function pointers in add filter functions, not in open.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-17 17:43:15 +02:00
Tobias Stoeckmann 860365a3f0 filter: Free data in its own function
Create free_data function for later reusing.
Also make sure that f->data is always set to NULL.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-17 17:43:15 +02:00
Tobias Stoeckmann 48e77da188 filter: Do not set filter data redundantly
There is no need to assign fields again if they do not change.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-17 17:43:15 +02:00
Tobias Stoeckmann c74bb50661 Fix typos
Typos found with codespell.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-17 08:49:17 +02:00
Tobias Stoeckmann 8992885932 Fix typos
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-17 08:49:12 +02:00
Tobias Stoeckmann d1c8cca883 cab: Check __archive_read_consume result
The bytes were not previously read, so check result of
__archive_read_consume.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-14 20:29:55 +02:00
Tobias Stoeckmann 8e750da087 warc: Sort include headers
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-11 13:22:58 +02:00
Tobias Stoeckmann 7d8c565377 warc: Unify private function names
Use same style as in other format parsers.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-11 13:22:58 +02:00
Tobias Stoeckmann 38a4047782 warc: Use common function names
Apply same style used in other format parsers.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-11 13:22:58 +02:00
Tobias Stoeckmann d646e22bd8 warc: Use proper data type for unconsumed
Match other parsers and use int64_t to avoid casts and switching between
signed and unsigned.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-11 13:22:56 +02:00
Tobias Stoeckmann d78fea3815 warc: Simplify __archive_read_ahead check
A short read would lead to a NULL return value, thus the second check
will never be true.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-11 13:20:06 +02:00
Tobias Stoeckmann d3d8481e7f cab: Grow pre-tree Huffman table if needed
The standard has no fixed 10 bit limit. Instead, a pre-tree could
contain up to 16 bit codes, although this is very unlikely.

Grow the table if needed.

While at it, clarify now that max_bits are actually lookup_bits.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann e04051cb4b cab: Add large LZX pre-tree test
If a pre-tree is larger than 10 bit, libarchive fails to read the data.
According to spec, up to 16 bit are valid.

Example file provided by Duncan Horn. Thanks a lot!

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann a791094bdf cab: Clarify LZX/Huffman bit handling
- Huffman operations are up to 16 bit, thus uint16_t
- LZX itself can handle up to 32 bit, thus uint32_t
- Memory sizes are size_t

Otherwise:

- Use uint8_t to show where bit lengths are stored
- Use int to clarify that a status is stored

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann e155b516a0 cab: Remove check in lzx_make_huffman_table
The check against table size is redundant since Kraft's inequality
already verified that this operation cannot exceed table size anymore.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann 8d22629c9e cab: Use correct limit in lzx_make_huffman_table
The table size is in bytes, but bitlen must be checked against maximum
bit length allowed.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann dcf31bea79 cab: Clarify lzx_decode_huffman signature
No more than 16 bits shall ever be supplied for a table lookup.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann 8beccd23e2 cab: Adjust lzx_read_bitlen API
Use 0 as marker for maximum symbol count to be able to eventually switch
to another data type.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann 4fa9fc1f37 cab: Clear lookup table in lzx_huffman_init
This step is not actually needed, but will prevent static analysis
warnings that values might exist which have never been initialized.

"Invalid" values only exist outside of the addressable area, which can
happen if maximum code bit length is smaller than table bit length.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann 5bcaf207c1 cab: Clarify what "len_size" is
It's actually the amount of symbols/codes in Huffman tree. Clarify it by
using the field name symbol_count instead.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann 4d8e56fd19 cab: Use ARCHIVE_* values with lzx functions
Some lzx functions reverse the generic logic of 0 being success and
anything else being an error. It's debatable from a boolean point of
view but use the regular ARCHIVE_OK in case of success, which is 0.

Unifies with other lzx functions and general idea of libarchive
functions.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann 898b63c19b cab: Fix Kraft's inequality check
We really have to check if Kraft's inequality is 1 (in our case it's
0x10000) or 0 for an empty tree. Anything else is an error.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann eed685b4d6 cab: Simplify lzx_decode_huffman
As long as the given index is in range of the table, the previous
creation of the lookup table takes care that all elements are valid.

This is a left over of actual tree removal done in commit
c253f0aae9.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann 06765dbea5 cab: Remove inline keyword
Most if not all compilers today understand inline, but it's also just a
hint. Static functions will be inlined if it's worth it from compiler
perspective and inline functions will be still functions, too.

Remove it and let compilers decide. Increases portability for very old
compilers which cannot handle it.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-09 08:37:34 +02:00
Tobias Stoeckmann 1bcc833a5d Check size arithmetic before memory allocations
Make sure that multiplications do not overflow (or if they overflow,
handle them properly).

This also means that proper data type size_t instead of int is used.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-07 17:57:33 +02:00
Tobias Stoeckmann 68ccc129a8 options: Use const char pointer where possible
If referenced strings are not modified, use const char pointer.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-07 17:16:01 +02:00
Tobias Stoeckmann 59826c033e cab: Use strnlen if possible
Adjust cab_strnlen to match strnlen API and use system's strnlen if
available.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-05 21:46:12 +02:00
Tobias Stoeckmann 405cf62736 mtree: Fix custom strnlen API
If strnlen is not available, custom code is used. Make sure that the
custom code keeps premise of strnlen API for maximum compatibility.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-05 21:35:14 +02:00
Tobias Stoeckmann 07aca47167 cab: Extend Huffman documentation
Explain the algorithm and data structure involved in Huffman tree
handling as array. Hopefully simplifies upcoming code change
verifications.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-05 19:35:45 +02:00
Tobias Stoeckmann 3ad6bb89a9 cab: Do not nest struct definitions
This complies with other C source files of format parsers.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-05 17:27:14 +02:00
Tobias Stoeckmann c1ce208f8e cpio: Prevent signed integer overflow
If enough bytes have to be skipped, a signed integer overflow could
occur (most realistically on 32 bit systems). Use an unsigned type,
which could still overflow but has only negative impact on diagnostic
warning message.

Follow up of 02fa05675f but this time for
function find_newc_header.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-04 19:06:52 +02:00
Tobias Stoeckmann f4493cea76 raw: Properly treat huge inputs
If the raw parser ever treats a basically endless stream which
eventually overflows INT64_MAX, fail instead of overflowing
offset + size calculation, leading to subsequent issues in callers.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-03 23:26:28 +02:00
Tobias Stoeckmann 47964b3533 Check archive_entry_new2 return value
The memory allocation might fail. Check return value and properly handle
error cases.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-03 22:45:11 +02:00
Tobias Stoeckmann 53d1c0bc65 cab: Add E8 translation test
Covers basic E8 translations, yet it lacks the handling of negative
values because this would require large archives.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-03 17:37:37 +02:00
Tobias Stoeckmann 220eefe346 cab: Translate E8 calls only in first 1 GB of data
The cab specification states that E8 translation shall be performed only
within the first 1 GB range of an uncompressed file, i.e. for the first
0x8000 CFDATA entries in a folder.

Take this limit properly into account.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-03 17:37:34 +02:00
Tobias Stoeckmann dfcc108ebd Check result of archive_read_set_callback_data
The memory allocation in archive_read_set_callback_data2, which is
called by archive_read_set_callback_data, could fail.

Check result and properly handle failure.

Resolves #3210.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-07-01 22:38:58 +02:00
Tobias Stoeckmann 1afe5ba2cd cab: Improve lzx_translation data types
Use correct data types to reduce amount of casts. This should further
improve readability.

While at it, also improve white space usage.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-30 22:42:06 +02:00
Tobias Stoeckmann ee07a5486a cab: Improve lzx_translation variable names
It's quite rare that "p" is not the iteration pointer but the base
pointer, while "b" is not the base pointer but the iteration pointer.

Use more descriptive variable names to assist auditors to figure out how
E8 call translation reversal works. It's already hard by itself.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-30 22:40:17 +02:00
Tobias Stoeckmann 4e6c97e66c cab: Turn translation size into int32_t
It's a file size and used for signed integer comparison. Since it's
literally a file size, its maximum value should be properly checked as
well.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-30 22:38:52 +02:00
Tobias Stoeckmann b4f9d327c6 cab: Document some magic numbers
Use more macro definitions instead of magic numbers to improve code
readability. Hopefully this explains a bit more where these values come
from.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-30 22:36:29 +02:00
Tobias Stoeckmann ed8d866f77 cab: Improve decoding status value description
Move the macro definitions up to the field where they are used, which is
done for other such fields as well.

This in turn allows to avoid the magic value 0 in initial assignment.
Use ST_RD_TRANSLATION instead for better documentation where the finite
state machine starts (and that this state cannot be reached again).

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-30 22:33:32 +02:00
Tobias Stoeckmann 33e0dd575e cab: Remove obsolete safety checks
Commit e1f890dc62 fixed the underlying
root cause of these OOB checks: If a header is invalid and ARCHIVE_FATAL
is returned, do not allow data reads anymore.

The checks while reading CFDATA already looked rather off, since the
check exists in header parser and the value is never updated, but
prevent worst case scenario.

Keep the tests as regression tests if archive_read.c is modified again.

This makes the code easier to read and audit.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-29 19:55:08 +02:00
Tobias Stoeckmann 17af9d6ea6 ar: Adjust style and fix comment grammar
Use style most often seen in other format parsers.
While at it, fix grammar in comment.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-29 17:12:37 +02:00
Tobias Stoeckmann 7054bec543 ar: Simplify error handling in _ar_read_header
The comment states that a warning should be returned, but
97f964e3e0 explicitly changed that to
fatal. Adjust the comment and remove unneeded function call.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-29 17:12:37 +02:00
Tobias Stoeckmann 80cf6d2199 ar: Simplify _ar_read_header cases
Symbol tables are not treated specially. Clarify that by merely
mentioning them in a comment.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-29 17:12:37 +02:00
Tobias Stoeckmann c3e27cc135 ar: Set filetype only once
All entries in ar are regular files. Set this information only once to
simplify code audits.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-29 17:12:37 +02:00
Tobias Stoeckmann e235bb06a4 ar: Simplify ar_parse_common_header usage
The function ar_parse_common_header can be called at exactly one
position in _ar_read_header, since it parses common information.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-29 17:12:37 +02:00
Tobias Stoeckmann 2876f4912b ar: Simplify __archive_read_consume usage
The __archive_read_consume function is binary: Either all bytes are
consumed or a failure occurred. No partial consumption can occur.

Simplify its calls to clarify this.

While at it, adjust style of callers as well.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-29 17:12:30 +02:00
Tobias Stoeckmann b3ad4f20c7 mtree: Fix integer overflow on 32 bit systems
If a file close to SSIZE_MAX in size is mapped into memory and checked
for mtree, a signed integer overflow could occur.

While this this generally harmless and the scenario very unlikely, fix
the issue with a proper cast.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-28 22:16:42 +02:00
Tobias Stoeckmann ce962f1425 ar: Use correct data types
Use int64_t for entry/file sizes.
Use size_t for sizes in heap.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-28 12:11:02 +02:00
Tobias Stoeckmann 4bb750c789 xattr: Treat memory allocation failure as error
Do not silently truncate values if allocation fails.

Reported by ysf.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-28 10:43:31 +02:00
Tobias Stoeckmann f58645fb60 7zip: Fix style and typos
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann 9a92fb1d92 7zip: Move files_info_numfiles_is_sane body
Declare and define files_info_numfiles_is_sane like all other static
functions in this file.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann 532b6a8d9e 7zip: Do not store unused values
If values are only parsed and verified but not used later on, skip field
assignments. This reduces amount of fields to audit.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann e9e9b2ebcc 7zip: Check skip in setup_decode_folder
Make sure that all bytes are actually skipped. Normally, the result can
be ignored because the bytes were read previously. Here, more bytes than
previously read will be skipped.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann 20e12ad019 7zip: Fix BZ2/DEFLATE with huge input files
If more than UINT_MAX byte are available, e.g. if a very large archive
is mapped into memory, the BZ2/DEFLATE avail_in/avail_out values are
truncated.

Set to maximum allowed value in such cases.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann cd938e2815 7zip: Simplify decode_encoded_header_info
With correct data types in place, simplify the overflow check by using
checked arithmetic and simple comparison.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann aca55d476b 7zip: Use correct data types
Follow idea of internal libarchive APIs:

- File offsets and sizes are int64_t
- Memory offsets and sizes are size_t

Also use correct data types for iterator variables.

This refactoring also makes parse_7zip_uint64 obsolete by turning it
into parse_7zip_int64.

Resolves GHSA-m4vx-w6j3-jp82.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann 5a7ae702e4 7zip: Unify unpack_streams check
Use checked arithmetic and unify unpack_streams check to cover all
cases. This should be a purely defensive measure.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann c1bcd9ca9e 7zip: Unify UMAX_ENTRY check
Introduce parse_7zip_size, which performs the UMAX_ENTRY check.
Generally, use this function whenever memory allocation sizes or indices
into memory allocations are used, i.e. when size_t is meant.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann c1ddb31c07 7zip: Use checked arithmetic more often
Use checked arithmetic to avoid overflows. Use it also where checks are
already in place for better readability.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann a6e72ee36a 7zip: Simplify decode_codec_id
Since we have checked integer arithmetic now, use it.
Clarify that INT64_MAX is the maximum according to standard.

With that, switch to int64_t to avoid (unsigned long)-1 casts.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann b7e902b471 7zip: Check numDigests in read_SubStreamsInfo
Verify that sum of numUnpackStreams will never overflow numDigests. For
this, also set numDigests to size_t because that's how the variable is
used later on.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:40 +02:00
Tobias Stoeckmann 48657cc38f 7zip: Handle large requests in extract_pack_stream
If the minimum amount of bytes could overflow size_t after alignment,
allocate exact minimum amount instead.

This avoids integer overflow and will most likely lead to regular OOM
error handling instead.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 19:23:36 +02:00
Tobias Stoeckmann bc3e9af734 7zip: Skip bytes in read_stream in chunks
Avoid huge requests while skipping bytes to avoid integer truncation and
memory allocation issues.
2026-06-26 18:36:04 +02:00
Tobias Stoeckmann a42f5d1322 7zip: Improve integer handling in skip_stream
The skip_stream function is supposed to skip a specified amount of bytes
from input stream. Since streams could be larger than 4 GB even on 32
bit systems, do not cast to size_t. Instead, read the data in small
chunks until all bytes are skipped.

This also helps to avoid large allocations on 64 bit systems.

Also verify that skipped_bytes tracking cannot overflow.

While at it, change return value to int because it's only needed for
status information, i.e. if operation was successful or not. This avoids
another cast issue with uint64_t values larger than INT64_MAX.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-26 18:36:04 +02:00
Tobias Stoeckmann 37b0b5a502 unzip: Add Windows port
Copy missing functionality from libarchive and cpio.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2026-06-24 18:02:15 +02:00