expr: don't divide by zero

Calculator parser 101.

Signed-off-by: Ran Benita <ran234@gmail.com>
master
Ran Benita 2012-07-23 21:21:03 +03:00
parent 7662397fd4
commit c6279b8bae
3 changed files with 28 additions and 0 deletions

View File

@ -337,6 +337,11 @@ ExprResolveKeyCode(struct xkb_context *ctx, ExprDef *expr,
val_rtrn->uval = leftRtrn.uval * rightRtrn.uval; val_rtrn->uval = leftRtrn.uval * rightRtrn.uval;
break; break;
case OpDivide: 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; val_rtrn->uval = leftRtrn.uval / rightRtrn.uval;
break; break;
} }
@ -448,6 +453,11 @@ ExprResolveIntegerLookup(struct xkb_context *ctx, ExprDef *expr,
val_rtrn->ival = leftRtrn.ival * rightRtrn.ival; val_rtrn->ival = leftRtrn.ival * rightRtrn.ival;
break; break;
case OpDivide: 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; val_rtrn->ival = leftRtrn.ival / rightRtrn.ival;
break; break;
} }

View File

@ -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 ] };
};
};

View File

@ -55,6 +55,7 @@ main(void)
assert(test_file(ctx, "keymaps/default.xkb")); assert(test_file(ctx, "keymaps/default.xkb"));
assert(test_file(ctx, "keymaps/comprehensive-plus-geom.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")); assert(!test_file(ctx, "bad.xkb"));
xkb_context_unref(ctx); xkb_context_unref(ctx);