zsh-workers
 help / color / mirror / code / Atom feed
From: Sven Wischnowsky <wischnow@informatik.hu-berlin.de>
To: zsh-workers@sunsite.auc.dk
Subject: PATCH: Re: `foo=foo; (( foo ))' => infinite recursion
Date: Tue, 14 Mar 2000 09:05:52 +0100 (MET)	[thread overview]
Message-ID: <200003140805.JAA00704@beta.informatik.hu-berlin.de> (raw)
In-Reply-To: "Bart Schaefer"'s message of Mon, 13 Mar 2000 16:05:38 +0000


Bart Schaefer wrote:

> On Mar 13, 10:34am, Sven Wischnowsky wrote:
> } Subject: Re: `foo=foo; (( foo ))' => infinite recursion
> }
> } What really irritated me was that getnumvalue() called matheval().
> } That meant that in cases like the one above $foo could contain any
> } mathematical expression and that would get evaluated.
> 
> Urgh.  This is ugly, but appears to have been done for a long time, so
> I'm a bit leery of changing it.  (( foo == $foo )) is probably meant to
> be true, which won't be the case any more with your patch.
> 
> Maybe just a recursion counter with a reasonably large limit?

Ok. The patch is relative to my last one for this. If you haven't
applied that, just ignore anything but the first two hunks.

Bye
 Sven

diff -ru ../z.old/Src/math.c Src/math.c
--- ../z.old/Src/math.c	Mon Mar 13 17:49:30 2000
+++ Src/math.c	Tue Mar 14 09:03:54 2000
@@ -47,6 +47,8 @@
 static mnumber yyval;
 static char *yylval;
 
+#define MAX_MLEVEL 256
+
 static int mlevel = 0;
 
 /* != 0 means recognize unary plus, minus, etc. */
@@ -861,6 +863,14 @@
     struct mathvalue *xstack = 0, nstack[STACKSZ];
     mnumber ret;
 
+    if (mlevel >= MAX_MLEVEL) {
+	xyyval.type = MN_INTEGER;
+	xyyval.u.l = 0;
+
+	zerr("math recursion limit exceeded", NULL, 0);
+
+	return xyyval;
+    }
     if (mlevel++) {
 	xlastbase = lastbase;
 	xnoeval = noeval;
@@ -948,78 +958,6 @@
 	(*ss)--;
     mtok = xmtok;
     return (x.type & MN_FLOAT) ? (zlong)x.u.d : x.u.l;
-}
-
-/**/
-mod_export mnumber
-mathnumber(char *s)
-{
-    mnumber ret;
-
-    ret.type = MN_INTEGER;
-
-    while (*s) {
-	switch (*s++) {
-	case '[':
-	    {
-		int base = zstrtol(s, &s, 10);
-
-		if (*s == ']')
-		    s++;
-		ret.u.l = zstrtol(s, &s, base);
-		return ret;
-	    }
-	case ' ':
-	case '\t':
-	case '\n':
-	    break;
-	case '0':
-	    if (*s == 'x' || *s == 'X') {
-		/* Should we set lastbase here? */
-		ret.u.l = zstrtol(++s, &s, 16);
-		return ret;
-	    }
-	/* Fall through! */
-	default:
-	    if (idigit(*--s) || *s == '.') {
-		char *nptr;
-#ifdef USE_LOCALE
-		char *prev_locale;
-#endif
-		for (nptr = s; idigit(*nptr); nptr++);
-
-		if (*nptr == '.' || *nptr == 'e' || *nptr == 'E') {
-		    /* it's a float */
-		    ret.type = MN_FLOAT;
-#ifdef USE_LOCALE
-		    prev_locale = setlocale(LC_NUMERIC, NULL);
-		    setlocale(LC_NUMERIC, "POSIX");
-#endif
-		    ret.u.d = strtod(s, &nptr);
-#ifdef USE_LOCALE
-		    setlocale(LC_NUMERIC, prev_locale);
-#endif
-		    if (s == nptr || *nptr == '.')
-			goto end;
-		    s = nptr;
-		} else {
-		    /* it's an integer */
-		    ret.u.l = zstrtol(s, &s, 10);
-
-		    if (*s == '#')
-			ret.u.l = zstrtol(++s, &s, ret.u.l);
-		}
-		return ret;
-	    }
-	    goto end;
-	}
-    }
- end:
-
-    ret.type = MN_INTEGER;
-    ret.u.l = 0;
-
-    return ret;
 }
 
 /*
diff -ru ../z.old/Src/params.c Src/params.c
--- ../z.old/Src/params.c	Mon Mar 13 17:49:31 2000
+++ Src/params.c	Tue Mar 14 09:03:55 2000
@@ -1420,7 +1420,7 @@
 	mn.type = MN_FLOAT;
 	mn.u.d = v->pm->gets.ffn(v->pm);
     } else
-	return mathnumber(getstrvalue(v));
+	return matheval(getstrvalue(v));
     return mn;
 }
 

--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


             reply	other threads:[~2000-03-14  8:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-03-14  8:05 Sven Wischnowsky [this message]
2000-03-14 17:26 ` Bart Schaefer
2000-03-15  9:06 Sven Wischnowsky

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=200003140805.JAA00704@beta.informatik.hu-berlin.de \
    --to=wischnow@informatik.hu-berlin.de \
    --cc=zsh-workers@sunsite.auc.dk \
    /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).