jcs's openbsd hax
openbsd

Fix not accounting for NUL for allocation size and move to reallocarray while there. ok deraadt@ millert@

otto c0cf3824 cc381e73

+7 -7
+7 -7
usr.bin/fmt/fmt.c
··· 1 - /* $OpenBSD: fmt.c,v 1.38 2017/02/20 15:48:00 schwarze Exp $ */ 1 + /* $OpenBSD: fmt.c,v 1.39 2018/10/18 05:04:52 otto Exp $ */ 2 2 /* 3 3 * This file is a derived work. 4 4 * The changes are covered by the following Copyright and license: ··· 245 245 static void output_indent(size_t); 246 246 static void center_stream(FILE *, const char *); 247 247 static char *get_line(FILE *); 248 - static void *xrealloc(void *, size_t); 248 + static void *xreallocarray(void *, size_t, size_t); 249 249 void usage(void); 250 250 251 251 #define ERRS(x) (x >= 127 ? 127 : ++x) ··· 680 680 681 681 if (buf == NULL) { 682 682 length = 100; 683 - buf = xrealloc(NULL, length); 683 + buf = xreallocarray(NULL, length, 1); 684 684 } 685 685 686 686 while ((ch = getc(stream)) != '\n' && ch != EOF) { 687 687 if ((len == 0) && (ch == '.' && !format_troff)) 688 688 troff = 1; 689 689 if (troff || ch == '\t' || !iscntrl(ch)) { 690 - if (len >= length) { 690 + if (len >= length - 1) { 691 + buf = xreallocarray(buf, length, 2); 691 692 length *= 2; 692 - buf = xrealloc(buf, length); 693 693 } 694 694 buf[len++] = ch; 695 695 } else if (ch == '\b') { ··· 706 706 /* (Re)allocate some memory, exiting with an error if we can't. 707 707 */ 708 708 static void * 709 - xrealloc(void *ptr, size_t nbytes) 709 + xreallocarray(void *ptr, size_t nmemb, size_t size) 710 710 { 711 711 void *p; 712 712 713 - p = realloc(ptr, nbytes); 713 + p = reallocarray(ptr, nmemb, size); 714 714 if (p == NULL) 715 715 errx(1, "out of memory"); 716 716 return p;