summaryrefslogtreecommitdiffstats
path: root/generic/tclUtil.c
diff options
context:
space:
mode:
Diffstat (limited to 'generic/tclUtil.c')
-rw-r--r--generic/tclUtil.c297
1 files changed, 141 insertions, 156 deletions
diff --git a/generic/tclUtil.c b/generic/tclUtil.c
index c5833c5..9a35e4e 100644
--- a/generic/tclUtil.c
+++ b/generic/tclUtil.c
@@ -108,7 +108,7 @@ static int SetEndOffsetFromAny(Tcl_Interp *interp,
static int FindElement(Tcl_Interp *interp, const char *string,
int stringLength, const char *typeStr,
const char *typeCode, const char **elementPtr,
- const char **nextPtr, int *sizePtr,
+ const char **nextPtr, size_t *sizePtr,
int *literalPtr);
/*
* The following is the Tcl object type definition for an object that
@@ -385,12 +385,12 @@ static const Tcl_ObjType endOffsetType = {
int
TclMaxListLength(
const char *bytes,
- int numBytes,
+ size_t numBytes,
const char **endPtr)
{
- int count = 0;
+ size_t count = 0;
- if ((numBytes == 0) || ((numBytes == -1) && (*bytes == '\0'))) {
+ if ((numBytes == 0) || ((numBytes == TCL_AUTO_LENGTH) && (*bytes == '\0'))) {
/* Empty string case - quick exit */
goto done;
}
@@ -406,7 +406,7 @@ TclMaxListLength(
*/
while (numBytes) {
- if ((numBytes == -1) && (*bytes == '\0')) {
+ if ((numBytes == TCL_AUTO_LENGTH) && (*bytes == '\0')) {
break;
}
if (TclIsSpaceProc(*bytes)) {
@@ -417,9 +417,9 @@ TclMaxListLength(
count++;
do {
bytes++;
- numBytes -= (numBytes != -1);
+ numBytes -= (numBytes != TCL_AUTO_LENGTH);
} while (numBytes && TclIsSpaceProc(*bytes));
- if ((numBytes == 0) || ((numBytes == -1) && (*bytes == '\0'))) {
+ if ((numBytes == 0) || ((numBytes == TCL_AUTO_LENGTH) && (*bytes == '\0'))) {
break;
}
@@ -428,7 +428,7 @@ TclMaxListLength(
*/
}
bytes++;
- numBytes -= (numBytes != -1);
+ numBytes -= (numBytes != TCL_AUTO_LENGTH);
}
/*
@@ -497,7 +497,7 @@ TclFindElement(
const char **nextPtr, /* Fill in with location of character just
* after all white space following end of
* argument (next arg or end of list). */
- int *sizePtr, /* If non-zero, fill in with size of
+ size_t *sizePtr, /* If non-zero, fill in with size of
* element. */
int *literalPtr) /* If non-zero, fill in with non-zero/zero to
* indicate that the substring of *sizePtr
@@ -526,7 +526,7 @@ TclFindDictElement(
const char **nextPtr, /* Fill in with location of character just
* after all white space following end of
* element (next arg or end of list). */
- int *sizePtr, /* If non-zero, fill in with size of
+ size_t *sizePtr, /* If non-zero, fill in with size of
* element. */
int *literalPtr) /* If non-zero, fill in with non-zero/zero to
* indicate that the substring of *sizePtr
@@ -558,7 +558,7 @@ FindElement(
const char **nextPtr, /* Fill in with location of character just
* after all white space following end of
* argument (next arg or end of list/dict). */
- int *sizePtr, /* If non-zero, fill in with size of
+ size_t *sizePtr, /* If non-zero, fill in with size of
* element. */
int *literalPtr) /* If non-zero, fill in with non-zero/zero to
* indicate that the substring of *sizePtr
@@ -573,7 +573,7 @@ FindElement(
int openBraces = 0; /* Brace nesting level during parse. */
int inQuotes = 0;
int size = 0; /* lint. */
- int numChars;
+ size_t numChars;
int literal = 1;
const char *p2;
@@ -784,20 +784,20 @@ FindElement(
*----------------------------------------------------------------------
*/
-int
+size_t
TclCopyAndCollapse(
- int count, /* Number of byte to copy from src. */
+ size_t count, /* Number of byte to copy from src. */
const char *src, /* Copy from here... */
char *dst) /* ... to here. */
{
- int newCount = 0;
+ size_t newCount = 0;
while (count > 0) {
char c = *src;
if (c == '\\') {
- int numRead;
- int backslashCount = TclParseBackslash(src, count, &numRead, dst);
+ size_t numRead;
+ size_t backslashCount = TclParseBackslash(src, count, &numRead, dst);
dst += backslashCount;
newCount += backslashCount;
@@ -855,7 +855,8 @@ Tcl_SplitList(
{
const char **argv, *end, *element;
char *p;
- int length, size, i, result, elSize;
+ int length, size, i, result;
+ size_t elSize;
/*
* Allocate enough space to work in. A (const char *) for each (possible)
@@ -867,7 +868,7 @@ Tcl_SplitList(
size = TclMaxListLength(list, -1, &end) + 1;
length = end - list;
- argv = ckalloc((size * sizeof(char *)) + length + 1);
+ argv = Tcl_Alloc((size * sizeof(char *)) + length + 1);
for (i = 0, p = ((char *) argv) + size*sizeof(char *);
*list != 0; i++) {
@@ -878,14 +879,14 @@ Tcl_SplitList(
&elSize, &literal);
length -= (list - prevList);
if (result != TCL_OK) {
- ckfree(argv);
+ Tcl_Free(argv);
return result;
}
if (*element == 0) {
break;
}
if (i >= size) {
- ckfree(argv);
+ Tcl_Free(argv);
if (interp != NULL) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"internal error in Tcl_SplitList", -1));
@@ -896,7 +897,7 @@ Tcl_SplitList(
}
argv[i] = p;
if (literal) {
- memcpy(p, element, (size_t) elSize);
+ memcpy(p, element, elSize);
p += elSize;
*p = 0;
p++;
@@ -932,11 +933,11 @@ Tcl_SplitList(
*----------------------------------------------------------------------
*/
-int
+size_t
Tcl_ScanElement(
- register const char *src, /* String to convert to list element. */
- register int *flagPtr) /* Where to store information to guide
- * Tcl_ConvertCountedElement. */
+ const char *src, /* String to convert to list element. */
+ int *flagPtr) /* Where to store information to guide
+ * Tcl_ConvertCountedElement. */
{
return Tcl_ScanCountedElement(src, -1, flagPtr);
}
@@ -949,7 +950,7 @@ Tcl_ScanElement(
* This function is a companion function to Tcl_ConvertCountedElement. It
* scans a string to see what needs to be done to it (e.g. add
* backslashes or enclosing braces) to make the string into a valid Tcl
- * list element. If length is -1, then the string is scanned from src up
+ * list element. If length is (size_t)-1, then the string is scanned from src up
* to the first null byte.
*
* Results:
@@ -964,10 +965,10 @@ Tcl_ScanElement(
*----------------------------------------------------------------------
*/
-int
+size_t
Tcl_ScanCountedElement(
const char *src, /* String to convert to Tcl list element. */
- int length, /* Number of bytes in src, or -1. */
+ size_t length, /* Number of bytes in src, or (size_t)-1. */
int *flagPtr) /* Where to store information to guide
* Tcl_ConvertElement. */
{
@@ -1008,10 +1009,10 @@ Tcl_ScanCountedElement(
*----------------------------------------------------------------------
*/
-int
+size_t
TclScanElement(
const char *src, /* String to convert to Tcl list element. */
- int length, /* Number of bytes in src, or -1. */
+ size_t length, /* Number of bytes in src, or -1. */
char *flagPtr) /* Where to store information to guide
* Tcl_ConvertElement. */
{
@@ -1032,7 +1033,7 @@ TclScanElement(
int braceCount = 0; /* Count of all braces '{' '}' seen. */
#endif /* COMPAT */
- if ((p == NULL) || (length == 0) || ((*p == '\0') && (length == -1))) {
+ if ((p == NULL) || (length == 0) || ((*p == '\0') && (length == (size_t)-1))) {
/*
* Empty string element must be brace quoted.
*/
@@ -1104,7 +1105,7 @@ TclScanElement(
break;
case '\\': /* TYPE_SUBS */
extra++; /* Escape '\' => '\\' */
- if ((length == 1) || ((length == -1) && (p[1] == '\0'))) {
+ if ((length == 1) || ((length == (size_t)-1) && (p[1] == '\0'))) {
/*
* Final backslash. Cannot format with brace quoting.
*/
@@ -1135,14 +1136,14 @@ TclScanElement(
#endif /* COMPAT */
break;
case '\0': /* TYPE_SUBS */
- if (length == -1) {
+ if (length == (size_t)-1) {
goto endOfString;
}
/* TODO: Panic on improper encoding? */
break;
}
}
- length -= (length > 0);
+ length -= (length+1 > 1);
p++;
}
@@ -1295,11 +1296,11 @@ TclScanElement(
*----------------------------------------------------------------------
*/
-int
+size_t
Tcl_ConvertElement(
- register const char *src, /* Source information for list element. */
- register char *dst, /* Place to put list-ified element. */
- register int flags) /* Flags produced by Tcl_ScanElement. */
+ const char *src, /* Source information for list element. */
+ char *dst, /* Place to put list-ified element. */
+ int flags) /* Flags produced by Tcl_ScanElement. */
{
return Tcl_ConvertCountedElement(src, -1, dst, flags);
}
@@ -1325,14 +1326,14 @@ Tcl_ConvertElement(
*----------------------------------------------------------------------
*/
-int
+size_t
Tcl_ConvertCountedElement(
register const char *src, /* Source information for list element. */
- int length, /* Number of bytes in src, or -1. */
+ size_t length, /* Number of bytes in src, or -1. */
char *dst, /* Place to put list-ified element. */
int flags) /* Flags produced by Tcl_ScanElement. */
{
- int numBytes = TclConvertElement(src, length, dst, flags);
+ size_t numBytes = TclConvertElement(src, length, dst, flags);
dst[numBytes] = '\0';
return numBytes;
}
@@ -1358,10 +1359,10 @@ Tcl_ConvertCountedElement(
*----------------------------------------------------------------------
*/
-int
+size_t
TclConvertElement(
register const char *src, /* Source information for list element. */
- int length, /* Number of bytes in src, or -1. */
+ size_t length, /* Number of bytes in src, or -1. */
char *dst, /* Place to put list-ified element. */
int flags) /* Flags produced by Tcl_ScanElement. */
{
@@ -1380,7 +1381,7 @@ TclConvertElement(
* No matter what the caller demands, empty string must be braced!
*/
- if ((src == NULL) || (length == 0) || (*src == '\0' && length == -1)) {
+ if ((src == NULL) || (length == 0) || (*src == '\0' && length == TCL_AUTO_LENGTH)) {
src = &tclEmptyString;
length = 0;
conversion = CONVERT_BRACE;
@@ -1396,7 +1397,7 @@ TclConvertElement(
p[1] = '#';
p += 2;
src++;
- length -= (length > 0);
+ length -= (length+1 > 1);
} else {
conversion = CONVERT_BRACE;
}
@@ -1407,7 +1408,7 @@ TclConvertElement(
*/
if (conversion == CONVERT_NONE) {
- if (length == -1) {
+ if (length == (size_t)-1) {
/* TODO: INT_MAX overflow? */
while (*src) {
*p++ = *src++;
@@ -1426,7 +1427,7 @@ TclConvertElement(
if (conversion == CONVERT_BRACE) {
*p = '{';
p++;
- if (length == -1) {
+ if (length == (size_t)-1) {
/* TODO: INT_MAX overflow? */
while (*src) {
*p++ = *src++;
@@ -1437,7 +1438,7 @@ TclConvertElement(
}
*p = '}';
p++;
- return p - dst;
+ return (size_t)(p - dst);
}
/* conversion == CONVERT_ESCAPE or CONVERT_MASK */
@@ -1446,7 +1447,7 @@ TclConvertElement(
* Formatted string is original string converted to escape sequences.
*/
- for ( ; length; src++, length -= (length > 0)) {
+ for ( ; length; src++, length -= (length+1 > 1)) {
switch (*src) {
case ']':
case '[':
@@ -1499,8 +1500,8 @@ TclConvertElement(
p++;
continue;
case '\0':
- if (length == -1) {
- return p - dst;
+ if (length == (size_t)-1) {
+ return (size_t)(p - dst);
}
/*
@@ -1516,7 +1517,7 @@ TclConvertElement(
*p = *src;
p++;
}
- return p - dst;
+ return (size_t)(p - dst);
}
/*
@@ -1546,7 +1547,8 @@ Tcl_Merge(
{
#define LOCAL_SIZE 64
char localFlags[LOCAL_SIZE], *flagPtr = NULL;
- int i, bytesNeeded = 0;
+ int i;
+ size_t bytesNeeded = 0;
char *result, *dst;
/*
@@ -1555,7 +1557,7 @@ Tcl_Merge(
*/
if (argc == 0) {
- result = ckalloc(1);
+ result = Tcl_Alloc(1);
result[0] = '\0';
return result;
}
@@ -1567,17 +1569,11 @@ Tcl_Merge(
if (argc <= LOCAL_SIZE) {
flagPtr = localFlags;
} else {
- flagPtr = ckalloc(argc);
+ flagPtr = Tcl_Alloc(argc);
}
for (i = 0; i < argc; i++) {
flagPtr[i] = ( i ? TCL_DONT_QUOTE_HASH : 0 );
bytesNeeded += TclScanElement(argv[i], -1, &flagPtr[i]);
- if (bytesNeeded < 0) {
- Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX);
- }
- }
- if (bytesNeeded > INT_MAX - argc + 1) {
- Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX);
}
bytesNeeded += argc;
@@ -1585,7 +1581,7 @@ Tcl_Merge(
* Pass two: copy into the result area.
*/
- result = ckalloc(bytesNeeded);
+ result = Tcl_Alloc(bytesNeeded);
dst = result;
for (i = 0; i < argc; i++) {
flagPtr[i] |= ( i ? TCL_DONT_QUOTE_HASH : 0 );
@@ -1596,7 +1592,7 @@ Tcl_Merge(
dst[-1] = 0;
if (flagPtr != localFlags) {
- ckfree(flagPtr);
+ Tcl_Free(flagPtr);
}
return result;
}
@@ -1704,14 +1700,14 @@ TrimRight(
return numBytes - (p - bytes);
}
-int
+size_t
TclTrimRight(
const char *bytes, /* String to be trimmed... */
- int numBytes, /* ...and its length in bytes */
+ size_t numBytes, /* ...and its length in bytes */
const char *trim, /* String of trim characters... */
- int numTrim) /* ...and its length in bytes */
+ size_t numTrim) /* ...and its length in bytes */
{
- int res;
+ size_t res;
Tcl_DString bytesBuf, trimBuf;
/* Empty strings -> nothing to do */
@@ -1753,12 +1749,12 @@ TclTrimRight(
*----------------------------------------------------------------------
*/
-static inline int
+static inline size_t
TrimLeft(
const char *bytes, /* String to be trimmed... */
- int numBytes, /* ...and its length in bytes */
+ size_t numBytes, /* ...and its length in bytes */
const char *trim, /* String of trim characters... */
- int numTrim) /* ...and its length in bytes */
+ size_t numTrim) /* ...and its length in bytes */
{
const char *p = bytes;
Tcl_UniChar ch1 = 0, ch2 = 0;
@@ -1802,14 +1798,14 @@ TrimLeft(
return p - bytes;
}
-int
+size_t
TclTrimLeft(
const char *bytes, /* String to be trimmed... */
- int numBytes, /* ...and its length in bytes */
+ size_t numBytes, /* ...and its length in bytes */
const char *trim, /* String of trim characters... */
- int numTrim) /* ...and its length in bytes */
+ size_t numTrim) /* ...and its length in bytes */
{
- int res;
+ size_t res;
Tcl_DString bytesBuf, trimBuf;
/* Empty strings -> nothing to do */
@@ -1849,15 +1845,15 @@ TclTrimLeft(
*----------------------------------------------------------------------
*/
-int
+size_t
TclTrim(
const char *bytes, /* String to be trimmed... */
- int numBytes, /* ...and its length in bytes */
+ size_t numBytes, /* ...and its length in bytes */
const char *trim, /* String of trim characters... */
- int numTrim, /* ...and its length in bytes */
- int *trimRight) /* Offset from the end of the string. */
+ size_t numTrim, /* ...and its length in bytes */
+ size_t *trimRight) /* Offset from the end of the string. */
{
- int trimLeft;
+ size_t trimLeft;
Tcl_DString bytesBuf, trimBuf;
*trimRight = 0;
@@ -1911,14 +1907,15 @@ TclTrim(
*/
/* The whitespace characters trimmed during [concat] operations */
-#define CONCAT_WS_SIZE (int) (sizeof(CONCAT_TRIM_SET "") - 1)
+#define CONCAT_WS_SIZE (sizeof(CONCAT_TRIM_SET "") - 1)
char *
Tcl_Concat(
int argc, /* Number of strings to concatenate. */
const char *const *argv) /* Array of strings to concatenate. */
{
- int i, needSpace = 0, bytesNeeded = 0;
+ int i;
+ size_t needSpace = 0, bytesNeeded = 0;
char *result, *p;
/*
@@ -1926,7 +1923,7 @@ Tcl_Concat(
*/
if (argc == 0) {
- result = (char *) ckalloc(1);
+ result = (char *) Tcl_Alloc(1);
result[0] = '\0';
return result;
}
@@ -1937,27 +1934,16 @@ Tcl_Concat(
for (i = 0; i < argc; i++) {
bytesNeeded += strlen(argv[i]);
- if (bytesNeeded < 0) {
- Tcl_Panic("Tcl_Concat: max size of Tcl value exceeded");
- }
- }
- if (bytesNeeded + argc - 1 < 0) {
- /*
- * Panic test could be tighter, but not going to bother for this
- * legacy routine.
- */
-
- Tcl_Panic("Tcl_Concat: max size of Tcl value exceeded");
}
/*
* All element bytes + (argc - 1) spaces + 1 terminating NULL.
*/
- result = ckalloc((unsigned) (bytesNeeded + argc));
+ result = Tcl_Alloc(bytesNeeded + argc);
for (p = result, i = 0; i < argc; i++) {
- int triml, trimr, elemLength;
+ size_t triml, trimr, elemLength;
const char *element;
element = argv[i];
@@ -2018,7 +2004,8 @@ Tcl_ConcatObj(
int objc, /* Number of objects to concatenate. */
Tcl_Obj *const objv[]) /* Array of objects to concatenate. */
{
- int i, elemLength, needSpace = 0, bytesNeeded = 0;
+ int i, needSpace = 0;
+ size_t bytesNeeded = 0, elemLength;
const char *element;
Tcl_Obj *objPtr, *resPtr;
@@ -2029,13 +2016,14 @@ Tcl_ConcatObj(
*/
for (i = 0; i < objc; i++) {
- int length;
+ size_t length;
objPtr = objv[i];
if (TclListObjIsCanonical(objPtr)) {
continue;
}
- TclGetStringFromObj(objPtr, &length);
+ TclGetString(objPtr);
+ length = objPtr->length;
if (length > 0) {
break;
}
@@ -2072,11 +2060,9 @@ Tcl_ConcatObj(
*/
for (i = 0; i < objc; i++) {
- element = TclGetStringFromObj(objv[i], &elemLength);
+ element = TclGetString(objv[i]);
+ elemLength = objv[i]->length;
bytesNeeded += elemLength;
- if (bytesNeeded < 0) {
- break;
- }
}
/*
@@ -2090,9 +2076,10 @@ Tcl_ConcatObj(
Tcl_SetObjLength(resPtr, 0);
for (i = 0; i < objc; i++) {
- int triml, trimr;
+ size_t triml, trimr;
- element = TclGetStringFromObj(objv[i], &elemLength);
+ element = TclGetString(objv[i]);
+ elemLength = objv[i]->length;
/* Trim away the leading/trailing whitespace. */
triml = TclTrim(element, elemLength, CONCAT_TRIM_SET,
@@ -2406,11 +2393,11 @@ Tcl_StringCaseMatch(
int
TclByteArrayMatch(
const unsigned char *string,/* String. */
- int strLen, /* Length of String */
+ size_t strLen, /* Length of String */
const unsigned char *pattern,
/* Pattern, which may contain special
* characters. */
- int ptnLen, /* Length of Pattern */
+ size_t ptnLen, /* Length of Pattern */
int flags)
{
const unsigned char *stringEnd, *patternEnd;
@@ -2587,7 +2574,8 @@ TclStringMatchObj(
int flags) /* Only TCL_MATCH_NOCASE should be passed, or
* 0. */
{
- int match, length, plen;
+ int match;
+ size_t length, plen;
/*
* Promote based on the type of incoming object.
@@ -2599,15 +2587,15 @@ TclStringMatchObj(
if ((strObj->typePtr == &tclStringType) || (strObj->typePtr == NULL)) {
Tcl_UniChar *udata, *uptn;
- udata = Tcl_GetUnicodeFromObj(strObj, &length);
- uptn = Tcl_GetUnicodeFromObj(ptnObj, &plen);
+ udata = TclGetUnicodeFromObj(strObj, &length);
+ uptn = TclGetUnicodeFromObj(ptnObj, &plen);
match = TclUniCharMatch(udata, length, uptn, plen, flags);
} else if (TclIsPureByteArray(strObj) && TclIsPureByteArray(ptnObj)
&& !flags) {
unsigned char *data, *ptn;
- data = Tcl_GetByteArrayFromObj(strObj, &length);
- ptn = Tcl_GetByteArrayFromObj(ptnObj, &plen);
+ data = TclGetByteArrayFromObj(strObj, &length);
+ ptn = TclGetByteArrayFromObj(ptnObj, &plen);
match = TclByteArrayMatch(data, length, ptn, plen, 0);
} else {
match = Tcl_StringCaseMatch(TclGetString(strObj),
@@ -2667,13 +2655,13 @@ Tcl_DStringAppend(
Tcl_DString *dsPtr, /* Structure describing dynamic string. */
const char *bytes, /* String to append. If length is -1 then this
* must be null-terminated. */
- int length) /* Number of bytes from "bytes" to append. If
- * < 0, then append all of bytes, up to null
+ size_t length) /* Number of bytes from "bytes" to append. If
+ * -1, then append all of bytes, up to null
* at end. */
{
- int newSize;
+ size_t newSize;
- if (length < 0) {
+ if (length == TCL_AUTO_LENGTH) {
length = strlen(bytes);
}
newSize = length + dsPtr->length;
@@ -2687,12 +2675,12 @@ Tcl_DStringAppend(
if (newSize >= dsPtr->spaceAvl) {
dsPtr->spaceAvl = newSize * 2;
if (dsPtr->string == dsPtr->staticSpace) {
- char *newString = ckalloc(dsPtr->spaceAvl);
+ char *newString = Tcl_Alloc(dsPtr->spaceAvl);
- memcpy(newString, dsPtr->string, (size_t) dsPtr->length);
+ memcpy(newString, dsPtr->string, dsPtr->length);
dsPtr->string = newString;
} else {
- int offset = -1;
+ size_t offset = TCL_AUTO_LENGTH;
/* See [16896d49fd] */
if (bytes >= dsPtr->string
@@ -2700,9 +2688,9 @@ Tcl_DStringAppend(
offset = bytes - dsPtr->string;
}
- dsPtr->string = ckrealloc(dsPtr->string, dsPtr->spaceAvl);
+ dsPtr->string = Tcl_Realloc(dsPtr->string, dsPtr->spaceAvl);
- if (offset >= 0) {
+ if (offset != TCL_AUTO_LENGTH) {
bytes = dsPtr->string + offset;
}
}
@@ -2734,10 +2722,9 @@ TclDStringAppendObj(
Tcl_DString *dsPtr,
Tcl_Obj *objPtr)
{
- int length;
- char *bytes = TclGetStringFromObj(objPtr, &length);
+ char *bytes = TclGetString(objPtr);
- return Tcl_DStringAppend(dsPtr, bytes, length);
+ return Tcl_DStringAppend(dsPtr, bytes, objPtr->length);
}
char *
@@ -2776,7 +2763,7 @@ Tcl_DStringAppendElement(
char *dst = dsPtr->string + dsPtr->length;
int needSpace = TclNeedSpace(dsPtr->string, dst);
char flags = needSpace ? TCL_DONT_QUOTE_HASH : 0;
- int newSize = dsPtr->length + needSpace
+ size_t newSize = dsPtr->length + needSpace
+ TclScanElement(element, -1, &flags);
/*
@@ -2790,9 +2777,9 @@ Tcl_DStringAppendElement(
if (newSize >= dsPtr->spaceAvl) {
dsPtr->spaceAvl = newSize * 2;
if (dsPtr->string == dsPtr->staticSpace) {
- char *newString = ckalloc(dsPtr->spaceAvl);
+ char *newString = Tcl_Alloc(dsPtr->spaceAvl);
- memcpy(newString, dsPtr->string, (size_t) dsPtr->length);
+ memcpy(newString, dsPtr->string, dsPtr->length);
dsPtr->string = newString;
} else {
int offset = -1;
@@ -2803,7 +2790,7 @@ Tcl_DStringAppendElement(
offset = element - dsPtr->string;
}
- dsPtr->string = ckrealloc(dsPtr->string, dsPtr->spaceAvl);
+ dsPtr->string = Tcl_Realloc(dsPtr->string, dsPtr->spaceAvl);
if (offset >= 0) {
element = dsPtr->string + offset;
@@ -2848,8 +2835,7 @@ Tcl_DStringAppendElement(
*
* Side effects:
* The length of dsPtr is changed to length and a null byte is stored at
- * that position in the string. If length is larger than the space
- * allocated for dsPtr, then a panic occurs.
+ * that position in the string.
*
*----------------------------------------------------------------------
*/
@@ -2857,13 +2843,10 @@ Tcl_DStringAppendElement(
void
Tcl_DStringSetLength(
Tcl_DString *dsPtr, /* Structure describing dynamic string. */
- int length) /* New length for dynamic string. */
+ size_t length) /* New length for dynamic string. */
{
- int newsize;
+ size_t newsize;
- if (length < 0) {
- length = 0;
- }
if (length >= dsPtr->spaceAvl) {
/*
* There are two interesting cases here. In the first case, the user
@@ -2884,12 +2867,12 @@ Tcl_DStringSetLength(
dsPtr->spaceAvl = length + 1;
}
if (dsPtr->string == dsPtr->staticSpace) {
- char *newString = ckalloc(dsPtr->spaceAvl);
+ char *newString = Tcl_Alloc(dsPtr->spaceAvl);
- memcpy(newString, dsPtr->string, (size_t) dsPtr->length);
+ memcpy(newString, dsPtr->string, dsPtr->length);
dsPtr->string = newString;
} else {
- dsPtr->string = ckrealloc(dsPtr->string, dsPtr->spaceAvl);
+ dsPtr->string = Tcl_Realloc(dsPtr->string, dsPtr->spaceAvl);
}
}
dsPtr->length = length;
@@ -2919,7 +2902,7 @@ Tcl_DStringFree(
Tcl_DString *dsPtr) /* Structure describing dynamic string. */
{
if (dsPtr->string != dsPtr->staticSpace) {
- ckfree(dsPtr->string);
+ Tcl_Free(dsPtr->string);
}
dsPtr->string = dsPtr->staticSpace;
dsPtr->length = 0;
@@ -2981,11 +2964,11 @@ Tcl_DStringGetResult(
Tcl_DString *dsPtr) /* Dynamic string that is to become the result
* of interp. */
{
- int length;
- char *bytes = TclGetStringFromObj(Tcl_GetObjResult(interp), &length);
+ Tcl_Obj *obj = Tcl_GetObjResult(interp);
+ char *bytes = TclGetString(obj);
Tcl_DStringFree(dsPtr);
- Tcl_DStringAppend(dsPtr, bytes, length);
+ Tcl_DStringAppend(dsPtr, bytes, obj->length);
Tcl_ResetResult(interp);
}
@@ -3218,7 +3201,7 @@ Tcl_PrintDouble(
}
*dst++ = '\0';
}
- ckfree(digits);
+ Tcl_Free(digits);
}
/*
@@ -3333,15 +3316,14 @@ TclNeedSpace(
*----------------------------------------------------------------------
*/
-int
+size_t
TclFormatInt(
char *buffer, /* Points to the storage into which the
* formatted characters are written. */
Tcl_WideInt n) /* The integer to format. */
{
- Tcl_WideInt intVal;
- int i;
- int numFormatted, j;
+ Tcl_WideInt intVal;
+ size_t i, numFormatted, j;
const char *digits = "0123456789";
/*
@@ -3442,6 +3424,7 @@ TclGetIntForIndex(
const char *bytes;
if (TclGetIntFromObj(NULL, objPtr, indexPtr) == TCL_OK) {
+ if (*indexPtr < -1) *indexPtr = -1;
return TCL_OK;
}
@@ -3486,6 +3469,7 @@ TclGetIntForIndex(
} else {
*indexPtr = first - second;
}
+ if (*indexPtr < -1) *indexPtr = -1;
return TCL_OK;
}
@@ -3539,6 +3523,7 @@ GetEndOffsetFromObj(
/* TODO: Handle overflow cases sensibly */
*indexPtr = endValue + (int)objPtr->internalRep.wideValue;
+ if ((*indexPtr < -1) && (endValue > 0)) *indexPtr = -1;
return TCL_OK;
}
@@ -3568,7 +3553,7 @@ SetEndOffsetFromAny(
{
Tcl_WideInt offset; /* Offset in the "end-offset" expression */
register const char *bytes; /* String rep of the object */
- int length; /* Length of the object's string rep */
+ size_t length; /* Length of the object's string rep */
/*
* If it's already the right type, we're fine.
@@ -3584,7 +3569,7 @@ SetEndOffsetFromAny(
bytes = TclGetStringFromObj(objPtr, &length);
if ((*bytes != 'e') || (strncmp(bytes, "end",
- (size_t)((length > 3) ? 3 : length)) != 0)) {
+ ((length > 3) ? 3 : length)) != 0)) {
if (interp != NULL) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"bad index \"%s\": must be end?[+-]integer?", bytes));
@@ -3836,7 +3821,7 @@ GetThreadHash(
Tcl_GetThreadData(keyPtr, sizeof(Tcl_HashTable *));
if (NULL == *tablePtrPtr) {
- *tablePtrPtr = ckalloc(sizeof(Tcl_HashTable));
+ *tablePtrPtr = Tcl_Alloc(sizeof(Tcl_HashTable));
Tcl_CreateThreadExitHandler(FreeThreadHash, *tablePtrPtr);
Tcl_InitHashTable(*tablePtrPtr, TCL_ONE_WORD_KEYS);
}
@@ -3865,7 +3850,7 @@ FreeThreadHash(
ClearHash(tablePtr);
Tcl_DeleteHashTable(tablePtr);
- ckfree(tablePtr);
+ Tcl_Free(tablePtr);
}
/*
@@ -3887,7 +3872,7 @@ FreeProcessGlobalValue(
pgvPtr->epoch++;
pgvPtr->numBytes = 0;
- ckfree(pgvPtr->value);
+ Tcl_Free(pgvPtr->value);
pgvPtr->value = NULL;
if (pgvPtr->encoding) {
Tcl_FreeEncoding(pgvPtr->encoding);
@@ -3926,13 +3911,13 @@ TclSetProcessGlobalValue(
pgvPtr->epoch++;
if (NULL != pgvPtr->value) {
- ckfree(pgvPtr->value);
+ Tcl_Free(pgvPtr->value);
} else {
Tcl_CreateExitHandler(FreeProcessGlobalValue, pgvPtr);
}
bytes = TclGetString(newValue);
pgvPtr->numBytes = newValue->length;
- pgvPtr->value = ckalloc(pgvPtr->numBytes + 1);
+ pgvPtr->value = Tcl_Alloc(pgvPtr->numBytes + 1);
memcpy(pgvPtr->value, bytes, pgvPtr->numBytes + 1);
if (pgvPtr->encoding) {
Tcl_FreeEncoding(pgvPtr->encoding);
@@ -3995,8 +3980,8 @@ TclGetProcessGlobalValue(
Tcl_ExternalToUtfDString(current, Tcl_DStringValue(&native),
Tcl_DStringLength(&native), &newValue);
Tcl_DStringFree(&native);
- ckfree(pgvPtr->value);
- pgvPtr->value = ckalloc(Tcl_DStringLength(&newValue) + 1);
+ Tcl_Free(pgvPtr->value);
+ pgvPtr->value = Tcl_Alloc(Tcl_DStringLength(&newValue) + 1);
memcpy(pgvPtr->value, Tcl_DStringValue(&newValue),
Tcl_DStringLength(&newValue) + 1);
Tcl_DStringFree(&newValue);