summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2003-09-28 07:55:19 +0000
committerotto <otto@openbsd.org>2003-09-28 07:55:19 +0000
commit1287dded2dca5fb0b3c5474177808438e18c3a13 (patch)
tree077756618f3a166ce4f650d26772952e811b39a6
parentFix function return types. Unbreaks sparc64 compilation. Spotted by (diff)
downloadwireguard-openbsd-1287dded2dca5fb0b3c5474177808438e18c3a13.tar.xz
wireguard-openbsd-1287dded2dca5fb0b3c5474177808438e18c3a13.zip
realloc(3) cleanup.
ok cloder@ tedu@
-rw-r--r--usr.bin/patch/inp.c13
-rw-r--r--usr.bin/patch/pch.c34
2 files changed, 35 insertions, 12 deletions
diff --git a/usr.bin/patch/inp.c b/usr.bin/patch/inp.c
index 761ee8d4356..3e6464234b7 100644
--- a/usr.bin/patch/inp.c
+++ b/usr.bin/patch/inp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: inp.c,v 1.28 2003/08/15 08:00:51 otto Exp $ */
+/* $OpenBSD: inp.c,v 1.29 2003/09/28 07:55:19 otto Exp $ */
/*
* patch - a program to apply diffs to original files
@@ -27,7 +27,7 @@
*/
#ifndef lint
-static const char rcsid[] = "$OpenBSD: inp.c,v 1.28 2003/08/15 08:00:51 otto Exp $";
+static const char rcsid[] = "$OpenBSD: inp.c,v 1.29 2003/09/28 07:55:19 otto Exp $";
#endif /* not lint */
#include <sys/types.h>
@@ -111,17 +111,20 @@ scan_input(const char *filename)
static bool
reallocate_lines(size_t *lines_allocated)
{
- char **p;
+ char **p;
+ size_t new_size;
- *lines_allocated = *lines_allocated * 3 / 2;
- p = realloc(i_ptr, (*lines_allocated + 2) * sizeof(char *));
+ new_size = *lines_allocated * 3 / 2;
+ p = 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);
i_ptr = NULL;
+ *lines_allocated = 0;
return false;
}
+ *lines_allocated = new_size;
i_ptr = p;
return true;
}
diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c
index 97c0e0f74f6..1da80fa07f8 100644
--- a/usr.bin/patch/pch.c
+++ b/usr.bin/patch/pch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: pch.c,v 1.30 2003/08/15 08:00:51 otto Exp $ */
+/* $OpenBSD: pch.c,v 1.31 2003/09/28 07:55:19 otto Exp $ */
/*
* patch - a program to apply diffs to original files
@@ -27,7 +27,7 @@
*/
#ifndef lint
-static const char rcsid[] = "$OpenBSD: pch.c,v 1.30 2003/08/15 08:00:51 otto Exp $";
+static const char rcsid[] = "$OpenBSD: pch.c,v 1.31 2003/09/28 07:55:19 otto Exp $";
#endif /* not lint */
#include <sys/types.h>
@@ -138,17 +138,37 @@ set_hunkmax(void)
static void
grow_hunkmax(void)
{
- hunkmax *= 2;
+ int new_hunkmax;
+ char **new_p_line;
+ short *new_p_len;
+ char *new_p_char;
+
+ new_hunkmax = hunkmax * 2;
if (p_line == NULL || p_len == NULL || p_char == NULL)
fatal("Internal memory allocation error\n");
- p_line = realloc(p_line, hunkmax * sizeof(char *));
- p_len = realloc(p_len, hunkmax * sizeof(short));
- p_char = realloc(p_char, hunkmax * sizeof(char));
+ new_p_line = realloc(p_line, new_hunkmax * sizeof(char *));
+ if (new_p_line == NULL)
+ free(p_line);
- if (p_line != NULL && p_len != NULL && p_char != NULL)
+ new_p_len = 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));
+ if (new_p_char == NULL)
+ free(p_char);
+
+ p_char = new_p_char;
+ p_len = new_p_len;
+ p_line = new_p_line;
+
+ if (p_line != NULL && p_len != NULL && p_char != NULL) {
+ hunkmax = new_hunkmax;
return;
+ }
+
if (!using_plan_a)
fatal("out of memory\n");
out_of_mem = true; /* whatever is null will be allocated again */