Commit c6279b8baeae7dd9ff486bc0fb60ff0bef0587af

Ran Benita 2012-07-23T21:21:03

expr: don't divide by zero Calculator parser 101. Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/xkbcomp/expr.c b/src/xkbcomp/expr.c
index 7653255..c728e65 100644
--- a/src/xkbcomp/expr.c
+++ b/src/xkbcomp/expr.c
@@ -337,6 +337,11 @@ ExprResolveKeyCode(struct xkb_context *ctx, ExprDef *expr,
                 val_rtrn->uval = leftRtrn.uval * rightRtrn.uval;
                 break;
             case OpDivide:
+                if (rightRtrn.uval == 0) {
+                    log_err(ctx, "Cannot divide by zero: %d / %d\n",
+                            leftRtrn.uval, rightRtrn.uval);
+                    return false;
+                }
                 val_rtrn->uval = leftRtrn.uval / rightRtrn.uval;
                 break;
             }
@@ -448,6 +453,11 @@ ExprResolveIntegerLookup(struct xkb_context *ctx, ExprDef *expr,
                 val_rtrn->ival = leftRtrn.ival * rightRtrn.ival;
                 break;
             case OpDivide:
+                if (rightRtrn.ival == 0) {
+                    log_err(ctx, "Cannot divide by zero: %d / %d\n",
+                            leftRtrn.ival, rightRtrn.ival);
+                    return false;
+                }
                 val_rtrn->ival = leftRtrn.ival / rightRtrn.ival;
                 break;
             }
diff --git a/test/data/keymaps/divide-by-zero.xkb b/test/data/keymaps/divide-by-zero.xkb
new file mode 100644
index 0000000..5ae7d44
--- /dev/null
+++ b/test/data/keymaps/divide-by-zero.xkb
@@ -0,0 +1,17 @@
+xkb_keymap {
+	xkb_keycodes {
+		 <ESC> = 1 / 0;
+	};
+
+	xkb_types {
+		virtual_modifiers NumLock;
+	};
+
+	xkb_compatibility {
+		virtual_modifiers NumLock;
+	};
+
+	xkb_symbols {
+		key <HYPR> {	[        NoSymbol,         Hyper_L ] };
+	};
+};
diff --git a/test/filecomp.c b/test/filecomp.c
index 757c9a7..0297897 100644
--- a/test/filecomp.c
+++ b/test/filecomp.c
@@ -55,6 +55,7 @@ main(void)
     assert(test_file(ctx, "keymaps/default.xkb"));
     assert(test_file(ctx, "keymaps/comprehensive-plus-geom.xkb"));
 
+    assert(!test_file(ctx, "keymaps/divide-by-zero.xkb"));
     assert(!test_file(ctx, "bad.xkb"));
 
     xkb_context_unref(ctx);