Permalink
Browse files

use calloc() to avoid malloc(n * m) overflows; checked by djm canacar…

… jsg
  • Loading branch information...
Theo de Raadt authored and howardjp committed Sep 2, 2007
1 parent 4db0397 commit 69dbfbbf623beef0e274aa79a05f5597ba16dd38
Showing with 16 additions and 5 deletions.
  1. +3 −3 grep.c
  2. +2 −1 grep.h
  3. +11 −1 util.c
View
6 grep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grep.c,v 1.37 2006/11/02 18:00:03 ray Exp $ */
+/* $OpenBSD: grep.c,v 1.38 2007/02/13 21:48:20 kili Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -450,8 +450,8 @@ main(int argc, char *argv[])
if (Eflag)
cflags |= REG_EXTENDED;
- fg_pattern = grep_malloc(patterns * sizeof(*fg_pattern));
- r_pattern = grep_malloc(patterns * sizeof(*r_pattern));
+ fg_pattern = grep_calloc(patterns, sizeof(*fg_pattern));
+ r_pattern = grep_calloc(patterns, sizeof(*r_pattern));
for (i = 0; i < patterns; ++i) {
/* Check if cheating is allowed (always is for fgrep). */
if (Fflag) {
View
3 grep.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: grep.h,v 1.12 2004/10/03 19:23:02 otto Exp $ */
+/* $OpenBSD: grep.h,v 1.13 2006/02/09 09:54:47 otto Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -82,6 +82,7 @@ extern char re_error[RE_ERROR_BUF + 1]; /* Seems big enough */
int procfile(char *fn);
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 printline(str_t *line, int sep);
int fastcomp(fastgrep_t *, const char *);
View
12 util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.33 2006/11/17 02:01:29 jaredy Exp $ */
+/* $OpenBSD: util.c,v 1.34 2006/12/26 20:59:23 otto Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -512,6 +512,16 @@ grep_malloc(size_t size)
return ptr;
}
+void *
+grep_calloc(size_t nmemb, size_t size)
+{
+ void *ptr;
+
+ if ((ptr = calloc(nmemb, size)) == NULL)
+ err(2, "calloc");
+ return ptr;
+}
+
void *
grep_realloc(void *ptr, size_t size)
{

0 comments on commit 69dbfbb

Please sign in to comment.