expr: don't divide by zero Calculator parser 101. Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
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);