Keep things portable (requested by joerg) by not depending on reallocarr

and instead doing the overflow check ourselves.
This commit is contained in:
christos 2018-06-18 18:33:31 +00:00
parent 4f9e5a443e
commit a2ee17b347
4 changed files with 25 additions and 12 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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 *);