zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Markus Trippelsdorf <markus@trippelsdorf.de>
Cc: zsh-workers@zsh.org
Subject: Re: Two issues found with -fsanitize=undefined
Date: Sat, 19 Sep 2015 20:57:51 +0100	[thread overview]
Message-ID: <20150919205751.5338bddc@ntlworld.com> (raw)
In-Reply-To: <20150917075759.GA24365@x4>

On Thu, 17 Sep 2015 09:57:59 +0200
Markus Trippelsdorf <markus@trippelsdorf.de> wrote:
> 1)
>  pattern.c:2645:12: runtime error: signed integer overflow: 1234567890123456789 * 10 cannot be represented in type 'long int'
> Test ./D02glob.ztst failed: error output differs from expected as shown above for:

This might shut the compiler up, although the existing code should do
the right thing anyway.  I don't have gcc 5.

pws

diff --git a/Src/pattern.c b/Src/pattern.c
index 3b55ccf..af56bd9 100644
--- a/Src/pattern.c
+++ b/Src/pattern.c
@@ -220,8 +220,10 @@ typedef union upat *Upat;
 #if defined(ZSH_64_BIT_TYPE) || defined(LONG_IS_64_BIT)
 typedef zlong zrange_t;
 #define ZRANGE_T_IS_SIGNED	(1)
+#define ZRANGE_MAX ZLONG_MAX
 #else
 typedef unsigned long zrange_t;
+#define ZRANGE_MAX ULONG_MAX
 #endif
 
 #ifdef MULTIBYTE_SUPPORT
@@ -2641,19 +2643,30 @@ patmatch(Upat prog)
 	    start = compend = patinput;
 	    comp = 0;
 	    while (patinput < patinend && idigit(*patinput)) {
-		if (comp)
-		    comp *= 10;
-		comp += *patinput - '0';
+		int out_of_range = 0;
+		int digit = *patinput - '0';
+		if (comp > ZRANGE_MAX / (zlong)10) {
+		    out_of_range = 1;
+		} else {
+		    zrange_t c10 = comp ? comp * 10 : 0;
+		    if (ZRANGE_MAX - c10 < digit) {
+			out_of_range = 1;
+		    } else {
+			comp = c10;
+			comp += digit;
+		    }
+		}
 		patinput++;
 		compend++;
 
-		if (comp & ((zrange_t)1 << (sizeof(comp)*8 -
+		if (out_of_range ||
+		    (comp & ((zrange_t)1 << (sizeof(comp)*8 -
 #ifdef ZRANGE_T_IS_SIGNED
 					    2
 #else
 					    1
 #endif
-				))) {
+				)))) {
 		    /*
 		     * Out of range (allowing for signedness, which
 		     * we need if we are using zlongs).
diff --git a/Src/zsh.h b/Src/zsh.h
index 4e2cb65..9c7e5d9 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -36,6 +36,12 @@
  */
 #ifdef ZSH_64_BIT_TYPE
 typedef ZSH_64_BIT_TYPE zlong;
+#if defind(ZLONG_IS_LONG_LONG) && defined(LLONG_MAX)
+#define ZLONG_MAX LLONG_MAX
+#else
+/* umm... */
+#define  ZLONG_MAX ((zlong)9223372036854775807)
+#endif
 #ifdef ZSH_64_BIT_UTYPE
 typedef ZSH_64_BIT_UTYPE zulong;
 #else
@@ -44,6 +50,7 @@ typedef unsigned zlong zulong;
 #else
 typedef long zlong;
 typedef unsigned long zulong;
+#define ZLONG_MAX LONG_MAX
 #endif
 
 /*


  parent reply	other threads:[~2015-09-19 19:57 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-17  7:57 Markus Trippelsdorf
2015-09-17 17:16 ` Bart Schaefer
2015-09-17 17:24   ` Markus Trippelsdorf
2015-09-17 17:41     ` Bart Schaefer
2015-09-19 19:57 ` Peter Stephenson [this message]
2015-09-19 20:18   ` Markus Trippelsdorf
2015-09-19 22:35     ` Peter Stephenson
2015-09-20  4:09       ` Bart Schaefer
2015-09-21 12:46   ` Peter Stephenson
2015-09-21 19:13 ` Peter Stephenson
2015-09-21 20:04   ` Markus Trippelsdorf
2015-09-21 20:21     ` Peter Stephenson
2015-09-21 20:34       ` Markus Trippelsdorf
2015-09-21 20:56         ` Bart Schaefer
2015-09-22  0:02           ` Markus Trippelsdorf

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=20150919205751.5338bddc@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=markus@trippelsdorf.de \
    --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).