[njs] Fixed undefined behaviour in left shift of negative numbers.

Valentin Bartenev vbart at nginx.com
Sun Jul 28 14:20:54 UTC 2019


details:   https://hg.nginx.org/njs/rev/6be62551e6d4
branches:  
changeset: 1081:6be62551e6d4
user:      Valentin Bartenev <vbart at nginx.com>
date:      Sun Jul 28 17:19:51 2019 +0300
description:
Fixed undefined behaviour in left shift of negative numbers.

Now it's implementation defined.

diffstat:

 njs/njs_vmcode.c |  3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diffs (13 lines):

diff -r 07989e97b198 -r 6be62551e6d4 njs/njs_vmcode.c
--- a/njs/njs_vmcode.c	Sun Jul 28 15:00:40 2019 +0300
+++ b/njs/njs_vmcode.c	Sun Jul 28 17:19:51 2019 +0300
@@ -421,7 +421,8 @@ next:
                         i32 = njs_number_to_int32(num);
 
                         if (op == NJS_VMCODE_LEFT_SHIFT) {
-                            i32 <<= u32;
+                            /* Shifting of negative numbers is undefined. */
+                            i32 = (uint32_t) i32 << u32;
                         } else {
                             i32 >>= u32;
                         }


More information about the nginx-devel mailing list