zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Anyone want to help make zsh/db/gdbm work?
Date: Tue, 03 Feb 2015 10:22:39 +0000	[thread overview]
Message-ID: <20150203102239.7585c67a@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <20150202094643.3ce41ebc@pwslap01u.europe.root.pri>

On Mon, 2 Feb 2015 09:46:43 +0000
Peter Stephenson <p.stephenson@samsung.com> wrote:
> On Fri, 30 Jan 2015 12:03:04 -0800
> Bart Schaefer <schaefer@brasslantern.com> wrote:
> > If you're actually going to look up the parameter first, you should also
> > delay the gdbm_open() until you've verified the parameter.
> 
> There's another point here that if unsetting the old parameter happens
> to untie an existing database, possibly even the same one, it needs to
> be done before the new open.

This moves things around.  I kept the code so I could test the return
value, but as noted in the comments it would currently be good enough to
use unsetparam() and test errflag.

I can't see why pmname was duplicated; createparam() doesn't require
this and doesn't ever free what you passed in.  It was suspicious that
the duplicated value of pmname was used in an error message just before
a return: either the value was invalid, or it needed to be freed, and I
think the latter.  I've simply removed the duplication.

diff --git a/Src/Modules/db_gdbm.c b/Src/Modules/db_gdbm.c
index 2e2bd3a..76d4751 100644
--- a/Src/Modules/db_gdbm.c
+++ b/Src/Modules/db_gdbm.c
@@ -85,14 +85,7 @@ bin_ztie(char *nam, char **args, Options ops, UNUSED(int func))
     }
 
     resource_name = OPT_ARG(ops, 'f');
-
-    dbf = gdbm_open(resource_name, 0, read_write, 0666, 0);
-    if(!dbf) {
-	zwarnnam(nam, "error opening database file %s", resource_name);
-	return 1;
-    }
-
-    pmname = ztrdup(*args);
+    pmname = *args;
 
     if ((tied_param = (Param)paramtab->getnode(paramtab, pmname)) &&
 	!(tied_param->node.flags & PM_UNSET)) {
@@ -100,13 +93,24 @@ bin_ztie(char *nam, char **args, Options ops, UNUSED(int func))
 	 * Unset any existing parameter.  Note there's no implicit
 	 * "local" here, but if the existing parameter is local
 	 * that will be reflected in the new one.
+	 *
+	 * We need to do this before attempting to open the DB
+	 * in case this variable is already tied to a DB.
+	 *
+	 * This can fail if the variable is readonly or restricted.
+	 * We could call unsetparam() and check errflag instead
+	 * of the return status.
 	 */
-	if (unsetparam_pm(tied_param, 0, 1)) {
-	    zsfree(pmname);
-	    gdbm_close(dbf);
+	if (unsetparam_pm(tied_param, 0, 1))
 	    return 1;
-	}
     }
+
+    dbf = gdbm_open(resource_name, 0, read_write, 0666, 0);
+    if(!dbf) {
+	zwarnnam(nam, "error opening database file %s", resource_name);
+	return 1;
+    }
+
     if (!(tied_param = createspecialhash(pmname, &getgdbmnode, &scangdbmkeys,
 					 pmflags))) {
         zwarnnam(nam, "cannot create the requested parameter %s", pmname);


  reply	other threads:[~2015-02-03 10:22 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-23  5:19 Bart Schaefer
2015-01-23  5:37 ` ZyX
2015-01-23  5:59   ` Bart Schaefer
2015-01-23 15:49     ` ZyX
2015-01-23 19:47       ` Peter Stephenson
2015-01-23 20:00         ` ZyX
2015-01-23 20:16           ` Aaron Schrab
2015-01-23 20:33         ` Mikael Magnusson
2015-01-26 12:11 ` Peter Stephenson
2015-01-29 20:46   ` Peter Stephenson
2015-01-29 22:06     ` Bart Schaefer
2015-01-30  8:59       ` Peter Stephenson
2015-01-30 19:56         ` Peter Stephenson
2015-01-30 20:03           ` Bart Schaefer
2015-02-02  9:46             ` Peter Stephenson
2015-02-03 10:22               ` Peter Stephenson [this message]
2015-01-31  3:21         ` Bart Schaefer
2015-01-31  3:37           ` Bart Schaefer
2015-02-01 19:57             ` Bart Schaefer
2015-02-01 21:49             ` Bart Schaefer
2015-02-02  5:18 ` ZyX
2015-02-02 17:33   ` Peter Stephenson

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=20150203102239.7585c67a@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --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).