From c746cc2da07e1089b0a293045fbd77d31016aef1 Mon Sep 17 00:00:00 2001
From: Botond Baranyi <botond.baranyi@ericsson.com>
Date: Tue, 3 Apr 2018 15:29:20 +0200
Subject: [PATCH] Fixed RAW encoding of -2147483648 (bug 533067)

Change-Id: I5984f0dea8ca8cd45589915d7a6b180d34286a71
Signed-off-by: Botond Baranyi <botond.baranyi@ericsson.com>
---
 core/Integer.cc | 2 +-
 core/RAW.cc     | 7 +++++--
 core/RAW.hh     | 2 +-
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/core/Integer.cc b/core/Integer.cc
index 1301fae04..42e5eb984 100644
--- a/core/Integer.cc
+++ b/core/Integer.cc
@@ -1177,7 +1177,7 @@ int INTEGER::RAW_encode(const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf)
   }
   else { // not IntX, use the field length
     length = (p_td.raw->fieldlength + 7) / 8;
-    if (min_bits(value) > p_td.raw->fieldlength) {
+    if (min_bits(value, neg_sgbit) > p_td.raw->fieldlength) {
       TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_LEN_ERR,
         "There are insufficient bits to encode '%s' : ", p_td.name);
       value = 0; // substitute with zero
diff --git a/core/RAW.cc b/core/RAW.cc
index 3a220ae71..80d218084 100644
--- a/core/RAW.cc
+++ b/core/RAW.cc
@@ -309,13 +309,16 @@ RAW_enc_tree* RAW_enc_tree::get_node(RAW_enc_tr_pos &req_pos)
  * @return the number of bits needed to represent the given integer
  * in sign+magnitude
  */
-int min_bits(int a)
+int min_bits(int a, bool signbit)
 {
+  if (a == INT_MIN) {
+    return sizeof(int) * 8 + signbit;
+  }
   register int bits = 0;
   register int tmp = a;
   if (a < 0) {
     bits = 1;
-    tmp = -a;
+    tmp = -a; // doesn't work on INT_MIN
   }
   while (tmp != 0) {
     bits++;
diff --git a/core/RAW.hh b/core/RAW.hh
index 410566776..6c32d68f7 100644
--- a/core/RAW.hh
+++ b/core/RAW.hh
@@ -52,7 +52,7 @@ extern const unsigned char BitMaskTable[9];
 class RAW_enc_tree;
 struct TTCN_Typedescriptor_t;
 struct TTCN_TEXTdescriptor_t;
-int min_bits(int a);
+int min_bits(int a, bool signbit);
 int min_bits(BIGNUM *a);
 enum ext_bit_t{
   EXT_BIT_NO,      /**< No extension bit */
-- 
GitLab