]> The Tcpdump Group git mirrors - libpcap/commitdiff
Check for division or modulus by zero at code generation time.
authorGuy Harris <[email protected]>
Fri, 24 Apr 2015 18:33:10 +0000 (11:33 -0700)
committerGuy Harris <[email protected]>
Fri, 24 Apr 2015 18:33:10 +0000 (11:33 -0700)
That lets us catch it even if optimization isn't being done.

gencode.c

index ad938d478a671732ce96b0e9d9f3c33365b03d95..090a8ed3ddece9efa7319df919aa2c4666b6cc78 100644 (file)
--- a/gencode.c
+++ b/gencode.c
@@ -7027,6 +7027,17 @@ gen_arth(code, a0, a1)
 {
        struct slist *s0, *s1, *s2;
 
+       /*
+        * Disallow division by, or modulus by, zero; we do this here
+        * so that it gets done even if the optimizer is disabled.
+        */
+       if (code == BPF_DIV) {
+               if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
+                       bpf_error("division by zero");
+       } else if (code == BPF_MOD) {
+               if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
+                       bpf_error("modulus by zero");
+       }
        s0 = xfer_to_x(a1);
        s1 = xfer_to_a(a0);
        s2 = new_stmt(BPF_ALU|BPF_X|code);