]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Issue 686: Keep retrying until buffer is big enough
authorTim Kientzle <kientzle@gmail.com>
Fri, 24 Jun 2016 05:07:19 +0000 (22:07 -0700)
committerTim Kientzle <kientzle@gmail.com>
Fri, 24 Jun 2016 05:07:19 +0000 (22:07 -0700)
@duy-github pointed out that commit 9134c3a fixed
one place where this mistake occurred, but left three
other places.

libarchive/archive_string.c

index 282c58e1eb9f05dd4821ea2377775105d2a2690a..33094f476122ed6469f9c7fdd7a5fbde2861639e 100644 (file)
@@ -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;