libarchive_cpp_wrapper icon indicating copy to clipboard operation
libarchive_cpp_wrapper copied to clipboard

size_t vs ssize_t in error handling

Open as-ascii opened this issue 1 year ago • 0 comments

Hello,

First of all thank you for great job to make libarchive library easier to embed in C++ application. We tried to use the library in larger opensource data processing SDK DocWire DocToText ( https://github.com/docwire/doctotext ) for extracting data from files in office formats and other stored inside compressed archives. Integration was quite quick and easy.

Unfortunately we had some unexpected crashes on password-protected files and after some debugging and investigation we found a problem in libarchive_cpp_wrapper code.

It seems that the code should be analyzed carefully for usage of size_t and ssize_t types. For example in the following fragment:

size_t _buff_size = archive_read_data(_archive, _buff, _buff_max_size); if( _buff_size < 0 ) { throw archive_exception( "Archive reader buffer reading error." ); }

buff_size should be ssize_t (signed) not size_t (unsigned). Unsigned values will be never less that zero so the program will do some next bad things and crash instead of throwing the exception.

Function signature according to the documentation: ssize_t archive_read_data(struct archive *, void *buff, size_t len);

We choosed the way to use libarchive directly finally, but I think that it is good to let you know.

Best regards.

as-ascii avatar Aug 01 '23 16:08 as-ascii