diff options
Diffstat (limited to 'openbsd-compat/bsd-snprintf.c')
-rw-r--r-- | openbsd-compat/bsd-snprintf.c | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/openbsd-compat/bsd-snprintf.c b/openbsd-compat/bsd-snprintf.c index cefb1d1ad..41d2be238 100644 --- a/openbsd-compat/bsd-snprintf.c +++ b/openbsd-compat/bsd-snprintf.c | |||
@@ -168,12 +168,13 @@ | |||
168 | 168 | ||
169 | #define DOPR_OUTCH(buf, pos, buflen, thechar) \ | 169 | #define DOPR_OUTCH(buf, pos, buflen, thechar) \ |
170 | do { \ | 170 | do { \ |
171 | if (++pos >= INT_MAX) { \ | 171 | if (pos + 1 >= INT_MAX) { \ |
172 | errno = ERANGE; \ | 172 | errno = ERANGE; \ |
173 | return -1; \ | 173 | return -1; \ |
174 | } \ | ||
174 | if (pos < buflen) \ | 175 | if (pos < buflen) \ |
175 | buf[pos] = thechar; \ | 176 | buf[pos] = thechar; \ |
176 | } \ | 177 | (pos)++; \ |
177 | } while (0) | 178 | } while (0) |
178 | 179 | ||
179 | static int dopr(char *buffer, size_t maxlen, const char *format, | 180 | static int dopr(char *buffer, size_t maxlen, const char *format, |
@@ -494,7 +495,8 @@ fmtstr(char *buffer, size_t *currlen, size_t maxlen, | |||
494 | ++cnt; | 495 | ++cnt; |
495 | } | 496 | } |
496 | while (*value && (cnt < max)) { | 497 | while (*value && (cnt < max)) { |
497 | DOPR_OUTCH(buffer, *currlen, maxlen, *value++); | 498 | DOPR_OUTCH(buffer, *currlen, maxlen, *value); |
499 | *value++; | ||
498 | ++cnt; | 500 | ++cnt; |
499 | } | 501 | } |
500 | while ((padlen < 0) && (cnt < max)) { | 502 | while ((padlen < 0) && (cnt < max)) { |
@@ -582,8 +584,10 @@ fmtint(char *buffer, size_t *currlen, size_t maxlen, | |||
582 | } | 584 | } |
583 | 585 | ||
584 | /* Digits */ | 586 | /* Digits */ |
585 | while (place > 0) | 587 | while (place > 0) { |
586 | DOPR_OUTCH(buffer, *currlen, maxlen, convert[--place]); | 588 | --place; |
589 | DOPR_OUTCH(buffer, *currlen, maxlen, convert[place]); | ||
590 | } | ||
587 | 591 | ||
588 | /* Left Justified spaces */ | 592 | /* Left Justified spaces */ |
589 | while (spadlen < 0) { | 593 | while (spadlen < 0) { |
@@ -788,8 +792,10 @@ fmtfp (char *buffer, size_t *currlen, size_t maxlen, | |||
788 | if (signvalue) | 792 | if (signvalue) |
789 | DOPR_OUTCH(buffer, *currlen, maxlen, signvalue); | 793 | DOPR_OUTCH(buffer, *currlen, maxlen, signvalue); |
790 | 794 | ||
791 | while (iplace > 0) | 795 | while (iplace > 0) { |
792 | DOPR_OUTCH(buffer, *currlen, maxlen, iconvert[--iplace]); | 796 | --iplace; |
797 | DOPR_OUTCH(buffer, *currlen, maxlen, iconvert[iplace]); | ||
798 | } | ||
793 | 799 | ||
794 | #ifdef DEBUG_SNPRINTF | 800 | #ifdef DEBUG_SNPRINTF |
795 | printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen); | 801 | printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen); |
@@ -807,9 +813,10 @@ fmtfp (char *buffer, size_t *currlen, size_t maxlen, | |||
807 | --zpadlen; | 813 | --zpadlen; |
808 | } | 814 | } |
809 | 815 | ||
810 | while (fplace > 0) | 816 | while (fplace > 0) { |
811 | DOPR_OUTCH(buffer, *currlen, maxlen, | 817 | --fplace; |
812 | fconvert[--fplace]); | 818 | DOPR_OUTCH(buffer, *currlen, maxlen, fconvert[fplace]); |
819 | } | ||
813 | } | 820 | } |
814 | 821 | ||
815 | while (padlen < 0) { | 822 | while (padlen < 0) { |
@@ -821,7 +828,7 @@ fmtfp (char *buffer, size_t *currlen, size_t maxlen, | |||
821 | #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ | 828 | #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ |
822 | 829 | ||
823 | #if !defined(HAVE_VSNPRINTF) | 830 | #if !defined(HAVE_VSNPRINTF) |
824 | static int | 831 | int |
825 | vsnprintf (char *str, size_t count, const char *fmt, va_list args) | 832 | vsnprintf (char *str, size_t count, const char *fmt, va_list args) |
826 | { | 833 | { |
827 | return dopr(str, count, fmt, args); | 834 | return dopr(str, count, fmt, args); |
@@ -829,8 +836,8 @@ vsnprintf (char *str, size_t count, const char *fmt, va_list args) | |||
829 | #endif | 836 | #endif |
830 | 837 | ||
831 | #if !defined(HAVE_SNPRINTF) | 838 | #if !defined(HAVE_SNPRINTF) |
832 | static int | 839 | int |
833 | snprintf(char *str, size_t count, const char *fmt, ...) | 840 | snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...) |
834 | { | 841 | { |
835 | size_t ret; | 842 | size_t ret; |
836 | va_list ap; | 843 | va_list ap; |