- /* snprintf should return -1 only in the error case, but older
- * glibcs and probably other systems are buggy in this respect and
- * return -1 if the buffer was too small. We only abort for LARGE
- * unrealistic buffer sizes here */
- if (len < 0) {
- if (size > 65536)
- return -1;
- size *= 2;
- buffer = realloc(buffer, size);
- } else if (len >= (int) size) {
- /* this should not happen if snprintf works correctly */
- abort();
- }
- } while (len < 0);
- free(buffer);
+ /* snprintf should return -1 only in the error case, but older glibcs
+ * and probably other systems are buggy in this respect and return -1 if
+ * the buffer was too small. We only abort for LARGE unrealistic buffer
+ * sizes here */
+ if (len < 0) {
+ if (buffer != buf)
+ free(buffer);
+ if (size > 65536)
+ return -1;
+ size *= 2;
+ } else if ((size_t)len >= size) {
+ /* If we come here more than once, vsnprintf() returned garbage */
+ assert(buffer == buf);
+ size = (size_t)len + 1;
+ } else {
+ break;
+ }
+ buffer = malloc(buffer, size);