summaryrefslogtreecommitdiff
path: root/libarchive/archive_string.c
diff options
context:
space:
mode:
authorTim Kientzle <kientzle@gmail.com>2016-06-23 22:07:19 -0700
committerTim Kientzle <kientzle@gmail.com>2016-06-23 22:07:19 -0700
commited9baeebd8e40eee4689909a651c6c917e62d39f (patch)
tree607c7d401d243f5d98e985bdd0afa62e61f413df /libarchive/archive_string.c
parente7642e12ea7636933cfaeecef1b26daf6490ad52 (diff)
downloadlibarchive-ed9baeebd8e40eee4689909a651c6c917e62d39f.tar.gz
Issue 686: Keep retrying until buffer is big enough
@duy-github pointed out that commit 9134c3a fixed one place where this mistake occurred, but left three other places.
Diffstat (limited to 'libarchive/archive_string.c')
-rw-r--r--libarchive/archive_string.c51
1 files changed, 27 insertions, 24 deletions
diff --git a/libarchive/archive_string.c b/libarchive/archive_string.c
index 282c58e1..33094f47 100644
--- a/libarchive/archive_string.c
+++ b/libarchive/archive_string.c
@@ -559,7 +559,8 @@ archive_wstring_append_from_mbs_in_codepage(struct archive_wstring *dest,
}
if (count == 0 && length != 0)
ret = -1;
- } while (0);
+ break;
+ } while (1);
}
dest->length += count;
dest->s[dest->length] = L'\0';
@@ -3552,18 +3553,19 @@ win_strncat_from_utf16(struct archive_string *as, const void *_p, size_t bytes,
ll = WideCharToMultiByte(sc->to_cp, 0,
(LPCWSTR)u16, (int)bytes>>1, mbs, (int)mbs_size,
NULL, &defchar);
- if (ll == 0 &&
- GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
- /* Need more buffer for MBS. */
- ll = WideCharToMultiByte(sc->to_cp, 0,
- (LPCWSTR)u16, (int)bytes, NULL, 0, NULL, NULL);
- if (archive_string_ensure(as, ll +1) == NULL)
- return (-1);
- mbs = as->s + as->length;
- mbs_size = as->buffer_length - as->length -1;
- continue;
+ /* Exit loop if we succeeded */
+ if (ll != 0 ||
+ GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
+ break;
}
- } while (0);
+ /* Else expand buffer and loop to try again. */
+ ll = WideCharToMultiByte(sc->to_cp, 0,
+ (LPCWSTR)u16, (int)bytes, NULL, 0, NULL, NULL);
+ if (archive_string_ensure(as, ll +1) == NULL)
+ return (-1);
+ mbs = as->s + as->length;
+ mbs_size = as->buffer_length - as->length -1;
+ } while (1);
archive_string_free(&tmp);
as->length += ll;
as->s[as->length] = '\0';
@@ -3634,19 +3636,20 @@ win_strncat_to_utf16(struct archive_string *as16, const void *_p,
do {
count = MultiByteToWideChar(sc->from_cp,
MB_PRECOMPOSED, s, (int)length, (LPWSTR)u16, (int)avail>>1);
- if (count == 0 &&
- GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
- /* Need more buffer for UTF-16 string */
- count = MultiByteToWideChar(sc->from_cp,
- MB_PRECOMPOSED, s, (int)length, NULL, 0);
- if (archive_string_ensure(as16, (count +1) * 2)
- == NULL)
- return (-1);
- u16 = as16->s + as16->length;
- avail = as16->buffer_length - 2;
- continue;
+ /* Exit loop if we succeeded */
+ if (count != 0 ||
+ GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
+ break;
}
- } while (0);
+ /* Expand buffer and try again */
+ count = MultiByteToWideChar(sc->from_cp,
+ MB_PRECOMPOSED, s, (int)length, NULL, 0);
+ if (archive_string_ensure(as16, (count +1) * 2)
+ == NULL)
+ return (-1);
+ u16 = as16->s + as16->length;
+ avail = as16->buffer_length - 2;
+ } while (1);
as16->length += count * 2;
as16->s[as16->length] = 0;
as16->s[as16->length+1] = 0;