[Pkg-gnupg-commit] [gnupg2] 15/160: tests/gpgscm: Dynamically allocate string buffer.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Fri Jul 15 09:36:32 UTC 2016


This is an automated email from the git hooks/post-receive script.

dkg pushed a commit to branch upstream
in repository gnupg2.

commit 8e5ad9aabdd57457f76078924d33acb94b75a877
Author: Justus Winter <justus at g10code.com>
Date:   Thu Mar 31 13:33:03 2016 +0200

    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 <justus at g10code.com>
---
 tests/gpgscm/scheme-private.h |  4 ++--
 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 404243e..0ddfdbc 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 fd8f294..1f40bb2 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) {
@@ -4674,6 +4697,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;
@@ -4798,6 +4827,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++) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/gnupg2.git



More information about the Pkg-gnupg-commit mailing list