Skip to content

Commit

Permalink
Prevent overflow by large arguments of opj_calloc
Browse files Browse the repository at this point in the history
calloc allocates (num * size) bytes, possibly without handling an
overflow when doing the multiplication. This is fatal because the
allocated memory would be too small in that case.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed Feb 28, 2024
1 parent 1b72e85 commit 72ecce4
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/lib/openjp2/opj_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define OPJ_SKIP_POISON
#include "opj_includes.h"

#include <errno.h>

#if defined(OPJ_HAVE_MALLOC_H) && defined(OPJ_HAVE_MEMALIGN)
# include <malloc.h>
#endif
Expand Down Expand Up @@ -201,6 +203,11 @@ void * opj_calloc(size_t num, size_t size)
/* prevent implementation defined behavior of realloc */
return NULL;
}
if (num > SIZE_MAX / size) {
/* prevent overflow */
errno = ENOMEM;
return NULL;
}
return calloc(num, size);
}

Expand Down

0 comments on commit 72ecce4

Please sign in to comment.