diff options
Diffstat (limited to 'generic/tclStringObj.c')
-rw-r--r-- | generic/tclStringObj.c | 103 |
1 files changed, 40 insertions, 63 deletions
diff --git a/generic/tclStringObj.c b/generic/tclStringObj.c index e4ddf37..d607c1d 100644 --- a/generic/tclStringObj.c +++ b/generic/tclStringObj.c @@ -314,9 +314,9 @@ Tcl_Obj * Tcl_DbNewStringObj( const char *bytes, /* Points to the first of the length bytes * used to initialize the new object. */ - int length, /* The number of bytes to copy from "bytes" + size_t length, /* The number of bytes to copy from "bytes" * when initializing the new object. If - * negative, use bytes up to the first NUL + * (size_t)-1, use bytes up to the first NUL * byte. */ const char *file, /* The name of the source file calling this * function; used for debugging. */ @@ -325,7 +325,7 @@ Tcl_DbNewStringObj( { Tcl_Obj *objPtr; - if (length < 0) { + if (length == (size_t)-1) { length = (bytes? strlen(bytes) : 0); } TclDbNewObj(objPtr, file, line); @@ -337,9 +337,9 @@ Tcl_Obj * Tcl_DbNewStringObj( const char *bytes, /* Points to the first of the length bytes * used to initialize the new object. */ - int length, /* The number of bytes to copy from "bytes" + size_t length, /* The number of bytes to copy from "bytes" * when initializing the new object. If - * negative, use bytes up to the first NUL + * (size_t)-1, use bytes up to the first NUL * byte. */ const char *file, /* The name of the source file calling this * function; used for debugging. */ @@ -400,13 +400,13 @@ Tcl_NewUnicodeObj( *---------------------------------------------------------------------- */ -int +size_t Tcl_GetCharLength( Tcl_Obj *objPtr) /* The String object to get the num chars * of. */ { String *stringPtr; - int numChars; + size_t numChars; /* * Quick, no-shimmer return for short string reps. @@ -442,7 +442,7 @@ Tcl_GetCharLength( * If numChars is unknown, compute it. */ - if (numChars == -1) { + if (numChars == (size_t)-1) { TclNumUtfChars(numChars, objPtr->bytes, objPtr->length); stringPtr->numChars = numChars; } @@ -680,8 +680,8 @@ Tcl_SetStringObj( Tcl_Obj *objPtr, /* Object whose internal rep to init. */ const char *bytes, /* Points to the first of the length bytes * used to initialize the object. */ - int length) /* The number of bytes to copy from "bytes" - * when initializing the object. If negative, + size_t length) /* The number of bytes to copy from "bytes" + * when initializing the object. If (size_t)-1, * use bytes up to the first NUL byte.*/ { if (Tcl_IsShared(objPtr)) { @@ -700,7 +700,7 @@ Tcl_SetStringObj( */ TclInvalidateStringRep(objPtr); - if (length < 0) { + if (length == (size_t)-1) { length = (bytes? strlen(bytes) : 0); } TclInitStringRep(objPtr, bytes, length); @@ -733,21 +733,12 @@ void Tcl_SetObjLength( Tcl_Obj *objPtr, /* Pointer to object. This object must not * currently be shared. */ - int length) /* Number of bytes desired for string + size_t length) /* Number of bytes desired for string * representation of object, not including * terminating null byte. */ { String *stringPtr; - if (length < 0) { - /* - * Setting to a negative length is nonsense. This is probably the - * result of overflowing the signed integer range. - */ - - Tcl_Panic("Tcl_SetObjLength: negative length requested: " - "%d (integer overflow?)", length); - } if (Tcl_IsShared(objPtr)) { Tcl_Panic("%s called with shared object", "Tcl_SetObjLength"); } @@ -763,7 +754,7 @@ Tcl_SetObjLength( /* * Change length of an existing string rep. */ - if ((size_t)length > stringPtr->allocated) { + if (length > stringPtr->allocated) { /* * Need to enlarge the buffer. */ @@ -838,20 +829,12 @@ int Tcl_AttemptSetObjLength( Tcl_Obj *objPtr, /* Pointer to object. This object must not * currently be shared. */ - int length) /* Number of bytes desired for string + size_t length) /* Number of bytes desired for string * representation of object, not including * terminating null byte. */ { String *stringPtr; - if (length < 0) { - /* - * Setting to a negative length is nonsense. This is probably the - * result of overflowing the signed integer range. - */ - - return 0; - } if (Tcl_IsShared(objPtr)) { Tcl_Panic("%s called with shared object", "Tcl_AttemptSetObjLength"); } @@ -866,7 +849,7 @@ Tcl_AttemptSetObjLength( /* * Change length of an existing string rep. */ - if ((size_t)length > stringPtr->allocated) { + if (length > stringPtr->allocated) { /* * Need to enlarge the buffer. */ @@ -899,10 +882,10 @@ Tcl_AttemptSetObjLength( * Changing length of pure unicode string. */ - if ((size_t)length > STRING_MAXCHARS) { + if (length > STRING_MAXCHARS) { return 0; } - if ((size_t)length > stringPtr->maxChars) { + if (length > stringPtr->maxChars) { stringPtr = stringAttemptRealloc(stringPtr, length); if (stringPtr == NULL) { return 0; @@ -948,7 +931,7 @@ Tcl_SetUnicodeObj( Tcl_Obj *objPtr, /* The object to set the string of. */ const Tcl_UniChar *unicode, /* The unicode string used to initialize the * object. */ - int numChars) /* Number of characters in the unicode + size_t numChars) /* Number of characters in the unicode * string. */ { if (Tcl_IsShared(objPtr)) { @@ -1029,23 +1012,23 @@ Tcl_AppendLimitedToObj( Tcl_Obj *objPtr, /* Points to the object to append to. */ const char *bytes, /* Points to the bytes to append to the * object. */ - int length, /* The number of bytes available to be - * appended from "bytes". If < 0, then all - * bytes up to a NUL byte are available. */ - int limit, /* The maximum number of bytes to append to + size_t length, /* The number of bytes available to be + * appended from "bytes". If (size_t)-1, then + * all bytes up to a NUL byte are available. */ + size_t limit, /* The maximum number of bytes to append to * the object. */ const char *ellipsis) /* Ellipsis marker string, appended to the * object to indicate not all available bytes * at "bytes" were appended. */ { String *stringPtr; - int toCopy = 0; + size_t toCopy = 0; if (Tcl_IsShared(objPtr)) { Tcl_Panic("%s called with shared object", "Tcl_AppendLimitedToObj"); } - if (length < 0) { + if (length == (size_t)-1) { length = (bytes ? strlen(bytes) : 0); } if (length == 0) { @@ -1059,7 +1042,7 @@ Tcl_AppendLimitedToObj( ellipsis = "..."; } toCopy = (bytes == NULL) ? limit - : Tcl_UtfPrev(bytes+limit+1-strlen(ellipsis), bytes) - bytes; + : (size_t)(Tcl_UtfPrev(bytes+limit+1-strlen(ellipsis), bytes) - bytes); } /* @@ -1111,11 +1094,11 @@ Tcl_AppendToObj( Tcl_Obj *objPtr, /* Points to the object to append to. */ const char *bytes, /* Points to the bytes to append to the * object. */ - int length) /* The number of bytes to append from "bytes". - * If < 0, then append all bytes up to NUL + size_t length) /* The number of bytes to append from "bytes". + * If (size_t)-1, then append all bytes up to NUL * byte. */ { - Tcl_AppendLimitedToObj(objPtr, bytes, length, INT_MAX, NULL); + Tcl_AppendLimitedToObj(objPtr, bytes, length, (size_t)-1, NULL); } /* @@ -1140,7 +1123,7 @@ Tcl_AppendUnicodeToObj( Tcl_Obj *objPtr, /* Points to the object to append to. */ const Tcl_UniChar *unicode, /* The unicode string to append to the * object. */ - int length) /* Number of chars in "unicode". */ + size_t length) /* Number of chars in "unicode". */ { String *stringPtr; @@ -1504,9 +1487,6 @@ AppendUtfToUtfRep( } oldLength = objPtr->length; newLength = numBytes + oldLength; - if ((int)newLength < 0) { - Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX); - } stringPtr = GET_STRING(objPtr); if (newLength > stringPtr->allocated) { @@ -3238,10 +3218,10 @@ TclStringFind( } lh = Tcl_GetCharLength(haystack); - if (haystack->bytes && (lh == haystack->length)) { + if (haystack->bytes && ((size_t)lh == haystack->length)) { /* haystack is all single-byte chars */ - if (needle->bytes && (ln == needle->length)) { + if (needle->bytes && ((size_t)ln == needle->length)) { /* needle is also all single-byte chars */ char *found = strstr(haystack->bytes + start, needle->bytes); @@ -3340,10 +3320,10 @@ TclStringLast( if (last + 1 > lh) { last = lh - 1; } - if (haystack->bytes && (lh == haystack->length)) { + if (haystack->bytes && ((size_t)lh == haystack->length)) { /* haystack is all single-byte chars */ - if (needle->bytes && (ln == needle->length)) { + if (needle->bytes && ((size_t)ln == needle->length)) { /* needle is also all single-byte chars */ char *try = haystack->bytes + last + 1 - ln; @@ -3402,7 +3382,7 @@ static void ReverseBytes( unsigned char *to, /* Copy bytes into here... */ unsigned char *from, /* ...from here... */ - int count) /* Until this many are copied, */ + size_t count) /* Until this many are copied, */ /* reversing as you go. */ { unsigned char *src = from + count; @@ -3471,8 +3451,8 @@ TclStringObjReverse( } if (objPtr->bytes) { - int numChars = stringPtr->numChars; - int numBytes = objPtr->length; + size_t numChars = stringPtr->numChars; + size_t numBytes = objPtr->length; char *to, *from = objPtr->bytes; if (Tcl_IsShared(objPtr)) { @@ -3481,7 +3461,7 @@ TclStringObjReverse( } to = objPtr->bytes; - if (numChars < numBytes) { + if ((numChars == (size_t)-1) || (numChars < numBytes)) { /* * Either numChars == -1 and we don't know how many chars are * represented by objPtr->bytes and we need Pass 1 just in case, @@ -3490,8 +3470,8 @@ TclStringObjReverse( * * Pass 1. Reverse the bytes of each multi-byte character. */ - int charCount = 0; - int bytesLeft = numBytes; + size_t charCount = 0; + size_t bytesLeft = numBytes; while (bytesLeft) { /* @@ -3499,7 +3479,7 @@ TclStringObjReverse( * It's part of the contract for objPtr->bytes values. * Thus, we can skip calling Tcl_UtfCharComplete() here. */ - int bytesInChar = TclUtfToUniChar(from, &ch); + size_t bytesInChar = TclUtfToUniChar(from, &ch); ReverseBytes((unsigned char *)to, (unsigned char *)from, bytesInChar); @@ -3787,9 +3767,6 @@ ExtendStringRepWithUnicode( for (i = 0; i < numChars; i++) { size += TclUtfCount(unicode[i]); } - if ((int)size < 0) { - Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX); - } /* * Grow space if needed. |