• R/O
  • SSH
  • HTTPS

yash: 提交


Commit MetaInfo

修訂4194 (tree)
時間2022-08-17 00:45:31
作者magicant

Log Message

arith: Detect error in bitwise shifting

Change Summary

差異

--- yash/branches/arith-error/arith.c (revision 4193)
+++ yash/branches/arith-error/arith.c (revision 4194)
@@ -443,9 +443,18 @@
443443 {
444444 switch (ttype) {
445445 case TT_LESSLESS: case TT_LESSLESSEQUAL:
446+ if (v1 < 0)
447+ goto negative_left_shift;
448+ if (v2 < 0 || v2 >= LONG_BIT)
449+ goto invalid_shift_width;
450+ unsigned long u1 = (unsigned long) v1;
451+ if ((u1 << v2 & (unsigned long) LONG_MAX) >> v2 != u1)
452+ goto overflow;
446453 *result = v1 << v2;
447454 return true;
448455 case TT_GREATERGREATER: case TT_GREATERGREATEREQUAL:
456+ if (v2 < 0 || v2 >= LONG_BIT)
457+ goto invalid_shift_width;
449458 *result = v1 >> v2;
450459 return true;
451460 case TT_AMP: case TT_AMPEQUAL:
@@ -460,6 +469,16 @@
460469 default:
461470 assert(false);
462471 }
472+
473+overflow:
474+ xerror(0, Ngt("arithmetic: overflow"));
475+ return false;
476+negative_left_shift:
477+ xerror(0, Ngt("arithmetic: negative value cannot be shifted to left"));
478+ return false;
479+invalid_shift_width:
480+ xerror(0, Ngt("arithmetic: invalid shift width"));
481+ return false;
463482 }
464483
465484 /* Applies binary operator `ttype' to the given operands `v1' and `v2'. */
Show on old repository browser