zsh-workers
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <psprint@zdharma.org>
To: zsh-workers@zsh.org
Subject: [PATCH] Update of db_gdbm module – strict zsfree() usage, better main #ifdef
Date: Wed, 24 May 2017 16:07:23 +0200	[thread overview]
Message-ID: <etPan.5925939c.6b8b4567.1a9@MacMini.local> (raw)

[-- Attachment #1: Type: text/plain, Size: 663 bytes --]

Hello,
when unmetafied string is returned, possibly containing nulls, zsfree() cannot be used. There is probably a better way to solve this, but following method for sure works:

         /* Free key, restoring its original length */
+        set_length(umkey, umlen);
         zsfree(umkey);

+static void set_length(char *buf, int size) {
+    buf[size]='\0';
+    while ( -- size >= 0 ) {
+        buf[size]=' ';
+    }
+}

Attached is a patch, tested with TESTNUM=V11. Would be nice if also 40898 would be applied: http://www.zsh.org/mla/workers/2017/msg00568.html

--
Sebastian Gniazdowski
psprint /at/ zdharma.org

[-- Attachment #2: gdbm_uplift.diff --]
[-- Type: application/octet-stream, Size: 3128 bytes --]

diff --git a/Src/Modules/db_gdbm.c b/Src/Modules/db_gdbm.c
index 596a8ae..9f441ba 100644
--- a/Src/Modules/db_gdbm.c
+++ b/Src/Modules/db_gdbm.c
@@ -41,7 +41,8 @@
 static Param createhash( char *name, int flags );
 static int append_tied_name( const char *name );
 static int remove_tied_name( const char *name );
-char *unmetafy_zalloc(const char *to_copy, int *new_len);
+static char *unmetafy_zalloc(const char *to_copy, int *new_len);
+static void set_length(char *buf, int size);
 
 /*
  * Make sure we have all the bits I'm using for memory mapping, otherwise
@@ -319,13 +320,15 @@ gdbmgetfn(Param pm)
         pm->u.str = metafy(content.dptr, content.dsize, META_DUP);
 
         /* Free key, restoring its original length */
+        set_length(umkey, umlen);
         zsfree(umkey);
 
         /* Can return pointer, correctly saved inside hash */
         return pm->u.str;
     }
 
-    /* Free key */
+    /* Free key, restoring its original length */
+    set_length(umkey, umlen);
     zsfree(umkey);
 
     /* Can this be "" ? */
@@ -374,12 +377,14 @@ gdbmsetfn(Param pm, char *val)
             (void)gdbm_store(dbf, key, content, GDBM_REPLACE);
 
             /* Free */
+            set_length(umval, umlen);
             zsfree(umval);
         } else {
             (void)gdbm_delete(dbf, key);
         }
 
         /* Free key */
+        set_length(umkey, key.dsize);
         zsfree(umkey);
     }
 }
@@ -518,10 +523,12 @@ gdbmhashsetfn(Param pm, HashTable ht)
 	    content.dsize = umlen;
 	    (void)gdbm_store(dbf, key, content, GDBM_REPLACE);	
 
-            /* Free - thanks to unmetafy_zalloc size of
-             * the strings is exact zalloc size - can
-             * pass to zsfree */
+            /* Free - unmetafy_zalloc allocates exact required
+             * space, however unmetafied string can have zeros
+             * in content, so we must first fill with non-0 bytes */
+            set_length(umval, content.dsize);
             zsfree(umval);
+            set_length(umkey, key.dsize);
             zsfree(umkey);
 
 	    unqueue_signals();
@@ -576,10 +583,6 @@ gdbmhashunsetfn(Param pm, UNUSED(int exp))
     pm->node.flags |= PM_UNSET;
 }
 
-#else
-# error no gdbm
-#endif /* have gdbm */
-
 static struct features module_features = {
     bintab, sizeof(bintab)/sizeof(*bintab),
     NULL, 0,
@@ -745,7 +748,7 @@ static int remove_tied_name( const char *name ) {
  *
  * No zsfree()-confusing string will be produced.
  */
-char *unmetafy_zalloc(const char *to_copy, int *new_len) {
+static char *unmetafy_zalloc(const char *to_copy, int *new_len) {
     char *work, *to_return;
     int my_new_len = 0;
 
@@ -766,3 +769,19 @@ char *unmetafy_zalloc(const char *to_copy, int *new_len) {
 
     return to_return;
 }
+
+/*
+ * For zsh-allocator, rest of Zsh seems to use
+ * free() instead of zsfree(), and such length
+ * restoration causes slowdown, but all is this
+ * way strict - correct */
+static void set_length(char *buf, int size) {
+    buf[size]='\0';
+    while ( -- size >= 0 ) {
+        buf[size]=' ';
+    }
+}
+
+#else
+# error no gdbm
+#endif /* have gdbm */

                 reply	other threads:[~2017-05-24 14:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=etPan.5925939c.6b8b4567.1a9@MacMini.local \
    --to=psprint@zdharma.org \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).