]> git.ipfire.org Git - thirdparty/libarchive.git/commit
Bugfix and optimize archive_wstring_append_from_mbs()
authorMartin Matuska <martin@matuska.org>
Thu, 21 Nov 2019 02:08:40 +0000 (03:08 +0100)
committerMartin Matuska <martin@matuska.org>
Thu, 21 Nov 2019 02:08:40 +0000 (03:08 +0100)
commit22b1db9d46654afc6f0c28f90af8cdc84a199f41
tree2e9967f060e4f8081212b7d9922aa70924cc775c
parentdc064872521cacc46c566ddc96f5609752c745ad
Bugfix and optimize archive_wstring_append_from_mbs()

The cal to mbrtowc() or mbtowc() should read up to mbs_length
bytes and not wcs_length. This avoids out-of-bounds reads.

mbrtowc() and mbtowc() return (size_t)-1 wit errno EILSEQ when
they encounter an invalid multibyte character and (size_t)-2 when
they they encounter an incomplete multibyte character. As we return
failure and all our callers error out it makes no sense to continue
parsing mbs.

As we allocate `len` wchars at the beginning and each wchar has
at least one byte, there will never be need to grow the buffer,
so the code can be left out. On the other hand, we are always
allocatng more memory than we need.

As long as wcs_length == mbs_length == len we can omit wcs_length.
We keep the old code commented if we decide to save memory and
use autoexpanding wcs_length in the future.

Fixes #1276
libarchive/archive_string.c