Tcl Source Code

Check-in [8cf7627603]
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to [email protected]
or submit via the online form by Sep 9.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment: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.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | libtommath-1.0
Files: files | file ages | folders
SHA1: 8cf7627603003f1d8c821e79dcb691d54451ea5f
User & Date: jan.nijtmans 2016-11-17 15:30:32
Context
2016-11-18
09:50
Fix [e6f27aa56f]: Update libtommath to 1.0 check-in: 6d14b81579 user: jan.nijtmans tags: trunk
2016-11-17
15:30
Restore bn_mp_radix_size.c to exact copy of libtommath-1.0 version: Since the radix_size of "9" shou... Closed-Leaf check-in: 8cf7627603 user: jan.nijtmans tags: libtommath-1.0
13:18
Restore bn_mp_add_d.c to exact copy of libtommath-1.0 version: Since the mp_clamp() function already... check-in: 7442311ee4 user: jan.nijtmans tags: libtommath-1.0
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/tclObj.c.

3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
    char *stringVal;

    UNPACK_BIGNUM(objPtr, bignumVal);
    status = mp_radix_size(&bignumVal, 10, &size);
    if (status != MP_OKAY) {
	Tcl_Panic("radix size failure in UpdateStringOfBignum");
    }
    if (size == 3) {
	/*
	 * mp_radix_size() returns 3 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.
	 *
	 * Note that so long as we enforce our bignums to the size that fits
	 * in a packed bignum, this branch will never be taken.
	 */

	Tcl_Panic("UpdateStringOfBignum: string length limit exceeded");
    }






|

|

|
<
<







3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259


3260
3261
3262
3263
3264
3265
3266
    char *stringVal;

    UNPACK_BIGNUM(objPtr, bignumVal);
    status = mp_radix_size(&bignumVal, 10, &size);
    if (status != MP_OKAY) {
	Tcl_Panic("radix size failure in UpdateStringOfBignum");
    }
    if (size < 2) {
	/*
	 * 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).


	 *
	 * Note that so long as we enforce our bignums to the size that fits
	 * in a packed bignum, this branch will never be taken.
	 */

	Tcl_Panic("UpdateStringOfBignum: string length limit exceeded");
    }

Changes to generic/tclTestObj.c.

148
149
150
151
152
153
154
155
156
157
158

159
160
161
162
163
164
165
...
289
290
291
292
293
294
295























296
297
298
299
300
301
302
TestbignumobjCmd(
    ClientData clientData,	/* unused */
    Tcl_Interp *interp,		/* Tcl interpreter */
    int objc,			/* Argument count */
    Tcl_Obj *const objv[])	/* Argument vector */
{
    const char *const subcmds[] = {
	"set",	    "get",	"mult10",	"div10",	"iseven", NULL
    };
    enum options {
	BIGNUM_SET, BIGNUM_GET,	BIGNUM_MULT10,	BIGNUM_DIV10,	BIGNUM_ISEVEN

    };
    int index, varIndex;
    const char *string;
    mp_int bignumValue, newValue;
    Tcl_Obj **varPtr;

    if (objc < 3) {
................................................................................
	    return TCL_ERROR;
	}
	if (!Tcl_IsShared(varPtr[varIndex])) {
	    Tcl_SetIntObj(varPtr[varIndex], mp_iseven(&bignumValue));
	} else {
	    SetVarToObj(varPtr, varIndex, Tcl_NewIntObj(mp_iseven(&bignumValue)));
	}























	mp_clear(&bignumValue);
	break;
    }

    Tcl_SetObjResult(interp, varPtr[varIndex]);
    return TCL_OK;
}






|


|
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
TestbignumobjCmd(
    ClientData clientData,	/* unused */
    Tcl_Interp *interp,		/* Tcl interpreter */
    int objc,			/* Argument count */
    Tcl_Obj *const objv[])	/* Argument vector */
{
    const char *const subcmds[] = {
	"set", "get", "mult10", "div10", "iseven", "radixsize", NULL
    };
    enum options {
	BIGNUM_SET, BIGNUM_GET, BIGNUM_MULT10, BIGNUM_DIV10, BIGNUM_ISEVEN,
	BIGNUM_RADIXSIZE
    };
    int index, varIndex;
    const char *string;
    mp_int bignumValue, newValue;
    Tcl_Obj **varPtr;

    if (objc < 3) {
................................................................................
	    return TCL_ERROR;
	}
	if (!Tcl_IsShared(varPtr[varIndex])) {
	    Tcl_SetIntObj(varPtr[varIndex], mp_iseven(&bignumValue));
	} else {
	    SetVarToObj(varPtr, varIndex, Tcl_NewIntObj(mp_iseven(&bignumValue)));
	}
	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]);
    return TCL_OK;
}

Changes to libtommath/bn_mp_radix_size.c.

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
      mp_clear (&t);
      return res;
    }
    ++digs;
  }
  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 MP_OKAY;
}

#endif

/* $Source$ */
/* $Revision$ */
/* $Date$ */






<
|
<
<
<
<
<
|
<
<
<








62
63
64
65
66
67
68

69





70



71
72
73
74
75
76
77
78
      mp_clear (&t);
      return res;
    }
    ++digs;
  }
  mp_clear (&t);


  /* return digs + 1, the 1 is for the NULL byte that would be required. */





  *size = digs + 1;



  return MP_OKAY;
}

#endif

/* $Source$ */
/* $Revision$ */
/* $Date$ */

Changes to tests/obj.test.

627
628
629
630
631
632
633






634
635
636
637
638
639
640
641
test obj-34.1 {mp_iseven} testobj {
    set result ""
    lappend result [testbignumobj set 1 0]
    lappend result [testbignumobj iseven 1]    ;
    lappend result [testobj type 1]
} {0 1 int}







if {[testConstraint testobj]} {
    testobj freeallvars
}

# cleanup
::tcltest::cleanupTests
return






>
>
>
>
>
>








627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
test obj-34.1 {mp_iseven} testobj {
    set result ""
    lappend result [testbignumobj set 1 0]
    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
}

# cleanup
::tcltest::cleanupTests
return