Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Integer overflow during brace expansion
- X-seq: zsh-workers 30279
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Leon Weber <leon@xxxxxxxxxxxx>
- Subject: Re: Integer overflow during brace expansion
- Date: Mon, 27 Feb 2012 17:54:02 +0100
- Authentication-results: mr.google.com; spf=pass (google.com: domain of mikachu@xxxxxxxxx designates 10.182.1.40 as permitted sender) smtp.mail=mikachu@xxxxxxxxx; dkim=pass header.i=mikachu@xxxxxxxxx
- Cc: zsh-workers@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=XB+BuDsujKgfenPTwl+nJk2CE6LNp8ea+TXsdWod0N8=; b=jAQ6efe6MDnzh+iIXenup/5OHNi5ngUhiygqjPOCjnYsKmy5UDAop2XGP5CtqcPcvn I5mFa/IzeBTQT22OX89kdcmWVJvopc9OroH9rbAHrrNiiJPqL+j9Dm1+tSTQlytt8uji te//lRTfI64ISpz2/Q2CPY0fm24LC3DyAXraA=
- In-reply-to: <CAHYJk3SwmPTq0pNd2qa88kZV0beai9UjsyHeJK0Wh5csgm4=eg@mail.gmail.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <20120227162251.GA17559@zaphod.q-ix.net> <CAHYJk3SwmPTq0pNd2qa88kZV0beai9UjsyHeJK0Wh5csgm4=eg@mail.gmail.com>
On 27 February 2012 17:52, Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
> On 27 February 2012 17:22, Leon Weber <leon@xxxxxxxxxxxx> wrote:
>> Hi,
>>
>> When parsing and expanding 'dotdot' type brace expressions (e.g. {1..3}),
>> zsh can encounter integer overflows because the range start and end
>> values are stored in two integers without proper bounds checking
>> (rstart and rend in glob.c:2092). Particularly,
>> this happens when one of the range boundaries is <=-2147483648
>> or >=2147483648, like in this example:
>>
>>> zsh% echo {-2147483648..-2147483646}
>>
>> This will cause zsh to eat up 100% CPU iterating through the loop declared
>> in line 2147 in glob.c. In that loop, rend is decreased until it underflows.
>> In the third and fourth iterations, we have these conditions:
> [snippysnip]
>> This gdb output clearly shows that rend has underflown and is now at the
>> far positive end of the valid integer values. zsh will keep iterating
>> over that loop and decreasing rend for a really long time.
>>
>> For comparison, a bash shell handles this correctly:
>>
>>> bash$ echo {-2147483648..-2147483646}
>>> -2147483648 -2147483647 -2147483646
>
> $ echo {-2147483648..2147483646}
> zsh: segmentation fault bash
> and if you do this
> $ echo {0..214783646}^C
> it doesn't free the allocated memory until you exit the shell :).
>
> (Of course, just for comparison. In zsh you can't even abort the
> process with ctrl-c ;).)
>
> The fix is fairly simple, just change the types involved to zlong. A
> problem is that sprintf is used to convert the generated numbers back
> to a string, and zlong can be an int or a long, but I forgot if we
> have a modifier macro that expands to the correct thing. I remember
> asking about it before (and possibly saying I would look in to fixing
> it), but at the moment I don't remember what the result was.
>
> With hardcoding it to %ld though, we get
ie, this:
diff --git i/Src/glob.c w/Src/glob.c
index 8f8127c..78b197c 100644
--- i/Src/glob.c
+++ w/Src/glob.c
@@ -2089,7 +2089,8 @@ xpandbraces(LinkList list, LinkNode *np)
char *dots, *p, *dots2 = NULL;
LinkNode olast = last;
/* Get the first number of the range */
- int rstart = zstrtol(str+1,&dots,10), rend = 0, err = 0, rev = 0, rincr = 1;
+ zlong rstart = zstrtol(str+1,&dots,10), rend = 0;
+ int err = 0, rev = 0, rincr = 1;
int wid1 = (dots - str) - 1, wid2 = (str2 - dots) - 2, wid3 = 0;
int strp = str - str3;
@@ -2134,7 +2135,7 @@ xpandbraces(LinkList list, LinkNode *np)
}
if (rstart > rend) {
/* Handle decreasing ranges correctly. */
- int rt = rend;
+ zlong rt = rend;
rend = rstart;
rstart = rt;
rev = !rev;
@@ -2147,7 +2148,7 @@ xpandbraces(LinkList list, LinkNode *np)
for (; rend >= rstart; rend -= rincr) {
/* Node added in at end, so do highest first */
p = dupstring(str3);
- sprintf(p + strp, "%0*d", minw, rend);
+ sprintf(p + strp, "%0*ld", minw, rend);
strcat(p + strp, str2 + 1);
insertlinknode(list, last, p);
if (rev) /* decreasing: add in reverse order. */
Messages sorted by:
Reverse Date,
Date,
Thread,
Author