From 7bd60afb8f35fcbfc74b138b25bd5905d2e6f26e Mon Sep 17 00:00:00 2001 From: Stefan Kempf Date: Fri, 12 Oct 2007 18:14:15 +0000 Subject: From master repo: > Add zero checks when dividing or taking modulo. ok otto@ --- usr.bin/pcc/cpp/cpp.c | 19 ++++++++++++++++++- usr.bin/pcc/cpp/cpp.h | 3 ++- usr.bin/pcc/cpp/cpy.y | 15 +++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) (limited to 'usr.bin') 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). @@ -727,6 +727,23 @@ id: savstr((usch *)yytext); 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) { 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); -- cgit v1.2.3