Module Name:    src
Committed By:   christos
Date:           Mon Jun 18 18:33:31 UTC 2018

Modified Files:
        src/usr.bin/patch: inp.c pch.c util.c util.h

Log Message:
Keep things portable (requested by joerg) by not depending on reallocarr
and instead doing the overflow check ourselves.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/usr.bin/patch/inp.c
cvs rdiff -u -r1.29 -r1.30 src/usr.bin/patch/pch.c
cvs rdiff -u -r1.27 -r1.28 src/usr.bin/patch/util.c
cvs rdiff -u -r1.12 -r1.13 src/usr.bin/patch/util.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/patch/inp.c
diff -u src/usr.bin/patch/inp.c:1.25 src/usr.bin/patch/inp.c:1.26
--- src/usr.bin/patch/inp.c:1.25	Fri Jun 15 20:40:14 2018
+++ src/usr.bin/patch/inp.c	Mon Jun 18 14:33:31 2018
@@ -1,7 +1,7 @@
 /*
  * $OpenBSD: inp.c,v 1.34 2006/03/11 19:41:30 otto Exp $
  * $DragonFly: src/usr.bin/patch/inp.c,v 1.6 2007/09/29 23:11:10 swildner Exp $
- * $NetBSD: inp.c,v 1.25 2018/06/16 00:40:14 christos Exp $
+ * $NetBSD: inp.c,v 1.26 2018/06/18 18:33:31 christos Exp $
  */
 
 /*
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: inp.c,v 1.25 2018/06/16 00:40:14 christos Exp $");
+__RCSID("$NetBSD: inp.c,v 1.26 2018/06/18 18:33:31 christos Exp $");
 
 #include <sys/types.h>
 #include <sys/file.h>
@@ -118,11 +118,12 @@ scan_input(const char *filename)
 static bool
 reallocate_lines(size_t *lines_allocated)
 {
+	char	**p;
 	size_t	new_size;
 
 	new_size = *lines_allocated * 3 / 2;
-	int res = reallocarr(&i_ptr, new_size + 2,  sizeof(char *));
-	if (res != 0) {	/* shucks, it was a near thing */
+	p = pch_realloc(i_ptr, new_size + 2,  sizeof(char *));
+	if (p == NULL) {	/* shucks, it was a near thing */
 		munmap(i_womp, i_size);
 		i_womp = NULL;
 		free(i_ptr);
@@ -131,6 +132,7 @@ reallocate_lines(size_t *lines_allocated
 		return false;
 	}
 	*lines_allocated = new_size;
+	i_ptr = p;
 	return true;
 }
 

Index: src/usr.bin/patch/pch.c
diff -u src/usr.bin/patch/pch.c:1.29 src/usr.bin/patch/pch.c:1.30
--- src/usr.bin/patch/pch.c:1.29	Thu Apr  5 14:50:10 2018
+++ src/usr.bin/patch/pch.c	Mon Jun 18 14:33:31 2018
@@ -1,7 +1,7 @@
 /*
  * $OpenBSD: pch.c,v 1.37 2007/09/02 15:19:33 deraadt Exp $
  * $DragonFly: src/usr.bin/patch/pch.c,v 1.6 2008/08/10 23:35:40 joerg Exp $
- * $NetBSD: pch.c,v 1.29 2018/04/05 18:50:10 christos Exp $
+ * $NetBSD: pch.c,v 1.30 2018/06/18 18:33:31 christos Exp $
  */
 
 /*
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pch.c,v 1.29 2018/04/05 18:50:10 christos Exp $");
+__RCSID("$NetBSD: pch.c,v 1.30 2018/06/18 18:33:31 christos Exp $");
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -156,15 +156,15 @@ grow_hunkmax(void)
 	if (p_line == NULL || p_len == NULL || p_char == NULL)
 		fatal("Internal memory allocation error\n");
 
-	new_p_line = realloc(p_line, new_hunkmax * sizeof(char *));
+	new_p_line = pch_realloc(p_line, new_hunkmax, sizeof(char *));
 	if (new_p_line == NULL)
 		free(p_line);
 
-	new_p_len = realloc(p_len, new_hunkmax * sizeof(short));
+	new_p_len = pch_realloc(p_len, new_hunkmax, sizeof(short));
 	if (new_p_len == NULL)
 		free(p_len);
 
-	new_p_char = realloc(p_char, new_hunkmax * sizeof(char));
+	new_p_char = pch_realloc(p_char, new_hunkmax, sizeof(char));
 	if (new_p_char == NULL)
 		free(p_char);
 

Index: src/usr.bin/patch/util.c
diff -u src/usr.bin/patch/util.c:1.27 src/usr.bin/patch/util.c:1.28
--- src/usr.bin/patch/util.c:1.27	Sat Nov  7 13:11:21 2015
+++ src/usr.bin/patch/util.c	Mon Jun 18 14:33:31 2018
@@ -1,7 +1,7 @@
 /*
  * $OpenBSD: util.c,v 1.32 2006/03/11 19:41:30 otto Exp $
  * $DragonFly: src/usr.bin/patch/util.c,v 1.9 2007/09/29 23:11:10 swildner Exp $
- * $NetBSD: util.c,v 1.27 2015/11/07 18:11:21 joerg Exp $
+ * $NetBSD: util.c,v 1.28 2018/06/18 18:33:31 christos Exp $
  */
 
 /*
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: util.c,v 1.27 2015/11/07 18:11:21 joerg Exp $");
+__RCSID("$NetBSD: util.c,v 1.28 2018/06/18 18:33:31 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -435,3 +435,13 @@ my_exit(int status)
 	unlink(TMPPATNAME);
 	exit(status);
 }
+
+void *
+pch_realloc(void *ptr, size_t number, size_t size)
+{
+	if (number > SIZE_MAX / size) {
+		errno = EOVERFLOW;
+		return NULL;
+	}
+	return realloc(ptr, number * size);
+}

Index: src/usr.bin/patch/util.h
diff -u src/usr.bin/patch/util.h:1.12 src/usr.bin/patch/util.h:1.13
--- src/usr.bin/patch/util.h:1.12	Tue Sep  6 14:25:14 2011
+++ src/usr.bin/patch/util.h	Mon Jun 18 14:33:31 2018
@@ -1,7 +1,7 @@
 /*
  * $OpenBSD: util.h,v 1.15 2005/06/20 07:14:06 otto Exp $
  * $DragonFly: src/usr.bin/patch/util.h,v 1.2 2007/09/29 23:11:10 swildner Exp $
- * $NetBSD: util.h,v 1.12 2011/09/06 18:25:14 joerg Exp $
+ * $NetBSD: util.h,v 1.13 2018/06/18 18:33:31 christos Exp $
  */
 
 /*
@@ -45,6 +45,7 @@ void		ignore_signals(void);
 void		makedirs(const char *, bool);
 void		version(void) __dead;
 void		my_exit(int) __dead;
+void		*pch_realloc(void *, size_t, size_t);
 
 /* in mkpath.c */
 extern int mkpath(char *);

Reply via email to