diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2014-12-01 06:36:05 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2014-12-01 06:36:05 +0000 |
commit | d64ce1902e872b6a2e9d67fc497577d94f69efe3 (patch) | |
tree | c806001a8fab519cb317d6f318d99cc38abf6503 /usr.bin | |
parent | 0ef94e6e3f97eca0d5d8834bea5c2e0448af5bc2 (diff) |
use reallocarray()
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/grep/grep.c | 7 | ||||
-rw-r--r-- | usr.bin/grep/grep.h | 3 | ||||
-rw-r--r-- | usr.bin/grep/util.c | 10 |
3 files changed, 15 insertions, 5 deletions
diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c index 89830da8d44..8d844564cd2 100644 --- a/usr.bin/grep/grep.c +++ b/usr.bin/grep/grep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.c,v 1.47 2014/11/26 18:34:51 millert Exp $ */ +/* $OpenBSD: grep.c,v 1.48 2014/12/01 06:36:04 deraadt Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -174,7 +174,7 @@ add_pattern(char *pat, size_t len) } if (patterns == pattern_sz) { pattern_sz *= 2; - pattern = grep_realloc(pattern, ++pattern_sz * sizeof(*pattern)); + pattern = grep_reallocarray(pattern, ++pattern_sz, sizeof(*pattern)); } if (len > 0 && pat[len - 1] == '\n') --len; @@ -358,7 +358,8 @@ main(int argc, char *argv[]) /* defer adding of expressions until all arguments are parsed */ if (exprs == expr_sz) { expr_sz *= 2; - expr = grep_realloc(expr, ++expr_sz * sizeof(*expr)); + expr = grep_reallocarray(expr, ++expr_sz, + sizeof(*expr)); } needpattern = 0; expr[exprs] = optarg; diff --git a/usr.bin/grep/grep.h b/usr.bin/grep/grep.h index 63d3dacb4a6..954844bdc01 100644 --- a/usr.bin/grep/grep.h +++ b/usr.bin/grep/grep.h @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.h,v 1.19 2013/11/26 13:21:16 deraadt Exp $ */ +/* $OpenBSD: grep.h,v 1.20 2014/12/01 06:36:04 deraadt Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -84,6 +84,7 @@ int grep_tree(char **argv); void *grep_malloc(size_t size); void *grep_calloc(size_t nmemb, size_t size); void *grep_realloc(void *ptr, size_t size); +void *grep_reallocarray(void *ptr, size_t nmemb, size_t size); void printline(str_t *line, int sep, regmatch_t *pmatch); int fastcomp(fastgrep_t *, const char *); void fgrepcomp(fastgrep_t *, const unsigned char *); diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index bc132f57746..c4b945d4b36 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.48 2014/05/20 01:25:23 guenther Exp $ */ +/* $OpenBSD: util.c,v 1.49 2014/12/01 06:36:04 deraadt Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -568,6 +568,14 @@ grep_realloc(void *ptr, size_t size) return ptr; } +void * +grep_reallocarray(void *ptr, size_t nmemb, size_t size) +{ + if ((ptr = reallocarray(ptr, nmemb, size)) == NULL) + err(2, "reallocarray"); + return ptr; +} + #ifndef SMALL /* * Returns: i >= 0 on failure (position that it failed) |