Malfunction of gcry_sexp_car

Benjamin Pousse benjamin.pousse at member.fsf.org
Fri Feb 17 22:16:12 CET 2012


2012/2/17 Andre Amorim <andre at amorim.me>:
> I cant read the code, need to USE "TAB" (80, col) format
>

Sorry for this. Looks like my mail manager sends lines of 71 characters.
Let's try with my webmail :


diff --git a/src/sexp.c b/src/sexp.c
index 0877773..380235b 100644
--- a/src/sexp.c
+++ b/src/sexp.c
@@ -576,13 +576,25 @@ gcry_sexp_nth( const gcry_sexp_t list, int number )
     p++;

     if ( *p == ST_DATA ) {
-	memcpy ( &n, p, sizeof n ); p += sizeof n;
-	newlist = gcry_malloc ( sizeof *newlist + n + 1 );
+      memcpy ( &n, p, sizeof n );
+      /* Allocate 1 (=sizeof *newlist) byte for ST_OPEN
+                  1 byte for ST_DATA
+                  sizeof n byte for n
+                  n byte for the data
+                  1 byte for ST_CLOSE
+                  1 byte for ST_STOP */
+      newlist = gcry_malloc ( sizeof *newlist + 1 + sizeof n + n + 2 );
         if (!newlist)
           return NULL;
-	d = newlist->d;
-	memcpy ( d, p, n ); d += n;
-	*d++ = ST_STOP;
+      d = newlist->d;
+      *d = ST_OPEN;   /* Put the ST_OPEN flag */
+      d++;            /* Move forward */
+      /* Copy ST_DATA, n and the data from p to d */
+      memcpy ( d, p, 1 + sizeof n + n );
+      d += 1 + sizeof n + n;  /* Move after the data copied */
+      *d = ST_CLOSE;          /* Put the ST_CLOSE flag */
+      d++;                    /* Move forward */
+      *d = ST_STOP;           /* Put the ST_STOP flag */
     }
     else if ( *p == ST_OPEN ) {
 	const byte *head = p;



More information about the Gcrypt-devel mailing list