From 44774dc3238ca1e269c6acedcbdfb449780c96f9 Mon Sep 17 00:00:00 2001
From: "jan.nijtmans" <nijtmans@users.sourceforge.net>
Date: Thu, 17 Nov 2016 15:30:32 +0000
Subject: Restore bn_mp_radix_size.c to exact copy of libtommath-1.0 version:
 Since the radix_size of "9" should return 2, not 3. Add test-case to prove
 that.

---
 generic/tclObj.c              |  8 +++-----
 generic/tclTestObj.c          | 28 ++++++++++++++++++++++++++--
 libtommath/bn_mp_radix_size.c | 13 ++-----------
 tests/obj.test                |  6 ++++++
 4 files changed, 37 insertions(+), 18 deletions(-)

diff --git a/generic/tclObj.c b/generic/tclObj.c
index d3f59ec..6c850af 100644
--- a/generic/tclObj.c
+++ b/generic/tclObj.c
@@ -3252,13 +3252,11 @@ UpdateStringOfBignum(
     if (status != MP_OKAY) {
 	Tcl_Panic("radix size failure in UpdateStringOfBignum");
     }
-    if (size == 3) {
+    if (size < 2) {
 	/*
-	 * mp_radix_size() returns 3 when more than INT_MAX bytes would be
+	 * mp_radix_size() returns < 2 when more than INT_MAX bytes would be
 	 * needed to hold the string rep (because mp_radix_size ignores
-	 * integer overflow issues). When we know the string rep will be more
-	 * than 3, we can conclude the string rep would overflow our string
-	 * length limits.
+	 * integer overflow issues).
 	 *
 	 * Note that so long as we enforce our bignums to the size that fits
 	 * in a packed bignum, this branch will never be taken.
diff --git a/generic/tclTestObj.c b/generic/tclTestObj.c
index 1cbe67a..06f8e5f 100644
--- a/generic/tclTestObj.c
+++ b/generic/tclTestObj.c
@@ -152,10 +152,11 @@ TestbignumobjCmd(
     Tcl_Obj *const objv[])	/* Argument vector */
 {
     const char *const subcmds[] = {
-	"set",	    "get",	"mult10",	"div10",	"iseven", NULL
+	"set", "get", "mult10", "div10", "iseven", "radixsize", NULL
     };
     enum options {
-	BIGNUM_SET, BIGNUM_GET,	BIGNUM_MULT10,	BIGNUM_DIV10,	BIGNUM_ISEVEN
+	BIGNUM_SET, BIGNUM_GET, BIGNUM_MULT10, BIGNUM_DIV10, BIGNUM_ISEVEN,
+	BIGNUM_RADIXSIZE
     };
     int index, varIndex;
     const char *string;
@@ -295,6 +296,29 @@ TestbignumobjCmd(
 	}
 	mp_clear(&bignumValue);
 	break;
+
+    case BIGNUM_RADIXSIZE:
+	if (objc != 3) {
+	    Tcl_WrongNumArgs(interp, 2, objv, "varIndex");
+	    return TCL_ERROR;
+	}
+	if (CheckIfVarUnset(interp, varPtr,varIndex)) {
+	    return TCL_ERROR;
+	}
+	if (Tcl_GetBignumFromObj(interp, varPtr[varIndex],
+		&bignumValue) != TCL_OK) {
+	    return TCL_ERROR;
+	}
+	if (mp_radix_size(&bignumValue, 10, &index) != MP_OKAY) {
+	    return TCL_ERROR;
+	}
+	if (!Tcl_IsShared(varPtr[varIndex])) {
+	    Tcl_SetIntObj(varPtr[varIndex], index);
+	} else {
+	    SetVarToObj(varPtr, varIndex, Tcl_NewIntObj(index));
+	}
+	mp_clear(&bignumValue);
+	break;
     }
 
     Tcl_SetObjResult(interp, varPtr[varIndex]);
diff --git a/libtommath/bn_mp_radix_size.c b/libtommath/bn_mp_radix_size.c
index 9c60401..e5d7772 100644
--- a/libtommath/bn_mp_radix_size.c
+++ b/libtommath/bn_mp_radix_size.c
@@ -66,17 +66,8 @@ int mp_radix_size (mp_int * a, int radix, int *size)
   }
   mp_clear (&t);
 
-  /* 
-   * return digs + 1, the 1 is for the NULL byte that would be required.
-   * mp_toradix_n requires a minimum of 3 bytes, so never report less than
-   * that.
-   */
-
-  if ( digs >= 2 ) {
-      *size = digs + 1;
-  } else {
-      *size = 3;
-  }
+  /* return digs + 1, the 1 is for the NULL byte that would be required. */
+  *size = digs + 1;
   return MP_OKAY;
 }
 
diff --git a/tests/obj.test b/tests/obj.test
index c354d69..833c906 100644
--- a/tests/obj.test
+++ b/tests/obj.test
@@ -631,6 +631,12 @@ test obj-34.1 {mp_iseven} testobj {
     lappend result [testbignumobj iseven 1]    ;
     lappend result [testobj type 1]
 } {0 1 int}
+test obj-34.2 {mp_radix_size} testobj {
+    set result ""
+    lappend result [testbignumobj set 1 9]
+    lappend result [testbignumobj radixsize 1]    ;
+    lappend result [testobj type 1]
+} {9 2 int}
 
 if {[testConstraint testobj]} {
     testobj freeallvars
-- 
cgit v0.12