Skip to content

Commit

Permalink
Introduce SIXEL_ALLOCATE_BYTES_MAX macro and limit allocation size to…
Browse files Browse the repository at this point in the history
… 128MB(#74)
  • Loading branch information
saitoha committed Dec 23, 2019
1 parent 8f48e2e commit 0b1e0b3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/sixel.h.in
Expand Up @@ -39,6 +39,7 @@ typedef unsigned char sixel_index_t;
#define SIXEL_PALETTE_MIN 2
#define SIXEL_PALETTE_MAX 256
#define SIXEL_USE_DEPRECATED_SYMBOLS 1
#define SIXEL_ALLOCATE_BYTES_MAX 10248UL * 1024UL * 128UL /* up to 128M */

/* return value */
typedef int SIXELSTATUS;
Expand Down
29 changes: 29 additions & 0 deletions src/allocator.c
Expand Up @@ -152,6 +152,11 @@ sixel_allocator_malloc(
"sixel_allocator_malloc: called with n == 0");
return NULL;
}

if (n > SIXEL_ALLOCATE_BYTES_MAX) {
return NULL;
}

return allocator->fn_malloc(n);
}

Expand All @@ -163,10 +168,24 @@ sixel_allocator_calloc(
size_t /* in */ nelm, /* number of elements */
size_t /* in */ elsize) /* size of element */
{
size_t n;

/* precondition */
assert(allocator);
assert(allocator->fn_calloc);

n = nelm * elsize;

if (n == 0) {
sixel_helper_set_additional_message(
"sixel_allocator_malloc: called with n == 0");
return NULL;
}

if (n > SIXEL_ALLOCATE_BYTES_MAX) {
return NULL;
}

return allocator->fn_calloc(nelm, elsize);
}

Expand All @@ -182,6 +201,16 @@ sixel_allocator_realloc(
assert(allocator);
assert(allocator->fn_realloc);

if (n == 0) {
sixel_helper_set_additional_message(
"sixel_allocator_malloc: called with n == 0");
return NULL;
}

if (n > SIXEL_ALLOCATE_BYTES_MAX) {
return NULL;
}

return allocator->fn_realloc(p, n);
}

Expand Down

0 comments on commit 0b1e0b3

Please sign in to comment.