zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Partial reads in ${ ... } capture
Date: Mon, 2 Oct 2023 17:27:04 -0700	[thread overview]
Message-ID: <CAH+w=7aX03B1mnYPQgm193DXz3Rjpq8iidKFzst7W=2pUfCVaQ@mail.gmail.com> (raw)

I inadvertently removed a zfree() when moving the guts of stuff() into
zstuff(), so patch below is for the resulting potential (though
unlikely) memory leak on a failed or partial read.

However, I did want to raise a question:  Should this instead return a
partly-filled buffer on a short read?  In which case a different patch
must be applied.

That would require using zshcalloc() when allocating the buffer so
that it would be properly NUL-terminated no matter how many bytes (up
to "len") were placed in it.  The ${ ... } substitution would ignore
the return of -1 and instead check whether anything had been placed in
the buffer.

I would not want to change the result of stuff() because I presume
having "fc" execute the content of a short read is not desirable, but
I'm less certain about command substitution.  Thoughts?

diff --git a/Src/input.c b/Src/input.c
index 8d7f44d7c..dd8f2edc7 100644
--- a/Src/input.c
+++ b/Src/input.c
@@ -632,6 +632,7 @@ zstuff(char **out, const char *fn)
     if (len && !(fread(buf, len, 1, in))) {
     zerr("read error on %s", fn);
     fclose(in);
+    zfree(buf, len + 1);
     unqueue_signals();
     return -1;
     }


             reply	other threads:[~2023-10-03  0:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-03  0:27 Bart Schaefer [this message]
2023-10-03  8:29 ` 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='CAH+w=7aX03B1mnYPQgm193DXz3Rjpq8iidKFzst7W=2pUfCVaQ@mail.gmail.com' \
    --to=schaefer@brasslantern.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).