Tcl Source Code

Check-in [ec70581d16]
Login

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

Overview
Comment:Continue consistent use of LLONG_MIN and LLONG_MAX.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | index-range-and-overflow
Files: files | file ages | folders
SHA3-256: ec70581d16d9b16c267637b41396698f8c47bc3217a4d39fb1c09af1ce16f604
User & Date: dgp 2018-03-01 04:19:37.384
Context
2018-03-01
19:30
Avoid full list conversion when we can cheaply discover a multi-element list is not possible. check-in: ca6ab1446a user: dgp tags: index-range-and-overflow
04:19
Continue consistent use of LLONG_MIN and LLONG_MAX. check-in: ec70581d16 user: dgp tags: index-range-and-overflow
04:13
merge 8.7 check-in: c3b9bfbc3d user: dgp tags: index-range-and-overflow
Changes
Unified Diff Ignore Whitespace Patch
Changes to generic/tclUtil.c.
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include "tclInt.h"
#include "tclParse.h"
#include "tclStringTrim.h"
#include "tommath.h"
#include <math.h>

const Tcl_WideInt wideMax = ((~(Tcl_WideUInt)0) >> 1);

/*
 * The absolute pathname of the executable in which this Tcl library is
 * running.
 */

static ProcessGlobalValue executableName = {
    0, 0, NULL, NULL, NULL, NULL, NULL







<
<







14
15
16
17
18
19
20


21
22
23
24
25
26
27

#include "tclInt.h"
#include "tclParse.h"
#include "tclStringTrim.h"
#include "tommath.h"
#include <math.h>



/*
 * The absolute pathname of the executable in which this Tcl library is
 * running.
 */

static ProcessGlobalValue executableName = {
    0, 0, NULL, NULL, NULL, NULL, NULL
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
	    return TCL_OK;
	}
	if (numType == TCL_NUMBER_BIG) {
	    /* objPtr holds an integer outside the signed wide range */
	    mp_int *bigPtr = (mp_int *)cd;

	    if (mp_isneg(bigPtr)) {
		*widePtr = ~wideMax;
	    } else {
		*widePtr = wideMax;
	    }
	    return TCL_OK;
	}

	/* Must be a double -> not a valid index */
	goto parseError;
    }

    /* objPtr does not hold a number, check the end+/- format... */

    if (SetEndOffsetFromAny(NULL, objPtr) == TCL_OK) {
	Tcl_WideInt offset = objPtr->internalRep.wideValue;

	if ((endValue ^ offset) < 0) {
	    /* Different signs, sum cannot overflow */
	    *widePtr = endValue + offset;
	} else if (endValue >= 0) {
	    if (endValue < wideMax - offset) {
		*widePtr = endValue + offset;
	    } else {
		*widePtr = wideMax;
	    }
	} else {
	    if (endValue > ~wideMax - offset) {
		*widePtr = endValue + offset;
	    } else {
		*widePtr = ~wideMax;
	    }
	}
	return TCL_OK;
    }

    if (TCL_OK == Tcl_ListObjLength(NULL, objPtr, &length) && (length > 1)) {
	goto parseError;







|

|

















|


|


|


|







3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
	    return TCL_OK;
	}
	if (numType == TCL_NUMBER_BIG) {
	    /* objPtr holds an integer outside the signed wide range */
	    mp_int *bigPtr = (mp_int *)cd;

	    if (mp_isneg(bigPtr)) {
		*widePtr = LLONG_MIN;
	    } else {
		*widePtr = LLONG_MAX;
	    }
	    return TCL_OK;
	}

	/* Must be a double -> not a valid index */
	goto parseError;
    }

    /* objPtr does not hold a number, check the end+/- format... */

    if (SetEndOffsetFromAny(NULL, objPtr) == TCL_OK) {
	Tcl_WideInt offset = objPtr->internalRep.wideValue;

	if ((endValue ^ offset) < 0) {
	    /* Different signs, sum cannot overflow */
	    *widePtr = endValue + offset;
	} else if (endValue >= 0) {
	    if (endValue < LLONG_MAX - offset) {
		*widePtr = endValue + offset;
	    } else {
		*widePtr = LLONG_MAX;
	    }
	} else {
	    if (endValue > LLONG_MIN - offset) {
		*widePtr = endValue + offset;
	    } else {
		*widePtr = LLONG_MIN;
	    }
	}
	return TCL_OK;
    }

    if (TCL_OK == Tcl_ListObjLength(NULL, objPtr, &length) && (length > 1)) {
	goto parseError;
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
		    NULL, TCL_PARSE_INTEGER_ONLY)) {
		TclGetNumberFromObj(NULL, objPtr, &cd, &numType);
		if (numType == TCL_NUMBER_WIDE) {
		    Tcl_WideInt w2 = (*(Tcl_WideInt *)cd);

		    TclFreeIntRep(objPtr);
		    if (*opPtr == '-') {
			if (w2 == ~wideMax) {
			    /* TODO: need bignum */
			    goto parseError;
			}
			w2 = -w2;
		    }

		    if ((w1 ^ w2) < 0) {
			*widePtr = w1 + w2;
		    } else if (w1 >= 0) {
			if (w1 < wideMax - w2) {
			    *widePtr = w1 + w2;
			} else {
			    *widePtr = wideMax;
			}
		    } else {
			if (w1 > ~wideMax - w2) {
			    *widePtr = w1 + w2;
			} else {
			    *widePtr = ~wideMax;
			}
		    }
		    return TCL_OK;
		}
		/* TODO: 2d half is bignum */
		goto parseError;
	    }







|









|


|


|


|







3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
		    NULL, TCL_PARSE_INTEGER_ONLY)) {
		TclGetNumberFromObj(NULL, objPtr, &cd, &numType);
		if (numType == TCL_NUMBER_WIDE) {
		    Tcl_WideInt w2 = (*(Tcl_WideInt *)cd);

		    TclFreeIntRep(objPtr);
		    if (*opPtr == '-') {
			if (w2 == LLONG_MIN) {
			    /* TODO: need bignum */
			    goto parseError;
			}
			w2 = -w2;
		    }

		    if ((w1 ^ w2) < 0) {
			*widePtr = w1 + w2;
		    } else if (w1 >= 0) {
			if (w1 < LLONG_MAX - w2) {
			    *widePtr = w1 + w2;
			} else {
			    *widePtr = LLONG_MAX;
			}
		    } else {
			if (w1 > LLONG_MIN - w2) {
			    *widePtr = w1 + w2;
			} else {
			    *widePtr = LLONG_MIN;
			}
		    }
		    return TCL_OK;
		}
		/* TODO: 2d half is bignum */
		goto parseError;
	    }
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
	}
	TclGetNumberFromObj(NULL, objPtr, &cd, &numType);
	if (numType == TCL_NUMBER_BIG) {
	    /* objPtr holds an integer outside the signed wide range */
	    mp_int *bigPtr = (mp_int *)cd;

	    if (mp_isneg(bigPtr)) {
		offset = ~wideMax;
	    } else {
		offset = wideMax;
	    }
	} else if (numType == TCL_NUMBER_WIDE) {
	    offset = (*(Tcl_WideInt *)cd);
	} else {
	    /* Can't happen? */
	    goto badIndexFormat;
	}
	if (bytes[3] == '-') {
	    if (offset == ~wideMax) {
		offset = wideMax;
	    } else {
		offset = -offset;
	    }
	}
    } else {
	/*
	 * Conversion failed. Report the error.







|

|








|
|







3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
	}
	TclGetNumberFromObj(NULL, objPtr, &cd, &numType);
	if (numType == TCL_NUMBER_BIG) {
	    /* objPtr holds an integer outside the signed wide range */
	    mp_int *bigPtr = (mp_int *)cd;

	    if (mp_isneg(bigPtr)) {
		offset = LLONG_MIN;
	    } else {
		offset = LLONG_MAX;
	    }
	} else if (numType == TCL_NUMBER_WIDE) {
	    offset = (*(Tcl_WideInt *)cd);
	} else {
	    /* Can't happen? */
	    goto badIndexFormat;
	}
	if (bytes[3] == '-') {
	    if (offset == LLONG_MIN) {
		offset = LLONG_MAX;
	    } else {
		offset = -offset;
	    }
	}
    } else {
	/*
	 * Conversion failed. Report the error.