From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-workers@zsh.org
Subject: Re: TR : Re : [BUG] Crash due to malloc call in signal handler
Date: Fri, 13 Dec 2019 21:08:27 +0000 [thread overview]
Message-ID: <5bd41b45eaf3d745547d1091f4fe22645fd2e8e5.camel@ntlworld.com> (raw)
In-Reply-To: <1576258224.5214.31.camel@samsung.com>
On Fri, 2019-12-13 at 17:30 +0000, Peter Stephenson wrote:
> I think the intention here is to make sure we're not blocking for a long
> time in this function, but if fgetc() is doing memory allocation we're
> going to have to put that in the signal blocking. However, if the input
> itself blocks in fgetc() that's going to be a problem. The fix might be
> not to fdopen() the input file, but just read into a buffer with
> read().
This would look like the following, which is passing tests...
Additional eyes are welcome; I'm not doing a lot of Unix system programming
at the moment...
pws
diff --git a/Src/exec.c b/Src/exec.c
index 9dc91a71e..7f23dd824 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -4653,12 +4653,10 @@ mod_export LinkList
readoutput(int in, int qt, int *readerror)
{
LinkList ret;
- char *buf, *ptr;
- int bsiz, c, cnt = 0;
- FILE *fin;
+ char *buf, *bufptr, *ptr, inbuf[64];
+ int bsiz, c, cnt = 0, readret;
int q = queue_signal_level();
- fin = fdopen(in, "r");
ret = newlinklist();
ptr = buf = (char *) hcalloc(bsiz = 64);
/*
@@ -4670,33 +4668,37 @@ readoutput(int in, int qt, int *readerror)
*/
dont_queue_signals();
child_unblock();
- while ((c = fgetc(fin)) != EOF || errno == EINTR) {
- if (c == EOF) {
- errno = 0;
- clearerr(fin);
- continue;
- }
- if (imeta(c)) {
- *ptr++ = Meta;
- c ^= 32;
- cnt++;
+ for (;;) {
+ readret = read(in, inbuf, 64);
+ if (readret <= 0) {
+ if (readret < 0 && errno == EINTR)
+ continue;
+ else
+ break;
}
- if (++cnt >= bsiz) {
- char *pp;
- queue_signals();
- pp = (char *) hcalloc(bsiz *= 2);
- dont_queue_signals();
+ for (bufptr = inbuf; bufptr < inbuf + readret; bufptr++) {
+ c = *bufptr;
+ if (imeta(c)) {
+ *ptr++ = Meta;
+ c ^= 32;
+ cnt++;
+ }
+ if (++cnt >= bsiz) {
+ char *pp;
+ queue_signals();
+ pp = (char *) hcalloc(bsiz *= 2);
+ dont_queue_signals();
- memcpy(pp, buf, cnt - 1);
- ptr = (buf = pp) + cnt - 1;
+ memcpy(pp, buf, cnt - 1);
+ ptr = (buf = pp) + cnt - 1;
+ }
+ *ptr++ = c;
}
- *ptr++ = c;
}
child_block();
restore_queue_signals(q);
if (readerror)
- *readerror = ferror(fin) ? errno : 0;
- fclose(fin);
+ *readerror = readret < 0 ? errno : 0;
while (cnt && ptr[-1] == '\n')
ptr--, cnt--;
*ptr = '\0';
next prev parent reply other threads:[~2019-12-13 21:09 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <CGME20191213171028eucas1p2e7820377701008b2d05c898192b7c17e@eucas1p2.samsung.com>
2019-12-13 17:09 ` Antoine C.
2019-12-13 17:30 ` Peter Stephenson
2019-12-13 21:08 ` Peter Stephenson [this message]
2019-12-14 12:32 ` Peter Stephenson
2019-12-15 19:06 ` Peter Stephenson
2019-12-16 4:00 ` Daniel Shahaf
2019-12-16 10:05 ` Peter Stephenson
2019-12-13 21:10 ` Bart Schaefer
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=5bd41b45eaf3d745547d1091f4fe22645fd2e8e5.camel@ntlworld.com \
--to=p.w.stephenson@ntlworld.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).