Skip to content

Commit

Permalink
fix: fixed malloc() call overflow for large matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregungory committed Jan 15, 2021
1 parent c4038b0 commit 60b4446
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/util/cmatrix.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef lint
static const char RCSid[] = "$Id: cmatrix.c,v 2.28 2020/03/30 20:41:47 greg Exp $";
static const char RCSid[] = "$Id: cmatrix.c,v 2.29 2021/01/15 02:46:28 greg Exp $";
#endif
/*
* Color matrix routines.
Expand Down Expand Up @@ -33,7 +33,7 @@ cm_alloc(int nrows, int ncols)
if ((nrows <= 0) | (ncols <= 0))
error(USER, "attempt to create empty matrix");
cm = (CMATRIX *)malloc(sizeof(CMATRIX) +
sizeof(COLOR)*(nrows*ncols - 1));
sizeof(COLOR)*((size_t)nrows*ncols - 1));
if (!cm)
error(SYSTEM, "out of memory in cm_alloc()");
cm->nrows = nrows;
Expand Down
4 changes: 2 additions & 2 deletions src/util/rmatrix.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef lint
static const char RCSid[] = "$Id: rmatrix.c,v 2.44 2020/05/07 18:45:16 greg Exp $";
static const char RCSid[] = "$Id: rmatrix.c,v 2.45 2021/01/15 02:46:28 greg Exp $";
#endif
/*
* General matrix operations.
Expand All @@ -24,7 +24,7 @@ rmx_alloc(int nr, int nc, int n)
if ((nr <= 0) | (nc <= 0) | (n <= 0))
return(NULL);
dnew = (RMATRIX *)malloc(sizeof(RMATRIX)-sizeof(dnew->mtx) +
sizeof(dnew->mtx[0])*(n*nr*nc));
sizeof(dnew->mtx[0])*n*nr*nc);
if (!dnew)
return(NULL);
dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n;
Expand Down

0 comments on commit 60b4446

Please sign in to comment.