From 411e0a694f3ed02e1d98abe923601d511bc15b59 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 27 Jul 2021 16:01:30 +0200 Subject: Add ALLOCA_LIMIT to fix bug #13 on GitHub reported by Justin Gottula MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- common.h | 19 +++++++++++++------ libkeccak_generalised_sum_fd.c | 20 +++++++++++++++++--- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/common.h b/common.h index bf605f5..4953da2 100644 --- a/common.h +++ b/common.h @@ -2,13 +2,20 @@ #include "libkeccak.h" +#ifndef ALLOCA_LIMIT +# define ALLOCA_LIMIT (16UL << 10) +#endif + + #include -#if defined(__GLIBC__) || defined(__sun) || defined(__CYGWIN__) -# include -#elif defined(_WIN32) -# include -# if !defined(alloca) -# define alloca _alloca /* For clang with MS Codegen */ +#if ALLOCA_LIMIT > 0 +# if defined(__GLIBC__) || defined(__sun) || defined(__CYGWIN__) +# include +# elif defined(_WIN32) +# include +# if !defined(alloca) +# define alloca _alloca /* For clang with MS Codegen */ +# endif # endif #endif #include diff --git a/libkeccak_generalised_sum_fd.c b/libkeccak_generalised_sum_fd.c index 879371b..4aa7a94 100644 --- a/libkeccak_generalised_sum_fd.c +++ b/libkeccak_generalised_sum_fd.c @@ -33,8 +33,16 @@ libkeccak_generalised_sum_fd(int fd, struct libkeccak_state *restrict state, con if (attr.st_blksize > 0) blksize = (size_t)attr.st_blksize; #endif - + +#if ALLOCA_LIMIT > 0 + if (blksize > (size_t)ALLOCA_LIMIT) + blksize = (size_t)ALLOCA_LIMIT; chunk = alloca(blksize); +#else + chunk = malloc(blksize); + if (!chunk) + return -1; +#endif for (;;) { got = read(fd, chunk, blksize); @@ -43,11 +51,17 @@ libkeccak_generalised_sum_fd(int fd, struct libkeccak_state *restrict state, con break; if (errno == EINTR) continue; - return -1; + goto fail; } if (libkeccak_fast_update(state, chunk, (size_t)got) < 0) - return -1; + goto fail; } return libkeccak_fast_digest(state, NULL, 0, 0, suffix, hashsum); + +fail: +#if ALLOCA_LIMIT <= 0 + free(chunk); +#endif + return -1; } -- cgit v1.2.3-70-g09d2