From a4f8e94bc1f61e78a46cad0d7b6c92c39243816b Mon Sep 17 00:00:00 2001 From: Lev Walkin Date: Sun, 8 Oct 2017 19:28:20 -0700 Subject: [PATCH] get rid of alloca() in compiler --- asn1c/asn1c.c | 10 +++---- asn1c/sys-common.h | 3 --- configure.ac | 3 +-- libasn1compiler/asn1c_C.c | 5 +++- libasn1compiler/asn1c_compat.c | 26 ++++++++----------- libasn1compiler/asn1c_compat.h | 4 +-- libasn1compiler/asn1c_fdeps.c | 13 +++++++--- libasn1compiler/asn1c_internal.h | 4 --- libasn1compiler/asn1c_save.c | 9 +++---- .../check-src/check-22.-fwide-types.c | 7 ++--- .../check-src/check-24.-fwide-types.c | 7 ++--- .../check-src/check-25.-fwide-types.c | 18 +++++-------- .../check-src/check-30.-fwide-types.c | 7 ++--- .../check-src/check-31.-fwide-types.c | 7 ++--- tests/tests-c-compiler/check-src/check-35.c | 9 ++++--- .../check-src/check-41.-fwide-types.c | 16 +++++++----- tests/tests-c-compiler/check-src/check-41.c | 16 +++++++----- tests/tests-c-compiler/check-src/check-42.c | 3 ++- 18 files changed, 76 insertions(+), 91 deletions(-) diff --git a/asn1c/asn1c.c b/asn1c/asn1c.c index 1d7c6b68..aecf111f 100644 --- a/asn1c/asn1c.c +++ b/asn1c/asn1c.c @@ -226,7 +226,7 @@ main(int ac, char **av) { ac -= optind; av += optind; } else { - char *bin_name = a1c_basename(av[0]); + const char *bin_name = a1c_basename(av[0]); fprintf(stderr, "%s: No input files specified. " "Try '%s -h' for more information\n", @@ -247,15 +247,15 @@ main(int ac, char **av) { * compute it from my file name: * ./asn1c/asn1c -> ./skeletons */ - char *p; + const char *skel_dir; size_t len; - p = a1c_dirname(av[-optind]); + skel_dir = a1c_dirname(av[-optind]); - len = strlen(p) + sizeof("/../skeletons"); + len = strlen(skel_dir) + sizeof("/../skeletons"); skeletons_dir = malloc(len); assert(skeletons_dir); - snprintf(skeletons_dir, len, "%s/../skeletons", p); + snprintf(skeletons_dir, len, "%s/../skeletons", skel_dir); if(stat(skeletons_dir, &sb)) { fprintf(stderr, "WARNING: skeletons are neither in " diff --git a/asn1c/sys-common.h b/asn1c/sys-common.h index 9a9ea96e..abbfd8f8 100644 --- a/asn1c/sys-common.h +++ b/asn1c/sys-common.h @@ -64,8 +64,5 @@ #ifndef EX_OSFILE #define EX_OSFILE 72 #endif -#if defined HAVE_DECL_ALLOCA && !HAVE_DECL_ALLOCA -#define alloca _alloca -#endif #define snprintf _snprintf #endif /* _WIN32 */ diff --git a/configure.ac b/configure.ac index 88b73575..3d8b8d56 100644 --- a/configure.ac +++ b/configure.ac @@ -227,7 +227,6 @@ AC_SUBST(ASAN_ENV_FLAGS) dnl Checks for header files. AC_HEADER_STDC AC_CHECK_HEADERS(sys/param.h) -AC_CHECK_HEADERS(alloca.h) dnl Checks for typedefs, structures, and compiler characteristics. AC_C_BIGENDIAN @@ -249,7 +248,7 @@ AC_CHECK_FUNCS(strtoimax strtoll) AC_CHECK_FUNCS(mergesort) AC_CHECK_FUNCS(mkstemps) AC_CHECK_FUNCS(timegm) -AC_CHECK_DECLS(alloca strcasecmp) +AC_CHECK_DECLS(strcasecmp) AC_CHECK_DECLS(vasprintf) AC_TRY_LINK_FUNC([symlink],[AC_DEFINE([HAVE_SYMLINK], 1, [Define to 1 if you have the symlink function.])]) diff --git a/libasn1compiler/asn1c_C.c b/libasn1compiler/asn1c_C.c index 009f91cb..539f7920 100644 --- a/libasn1compiler/asn1c_C.c +++ b/libasn1compiler/asn1c_C.c @@ -132,7 +132,8 @@ asn1c_lang_C_type_common_INTEGER(arg_t *arg) { int eidx; int saved_target = arg->target->target; - v2e = alloca((el_count + 1) * sizeof(*v2e)); + v2e = calloc(el_count + 1, sizeof(*v2e)); + assert(v2e); /* * For all ENUMERATED types and for those INTEGER types which @@ -162,6 +163,7 @@ asn1c_lang_C_type_common_INTEGER(arg_t *arg) { map_extensions = eidx + 1; break; default: + free(v2e); return -1; } } @@ -258,6 +260,7 @@ asn1c_lang_C_type_common_INTEGER(arg_t *arg) { REDIR(saved_target); + free(v2e); return asn1c_lang_C_type_SIMPLE_TYPE(arg); } diff --git a/libasn1compiler/asn1c_compat.c b/libasn1compiler/asn1c_compat.c index 4bd70f31..ae0f475f 100644 --- a/libasn1compiler/asn1c_compat.c +++ b/libasn1compiler/asn1c_compat.c @@ -1,8 +1,8 @@ #include "asn1c_internal.h" #include "asn1c_compat.h" -#ifndef MAXPATHLEN -#define MAXPATHLEN 1024 +#ifndef PATH_MAX +#define PATH_MAX 1024 #endif /* Normally file permissions are (DEFFILEMODE & ~umask(2)) */ @@ -41,12 +41,11 @@ int mkstemp(char *template) { FILE * asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) { + char fname[PATH_MAX]; int created = 1; #ifndef _WIN32 struct stat sb; #endif - char *fname; - size_t len; FILE *fp; int ret; int fd; @@ -54,13 +53,11 @@ asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) { /* * Compute filenames. */ - len = strlen(name) + strlen(ext) + sizeof(".XXXXXX"); - fname = alloca(len); - ret = snprintf(fname, len, "%s%s%s", name, ext, - opt_tmpname ? ".XXXXXX" : ""); - assert(ret > 0 && ret < (ssize_t)len); + ret = snprintf(fname, sizeof(fname), "%s%s%s", name, ext, + opt_tmpname ? ".XXXXXX" : ""); + assert(ret > 0 && ret < (ssize_t)sizeof(fname)); - if(opt_tmpname) { + if(opt_tmpname) { /* * Create temporary file. */ @@ -130,10 +127,9 @@ asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) { return fp; } - -char * +const char * a1c_basename(const char *path) { - static char strbuf[MAXPATHLEN]; + static char strbuf[PATH_MAX]; const char *pend; const char *name; @@ -165,9 +161,9 @@ a1c_basename(const char *path) { } -char * +const char * a1c_dirname(const char *path) { - static char strbuf[MAXPATHLEN]; + static char strbuf[PATH_MAX]; const char *pend; const char *last = 0; int in_slash = 0; diff --git a/libasn1compiler/asn1c_compat.h b/libasn1compiler/asn1c_compat.h index 8aa38654..b0be029f 100644 --- a/libasn1compiler/asn1c_compat.h +++ b/libasn1compiler/asn1c_compat.h @@ -14,7 +14,7 @@ FILE *asn1c_open_file(const char *base_part, const char *extension, * Obtain base name and directory name of a path. * Some systems have them in as dirname(3) and basename(3). */ -char *a1c_basename(const char *path); -char *a1c_dirname(const char *path); +const char *a1c_basename(const char *path); +const char *a1c_dirname(const char *path); #endif /* ASN1C_COMPAT_H */ diff --git a/libasn1compiler/asn1c_fdeps.c b/libasn1compiler/asn1c_fdeps.c index de9cd760..18dd6377 100644 --- a/libasn1compiler/asn1c_fdeps.c +++ b/libasn1compiler/asn1c_fdeps.c @@ -1,11 +1,16 @@ #include "asn1c_internal.h" #include "asn1c_fdeps.h" +#ifndef PATH_MAX +#define PATH_MAX 1024 +#endif + static asn1c_fdeps_t *asn1c_new_dep(const char *filename); static int asn1c_dep_add(asn1c_fdeps_t *deps, asn1c_fdeps_t *d); int asn1c_activate_dependency(asn1c_fdeps_t *deps, asn1c_fdeps_t *cur, const char *data) { + char fname_scratch[PATH_MAX]; const char *fname; int i; @@ -30,10 +35,10 @@ asn1c_activate_dependency(asn1c_fdeps_t *deps, asn1c_fdeps_t *cur, const char *d end = strchr(start, '\"'); } if(end) { - char *p = alloca((end - start) + 1); - memcpy(p, start, end - start); - p[end-start] = '\0'; - fname = p; + assert((end-start) + 1 < (ssize_t)sizeof(fname_scratch)); + memcpy(fname_scratch, start, end - start); + fname_scratch[end-start] = '\0'; + fname = fname_scratch; } else { return 0; } diff --git a/libasn1compiler/asn1c_internal.h b/libasn1compiler/asn1c_internal.h index 1ac31668..e74371c4 100644 --- a/libasn1compiler/asn1c_internal.h +++ b/libasn1compiler/asn1c_internal.h @@ -35,10 +35,6 @@ #endif #include /* for open(2) */ -#ifdef HAVE_SYS_PARAM_H -#include /* For MAXPATHLEN */ -#endif - #include "asn1compiler.h" #include "asn1_namespace.h" diff --git a/libasn1compiler/asn1c_save.c b/libasn1compiler/asn1c_save.c index cf9c2e87..41601af4 100644 --- a/libasn1compiler/asn1c_save.c +++ b/libasn1compiler/asn1c_save.c @@ -302,7 +302,7 @@ asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) { out_chunk_t *ot; FILE *fp_c, *fp_h; char *tmpname_c, *tmpname_h; - char *name_buf; + char name_buf[FILENAME_MAX]; const char *header_id; const char *c_retained = ""; const char *h_retained = ""; @@ -379,9 +379,9 @@ asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) { fclose(fp_c); fclose(fp_h); - name_buf = alloca(strlen(filename) + 3); + int ret = snprintf(name_buf, sizeof(name_buf), "%s.c", filename); + assert(ret > 0 && ret < (ssize_t)sizeof(name_buf)); - sprintf(name_buf, "%s.c", filename); if(identical_files(name_buf, tmpname_c)) { c_retained = " (contents unchanged)"; unlink(tmpname_c); @@ -521,14 +521,13 @@ real_copy(const char *src, const char *dst) { static int asn1c_copy_over(arg_t *arg, char *path) { - char *fname; #ifdef _WIN32 int use_real_copy = 1; #else int use_real_copy = !(arg->flags & A1C_LINK_SKELETONS); #endif - fname = a1c_basename(path); + const char *fname = a1c_basename(path); if(!fname || (use_real_copy ? real_copy(path, fname) : symlink(path, fname)) ) { diff --git a/tests/tests-c-compiler/check-src/check-22.-fwide-types.c b/tests/tests-c-compiler/check-src/check-22.-fwide-types.c index 991f2626..2b3a5429 100644 --- a/tests/tests-c-compiler/check-src/check-22.-fwide-types.c +++ b/tests/tests-c-compiler/check-src/check-22.-fwide-types.c @@ -117,14 +117,11 @@ check(int is_ok, uint8_t *buf, size_t size, size_t consumed) { static void try_corrupt(uint8_t *buf, size_t size) { - uint8_t *tmp; - int i; + uint8_t tmp[size]; fprintf(stderr, "\nCorrupting...\n"); - tmp = alloca(size); - - for(i = 0; i < 1000; i++) { + for(int i = 0; i < 1000; i++) { int loc; memcpy(tmp, buf, size); diff --git a/tests/tests-c-compiler/check-src/check-24.-fwide-types.c b/tests/tests-c-compiler/check-src/check-24.-fwide-types.c index 8cd54765..aad7fb84 100644 --- a/tests/tests-c-compiler/check-src/check-24.-fwide-types.c +++ b/tests/tests-c-compiler/check-src/check-24.-fwide-types.c @@ -62,14 +62,11 @@ check(int is_ok, uint8_t *buf, size_t size, size_t consumed) { static void try_corrupt(uint8_t *buf, size_t size) { - uint8_t *tmp; - int i; + uint8_t tmp[size]; fprintf(stderr, "\nCorrupting...\n"); - tmp = alloca(size); - - for(i = 0; i < 1000; i++) { + for(int i = 0; i < 1000; i++) { int loc; memcpy(tmp, buf, size); diff --git a/tests/tests-c-compiler/check-src/check-25.-fwide-types.c b/tests/tests-c-compiler/check-src/check-25.-fwide-types.c index 41a90bfe..fda63c33 100644 --- a/tests/tests-c-compiler/check-src/check-25.-fwide-types.c +++ b/tests/tests-c-compiler/check-src/check-25.-fwide-types.c @@ -141,14 +141,11 @@ check(int is_ok, uint8_t *buf, size_t size, size_t consumed) { static void try_corrupt(uint8_t *buf, size_t size, int allow_consume) { - uint8_t *tmp; - int i; + uint8_t tmp[size]; fprintf(stderr, "\nCorrupting...\n"); - tmp = alloca(size); - - for(i = 0; i < 1000; i++) { + for(int i = 0; i < 1000; i++) { int loc; memcpy(tmp, buf, size); @@ -173,10 +170,9 @@ static void partial_read(uint8_t *buf, size_t size) { T_t t, *tp; asn_dec_rval_t rval; - size_t i1, i2; - uint8_t *tbuf1 = alloca(size); - uint8_t *tbuf2 = alloca(size); - uint8_t *tbuf3 = alloca(size); + uint8_t tbuf1[size]; + uint8_t tbuf2[size]; + uint8_t tbuf3[size]; fprintf(stderr, "\nPartial read sequence...\n"); @@ -186,8 +182,8 @@ partial_read(uint8_t *buf, size_t size) { * ^ buf ^ buf+size * Try to read block by block. */ - for(i1 = 0; i1 < size; i1++) { - for(i2 = i1; i2 < size; i2++) { + for(size_t i1 = 0; i1 < size; i1++) { + for(size_t i2 = i1; i2 < size; i2++) { uint8_t *chunk1 = buf; size_t size1 = i1; uint8_t *chunk2 = buf + size1; diff --git a/tests/tests-c-compiler/check-src/check-30.-fwide-types.c b/tests/tests-c-compiler/check-src/check-30.-fwide-types.c index e4a47371..18430809 100644 --- a/tests/tests-c-compiler/check-src/check-30.-fwide-types.c +++ b/tests/tests-c-compiler/check-src/check-30.-fwide-types.c @@ -216,14 +216,11 @@ check_xer(uint8_t *buf, uint8_t size, char *xer_sample) { static void try_corrupt(uint8_t *buf, size_t size) { - uint8_t *tmp; - int i; + uint8_t tmp[size]; fprintf(stderr, "\nCorrupting...\n"); - tmp = alloca(size); - - for(i = 0; i < 1000; i++) { + for(int i = 0; i < 1000; i++) { int loc; memcpy(tmp, buf, size); diff --git a/tests/tests-c-compiler/check-src/check-31.-fwide-types.c b/tests/tests-c-compiler/check-src/check-31.-fwide-types.c index 5cc11e3c..a6bd7728 100644 --- a/tests/tests-c-compiler/check-src/check-31.-fwide-types.c +++ b/tests/tests-c-compiler/check-src/check-31.-fwide-types.c @@ -178,14 +178,11 @@ check_xer(uint8_t *buf, uint8_t size, char *xer_sample) { static void try_corrupt(uint8_t *buf, size_t size) { - uint8_t *tmp; - int i; + uint8_t tmp[size]; fprintf(stderr, "\nCorrupting...\n"); - tmp = alloca(size); - - for(i = 0; i < 1000; i++) { + for(int i = 0; i < 1000; i++) { int loc; memcpy(tmp, buf, size); diff --git a/tests/tests-c-compiler/check-src/check-35.c b/tests/tests-c-compiler/check-src/check-35.c index edc2ea2d..f4e797c6 100644 --- a/tests/tests-c-compiler/check-src/check-35.c +++ b/tests/tests-c-compiler/check-src/check-35.c @@ -172,7 +172,8 @@ compare(T_t *tp, uint8_t *cmp_buf, size_t cmp_buf_size) { size_t i; buf_size = cmp_buf_size + 100; - buf = alloca(buf_size); + uint8_t scratch[buf_size]; + buf = scratch; buf_pos = 0; /* @@ -202,9 +203,9 @@ partial_read(uint8_t *data, size_t size) { T_t t, *tp; asn_dec_rval_t rval; size_t i1, i2; - uint8_t *data1 = alloca(size); - uint8_t *data2 = alloca(size); - uint8_t *data3 = alloca(size); + uint8_t data1[size]; + uint8_t data2[size]; + uint8_t data3[size]; fprintf(stderr, "\nPartial read sequence...\n"); diff --git a/tests/tests-c-compiler/check-src/check-41.-fwide-types.c b/tests/tests-c-compiler/check-src/check-41.-fwide-types.c index 6e21cf12..98741f51 100644 --- a/tests/tests-c-compiler/check-src/check-41.-fwide-types.c +++ b/tests/tests-c-compiler/check-src/check-41.-fwide-types.c @@ -188,7 +188,8 @@ compare(T_t *tp, uint8_t *cmp_buf, size_t cmp_buf_size) { size_t i; buf_size = cmp_buf_size + 100; - buffer = alloca(buf_size); + uint8_t scratch[buf_size]; + buffer = scratch; buf_pos = 0; /* @@ -211,16 +212,17 @@ compare(T_t *tp, uint8_t *cmp_buf, size_t cmp_buf_size) { } assert(buffer[i] == cmp_buf[i]); } + + buffer = 0; } static void partial_read(uint8_t *data, size_t size) { T_t t, *tp; asn_dec_rval_t rval; - size_t i1, i2; - uint8_t *data1 = alloca(size); - uint8_t *data2 = alloca(size); - uint8_t *data3 = alloca(size); + uint8_t data1[size]; + uint8_t data2[size]; + uint8_t data3[size]; fprintf(stderr, "\nPartial read sequence...\n"); @@ -230,8 +232,8 @@ partial_read(uint8_t *data, size_t size) { * ^ data ^ data+size * Try to read block by block. */ - for(i1 = 0; i1 < size; i1++) { - for(i2 = i1; i2 < size; i2++) { + for(size_t i1 = 0; i1 < size; i1++) { + for(size_t i2 = i1; i2 < size; i2++) { uint8_t *chunk1 = data; size_t size1 = i1; uint8_t *chunk2 = data + size1; diff --git a/tests/tests-c-compiler/check-src/check-41.c b/tests/tests-c-compiler/check-src/check-41.c index e0768949..483229e8 100644 --- a/tests/tests-c-compiler/check-src/check-41.c +++ b/tests/tests-c-compiler/check-src/check-41.c @@ -95,7 +95,8 @@ compare(T_t *tp, uint8_t *cmp_buf, int cmp_buf_size) { int i; buf_size = cmp_buf_size + 100; - buf = alloca(buf_size); + uint8_t scratch[buf_size]; + buf = scratch; buf_pos = 0; /* @@ -118,16 +119,17 @@ compare(T_t *tp, uint8_t *cmp_buf, int cmp_buf_size) { } assert(buf[i] == cmp_buf[i]); } + + buf = 0; } static void partial_read(uint8_t *buf_0, size_t size) { T_t t, *tp; asn_dec_rval_t rval; - size_t i1, i2; - uint8_t *buf_1 = alloca(size); - uint8_t *buf_2 = alloca(size); - uint8_t *buf_3 = alloca(size); + uint8_t buf_1[size]; + uint8_t buf_2[size]; + uint8_t buf_3[size]; fprintf(stderr, "\nPartial read sequence...\n"); @@ -137,8 +139,8 @@ partial_read(uint8_t *buf_0, size_t size) { * ^ buf_0 ^ buf_0+size * Try to read block by block. */ - for(i1 = 0; i1 < size; i1++) { - for(i2 = i1; i2 < size; i2++) { + for(size_t i1 = 0; i1 < size; i1++) { + for(size_t i2 = i1; i2 < size; i2++) { uint8_t *chunk1 = buf_0; size_t size1 = i1; uint8_t *chunk2 = buf_0 + size1; diff --git a/tests/tests-c-compiler/check-src/check-42.c b/tests/tests-c-compiler/check-src/check-42.c index 8a594109..8d5868e7 100644 --- a/tests/tests-c-compiler/check-src/check-42.c +++ b/tests/tests-c-compiler/check-src/check-42.c @@ -113,7 +113,8 @@ check_serialize() { asn_fprint(stderr, &asn_DEF_LogLine, &ll); buf_size = 128; - buf = alloca(buf_size); + uint8_t scratch[buf_size]; + buf = scratch; erval = der_encode(&asn_DEF_LogLine, &ll, buf_fill, 0); assert(erval.encoded > 1); fprintf(stderr, "Encoded in %zd bytes\n", erval.encoded);