summaryrefslogtreecommitdiffstatshomepage
path: root/py/runtime.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-11-02 02:39:41 +0200
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-11-02 02:41:30 +0200
commit039887a0ac9bd02cfd84e4792d3d21a78bc06a7f (patch)
tree009fccb2375d12d3bdc0302e346b6901753bd6d6 /py/runtime.c
parenta58713a899b7df5b291e868d2553b2333c0b3dc9 (diff)
downloadmicropython-039887a0ac9bd02cfd84e4792d3d21a78bc06a7f.tar.gz
micropython-039887a0ac9bd02cfd84e4792d3d21a78bc06a7f.zip
py: Fix bug with right-shifting small ints by large amounts.
Undefined behavior in C, needs explicit check.
Diffstat (limited to 'py/runtime.c')
-rw-r--r--py/runtime.c5
1 files changed, 5 insertions, 0 deletions
diff --git a/py/runtime.c b/py/runtime.c
index f6f34be940..e225ba8b05 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -337,6 +337,11 @@ mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
} else {
// standard precision is enough for right-shift
+ if (rhs_val >= BITS_PER_WORD) {
+ // Shifting to big amounts is underfined behavior
+ // in C and is CPU-dependent; propagate sign bit.
+ rhs_val = BITS_PER_WORD - 1;
+ }
lhs_val >>= rhs_val;
}
break;