zsh-workers
 help / color / mirror / code / Atom feed
From: Jun T <takimoto-j@kba.biglobe.ne.jp>
To: zsh-workers@zsh.org
Subject: Re: [PATCH] fix several memory leaks
Date: Wed, 1 Aug 2018 20:14:32 +0900	[thread overview]
Message-ID: <D9FE9B89-17E6-41BE-A574-6B68F3CFD5FD@kba.biglobe.ne.jp> (raw)
In-Reply-To: <CAH+w=7aNiLFDUUtdAnwGN-9LeO90PfF49xQE9U=bRZyV0F-zYw@mail.gmail.com>


> 2018/07/29 3:57, Bart Schaefer <schaefer@brasslantern.com> wrote:
> 
> On Sat, Jul 28, 2018, 11:06 AM Jun T. <takimoto-j@kba.biglobe.ne.jp> wrote:
> 
>> 
>> Parameters
>> are supposed to be freed by setfeatureenables(,,NULL),
>> and explicitly freeing them in cleanup_() is not a good
>> example.
>> 
> 
> Side-effect of this module having been created before the features
> mechanism existed, and incompletely updated after.

If we want to show that each feature can be independently enabled, then
setting the parameter exarr (arrparam) etc. in bin_example() is better
avoided.

But we still need to make sure that the parameters are allocated only
if they are enabled. We can do this, for example, as in the patch below,
but is this a "good example"?



diff --git a/Src/Modules/example.c b/Src/Modules/example.c
index c80c9e7b2..b10c2f0e5 100644
--- a/Src/Modules/example.c
+++ b/Src/Modules/example.c
@@ -42,15 +42,14 @@ static int
 bin_example(char *nam, char **args, Options ops, UNUSED(int func))
 {
     unsigned char c;
-    char **oargs = args, **p = arrparam;
-    long i = 0;
+    char **p = arrparam;
 
     printf("Options: ");
     for (c = 32; ++c < 128;)
 	if (OPT_ISSET(ops,c))
 	    putchar(c);
     printf("\nArguments:");
-    for (; *args; i++, args++) {
+    for (; *args; args++) {
 	putchar(' ');
 	fputs(*args, stdout);
     }
@@ -66,12 +65,6 @@ bin_example(char *nam, char **args, Options ops, UNUSED(int func))
 	while (*p) printf(" %s", *p++);
     printf("\n");
 
-    intparam = i;
-    zsfree(strparam);
-    strparam = ztrdup(*oargs ? *oargs : "");
-    if (arrparam)
-	freearray(arrparam);
-    arrparam = zarrdup(oargs);
     return 0;
 }
 
@@ -221,12 +214,19 @@ enables_(Module m, int **enables)
 int
 boot_(Module m)
 {
-    intparam = 42;
-    strparam = ztrdup("example");
-    arrparam = (char **) zalloc(3 * sizeof(char *));
-    arrparam[0] = ztrdup("example");
-    arrparam[1] = ztrdup("array");
-    arrparam[2] = NULL;
+    int *enables = getfeatureenables(m, &module_features);
+
+    if (enables[5]) {
+	arrparam = (char **) zalloc(3 * sizeof(char *));
+	arrparam[0] = ztrdup("example");
+	arrparam[1] = ztrdup("array");
+	arrparam[2] = NULL;
+    }
+    if (enables[6])
+	intparam = 42;
+    if (enables[7])
+	strparam = ztrdup("example");
+
     return addwrapper(m, wrapper);
 }
 



  reply	other threads:[~2018-08-01 11:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-28 16:34 Jun T.
2018-07-28 17:20 ` Jun T.
2018-07-28 18:57   ` Bart Schaefer
2018-08-01 11:14     ` Jun T [this message]
2018-07-30 13:47 ` Peter Stephenson
2018-07-31 14:30   ` Jun T.
2018-07-31 14:41     ` 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=D9FE9B89-17E6-41BE-A574-6B68F3CFD5FD@kba.biglobe.ne.jp \
    --to=takimoto-j@kba.biglobe.ne.jp \
    --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).