From c823ca95335a06308832f99a2df09c80f0cd759c Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Wed, 8 Jun 2022 09:48:10 +0200 Subject: [PATCH] Fix binary.write_int --- binary.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/binary.lua b/binary.lua index 48502e4..25b11a1 100644 --- a/binary.lua +++ b/binary.lua @@ -84,12 +84,12 @@ function write_uint(write_byte, uint, bytes) end function write_int(write_byte, int, bytes) - local max = 0x100 ^ bytes / 2 + local max = 0x100 ^ bytes if int < 0 then - -- No bound checking is needed: If the int is too small, the uint will be too big - int = max - int + assert(-int <= max / 2) + int = max + int else - assert(int < max) + assert(int < max / 2) end return write_uint(write_byte, int, bytes) end