Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: TR : Re : [BUG] Crash due to malloc call in signal handler
On Fri, 2019-12-13 at 21:08 +0000, Peter Stephenson wrote:
> 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...
Slight update to close the file descriptor and to document the arguments
of the function.
pws
diff --git a/Src/exec.c b/Src/exec.c
index 9dc91a71e..64eee7dc4 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -4646,19 +4646,25 @@ getoutput(char *cmd, int qt)
return NULL;
}
-/* read output of command substitution */
+/* read output of command substitution
+ *
+ * The file descriptor "in" is closed by the function.
+ *
+ * "qt" indicates if the substitution was in double quotes.
+ *
+ * "readerror", if not NULL, is used to return any error that
+ * occurred during the read.
+ */
/**/
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 +4676,38 @@ 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;
+ close(in);
while (cnt && ptr[-1] == '\n')
ptr--, cnt--;
*ptr = '\0';
Messages sorted by:
Reverse Date,
Date,
Thread,
Author