From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2601 invoked by alias); 20 Jan 2014 01:10:58 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 32294 Received: (qmail 24014 invoked from network); 20 Jan 2014 01:10:41 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 From: Bart Schaefer Message-id: <140119171049.ZM18371@torch.brasslantern.com> Date: Sun, 19 Jan 2014 17:10:49 -0800 In-reply-to: <140119160238.ZM5850@torch.brasslantern.com> Comments: In reply to Bart Schaefer "Re: segfault with exceedingly long path" (Jan 19, 4:02pm) References: <20140118002033.GY27889@sym.noone.org> <140117174902.ZM7366@torch.brasslantern.com> <20140119191048.018a051a@pws-pc.ntlworld.com> <140119133550.ZM5354@torch.brasslantern.com> <20140119221310.GA502@ruderich.org> <140119160238.ZM5850@torch.brasslantern.com> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-workers@zsh.org Subject: Re: segfault with exceedingly long path MIME-version: 1.0 Content-type: text/plain; charset=us-ascii On Jan 19, 4:02pm, Bart Schaefer wrote: } } I don't think we want to let this can of worms out of Pandora's box, } or we'll be chasing geese until the cows come home to roost. In spite of that ... we could at least not dump core in this specific case. There are probably many other core dumps waiting to be exposed. Behavior becomes undefined once the path gets too long, but: diff --git a/Src/utils.c b/Src/utils.c index c6d178c..705d2c4 100644 --- a/Src/utils.c +++ b/Src/utils.c @@ -725,32 +725,36 @@ xsymlinks(char *s) char **pp, **opp; char xbuf2[PATH_MAX*2], xbuf3[PATH_MAX*2]; int t0, ret = 0; + zulong xbuflen = strlen(xbuf); opp = pp = slashsplit(s); - for (; *pp; pp++) { - if (!strcmp(*pp, ".")) { - zsfree(*pp); + for (; xbuflen < sizeof(xbuf) && *pp; pp++) { + if (!strcmp(*pp, ".")) continue; - } if (!strcmp(*pp, "..")) { char *p; - zsfree(*pp); if (!strcmp(xbuf, "/")) continue; if (!*xbuf) continue; - p = xbuf + strlen(xbuf); - while (*--p != '/'); + p = xbuf + xbuflen; + while (*--p != '/') + xbuflen--; *p = '\0'; continue; } sprintf(xbuf2, "%s/%s", xbuf, *pp); t0 = readlink(unmeta(xbuf2), xbuf3, PATH_MAX); if (t0 == -1) { - strcat(xbuf, "/"); - strcat(xbuf, *pp); - zsfree(*pp); + zulong pplen = strlen(pp) + 1; + if ((xbuflen += pplen) < sizeof(xbuf)) { + strcat(xbuf, "/"); + strcat(xbuf, *pp); + } else { + *xbuf = 0; + break; + } } else { ret = 1; metafy(xbuf3, t0, META_NOALLOC); @@ -759,10 +763,9 @@ xsymlinks(char *s) xsymlinks(xbuf3 + 1); } else xsymlinks(xbuf3); - zsfree(*pp); } } - free(opp); + freearray(opp); return ret; } @@ -779,8 +782,10 @@ xsymlink(char *s) return NULL; *xbuf = '\0'; xsymlinks(s + 1); - if (!*xbuf) + if (!*xbuf) { + zwarn("path expansion failed, using root directory"); return ztrdup("/"); + } return ztrdup(xbuf); } -- Barton E. Schaefer