summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorStefan Kempf <stefan@cvs.openbsd.org>2007-10-12 18:14:15 +0000
committerStefan Kempf <stefan@cvs.openbsd.org>2007-10-12 18:14:15 +0000
commit7bd60afb8f35fcbfc74b138b25bd5905d2e6f26e (patch)
treea3c0682e3cdd962c1382d0b7cbf62dec77fe4a4f /usr.bin
parentf346c4af28baaa75f93787fda14f3ee8ca676d4c (diff)
From master repo:
> Add zero checks when dividing or taking modulo. ok otto@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/pcc/cpp/cpp.c19
-rw-r--r--usr.bin/pcc/cpp/cpp.h3
-rw-r--r--usr.bin/pcc/cpp/cpy.y15
3 files changed, 33 insertions, 4 deletions
diff --git a/usr.bin/pcc/cpp/cpp.c b/usr.bin/pcc/cpp/cpp.c
index 74005f0b045..4506592c717 100644
--- a/usr.bin/pcc/cpp/cpp.c
+++ b/usr.bin/pcc/cpp/cpp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cpp.c,v 1.2 2007/10/07 19:26:27 otto Exp $ */
+/* $OpenBSD: cpp.c,v 1.3 2007/10/12 18:14:14 stefan Exp $ */
/*
* Copyright (c) 2004 Anders Magnusson (ragge@ludd.luth.se).
@@ -728,6 +728,23 @@ bad: error("bad define");
}
void
+warning(usch *s)
+{
+ usch *t;
+ usch *sb = stringbuf;
+
+ flbuf();
+ savch(0);
+ if (ifiles != NULL) {
+ t = sheap("%s:%d: warning: ", ifiles->fname, ifiles->lineno);
+ write (2, t, strlen((char *)t));
+ }
+ write (2, s, strlen((char *)s));
+ write (2, "\n", 1);
+ stringbuf = sb;
+}
+
+void
xerror(usch *s)
{
usch *t;
diff --git a/usr.bin/pcc/cpp/cpp.h b/usr.bin/pcc/cpp/cpp.h
index d12968dcf08..d3e7773a522 100644
--- a/usr.bin/pcc/cpp/cpp.h
+++ b/usr.bin/pcc/cpp/cpp.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: cpp.h,v 1.2 2007/10/12 07:22:44 otto Exp $ */
+/* $OpenBSD: cpp.h,v 1.3 2007/10/12 18:14:14 stefan Exp $ */
/*
* Copyright (c) 2004 Anders Magnusson (ragge@ludd.luth.se).
@@ -143,6 +143,7 @@ void putch(int);
void putstr(usch *s);
void line(void);
usch *sheap(char *fmt, ...);
+void warning(usch *);
void xerror(usch *);
#define error(...) xerror(sheap(__VA_ARGS__))
void expmac(struct recur *);
diff --git a/usr.bin/pcc/cpp/cpy.y b/usr.bin/pcc/cpp/cpy.y
index ed73fceebcc..37ba7371fbc 100644
--- a/usr.bin/pcc/cpp/cpy.y
+++ b/usr.bin/pcc/cpp/cpy.y
@@ -1,4 +1,4 @@
-/* $OpenBSD: cpy.y,v 1.2 2007/10/12 07:22:44 otto Exp $ */
+/* $OpenBSD: cpy.y,v 1.3 2007/10/12 18:14:14 stefan Exp $ */
/*
* Copyright (c) 2004 Anders Magnusson (ragge@ludd.luth.se).
@@ -275,6 +275,18 @@ eval(struct nd *tree)
l = eval(tree->nd_left);
r = eval(tree->nd_right);
t = l.type || r.type;
+ if (tree->op == '/' || tree->op == '%') {
+ if ((t && r.v.uval == 0) || (!t && r.v.val == 0)) {
+ warning(tree->op == '/' ? "division by zero" :
+ "modulus by zero");
+ if (t)
+ ret.v.uval = 0;
+ else
+ ret.v.val = 0;
+ ret.type = t;
+ goto out;
+ }
+ }
switch (tree->op) {
EVALBIN(EQ, ==, t, l, r, 0);
EVALBIN(NE, !=, t, l, r, 0);
@@ -285,7 +297,6 @@ eval(struct nd *tree)
EVALBIN('+', +, t, l, r, t);
EVALBIN('-', -, t, l, r, t);
EVALBIN('*', *, t, l, r, t);
- // XXX check /,% by zero
EVALBIN('/', /, t, l, r, t);
EVALBIN('%', %, t, l, r, t);
EVALBIN('&', &, t, l, r, t);