diff options
author | Justus Winter <[email protected]> | 2016-03-31 11:33:03 +0000 |
---|---|---|
committer | Justus Winter <[email protected]> | 2016-04-21 13:36:34 +0000 |
commit | cac96faaeaa232c73f268688fa05c5ac2206f4cb (patch) | |
tree | e88271046a74eb45821dc0b96e940dc1435a8486 | |
parent | tests/gpgscm: Make exception value available. (diff) | |
download | gnupg-cac96faaeaa232c73f268688fa05c5ac2206f4cb.tar.gz gnupg-cac96faaeaa232c73f268688fa05c5ac2206f4cb.zip |
tests/gpgscm: Dynamically allocate string buffer.
* tests/gpgscm/scheme-config.h (strbuff{,_size}): Make buffer dynamic.
* tests/gpgscm/scheme.c (expand_strbuff): New function.
(putcharacter): Adapt length test.
(readstrexp): Expand buffer if necessary.
(scheme_init_custom_alloc): Initialize buffer.
(scheme_deinit): Free buffer.
Patch from Thomas Munro,
https://sourceforge.net/p/tinyscheme/patches/11/
Signed-off-by: Justus Winter <[email protected]>
-rw-r--r-- | tests/gpgscm/scheme-private.h | 4 | ||||
-rw-r--r-- | tests/gpgscm/scheme.c | 34 |
2 files changed, 34 insertions, 4 deletions
diff --git a/tests/gpgscm/scheme-private.h b/tests/gpgscm/scheme-private.h index 404243ef9..0ddfdbcc5 100644 --- a/tests/gpgscm/scheme-private.h +++ b/tests/gpgscm/scheme-private.h @@ -139,8 +139,8 @@ char linebuff[LINESIZE]; #ifndef STRBUFFSIZE #define STRBUFFSIZE 256 #endif -char strbuff[STRBUFFSIZE]; - +char *strbuff; +size_t strbuff_size; FILE *tmpfp; int tok; int print_flag; diff --git a/tests/gpgscm/scheme.c b/tests/gpgscm/scheme.c index 2c9fd0247..b62f70e2f 100644 --- a/tests/gpgscm/scheme.c +++ b/tests/gpgscm/scheme.c @@ -67,6 +67,7 @@ #define banner "TinyScheme 1.41" #include <string.h> +#include <stddef.h> #include <stdlib.h> #ifdef __APPLE__ @@ -1071,6 +1072,21 @@ INTERFACE pointer gensym(scheme *sc) { return sc->NIL; } +/* double the size of the string buffer */ +static int expand_strbuff(scheme *sc) { + size_t new_size = sc->strbuff_size * 2; + char *new_buffer = sc->malloc(new_size); + if (new_buffer == 0) { + sc->no_memory = 1; + return 1; + } + memcpy(new_buffer, sc->strbuff, sc->strbuff_size); + sc->free(sc->strbuff); + sc->strbuff = new_buffer; + sc->strbuff_size = new_size; + return 0; +} + /* make symbol or number atom from string */ static pointer mk_atom(scheme *sc, char *q) { char c, *p; @@ -1612,7 +1628,7 @@ INTERFACE void putcharacter(scheme *sc, int c) { static char *readstr_upto(scheme *sc, char *delim) { char *p = sc->strbuff; - while ((p - sc->strbuff < sizeof(sc->strbuff)) && + while ((p - sc->strbuff < sc->strbuff_size) && !is_one_of(delim, (*p++ = inchar(sc)))); if(p == sc->strbuff+2 && p[-2] == '\\') { @@ -1633,9 +1649,16 @@ static pointer readstrexp(scheme *sc) { for (;;) { c=inchar(sc); - if(c == EOF || p-sc->strbuff > sizeof(sc->strbuff)-1) { + if(c == EOF) { return sc->F; } + if(p-sc->strbuff > (sc->strbuff_size)-1) { + ptrdiff_t offset = p - sc->strbuff; + if (expand_strbuff(sc) != 0) { + return sc->F; + } + p = sc->strbuff + offset; + } switch(state) { case st_ok: switch(c) { @@ -4670,6 +4693,12 @@ int scheme_init_custom_alloc(scheme *sc, func_alloc malloc, func_dealloc free) { sc->loadport=sc->NIL; sc->nesting=0; sc->interactive_repl=0; + sc->strbuff = sc->malloc(STRBUFFSIZE); + if (sc->strbuff == 0) { + sc->no_memory=1; + return 0; + } + sc->strbuff_size = STRBUFFSIZE; if (alloc_cellseg(sc,FIRST_CELLSEGS) != FIRST_CELLSEGS) { sc->no_memory=1; @@ -4794,6 +4823,7 @@ void scheme_deinit(scheme *sc) { for(i=0; i<=sc->last_cell_seg; i++) { sc->free(sc->alloc_seg[i]); } + sc->free(sc->strbuff); #if SHOW_ERROR_LINE for(i=0; i<=sc->file_i; i++) { |