diff options
author | dgp <dgp@users.sourceforge.net> | 2016-03-24 19:21:47 (GMT) |
---|---|---|
committer | dgp <dgp@users.sourceforge.net> | 2016-03-24 19:21:47 (GMT) |
commit | de47657fd417be6eaa9fff7203cbb003fd3e0fa2 (patch) | |
tree | 9dc00ace939c8ba14d99c9c12277a53140e00e6a | |
parent | 92dc17a5efa5db9dc3247b3d8e2707140b043eff (diff) | |
download | tcl-de47657fd417be6eaa9fff7203cbb003fd3e0fa2.zip tcl-de47657fd417be6eaa9fff7203cbb003fd3e0fa2.tar.gz tcl-de47657fd417be6eaa9fff7203cbb003fd3e0fa2.tar.bz2 |
Revise Tcl_InitStringRep() to do non-panic attempt at allocation.
Let caller decide how catastrophic it is.
Revise [string repeat] to use new routine.
-rw-r--r-- | generic/tclCmdMZ.c | 21 | ||||
-rw-r--r-- | generic/tclObj.c | 7 |
2 files changed, 10 insertions, 18 deletions
diff --git a/generic/tclCmdMZ.c b/generic/tclCmdMZ.c index 13f9e7d..02d050a 100644 --- a/generic/tclCmdMZ.c +++ b/generic/tclCmdMZ.c @@ -2275,11 +2275,9 @@ StringReptCmd( } length2 = length1 * count; - /* - * Include space for the NUL. - */ - - string2 = attemptckalloc((unsigned) length2 + 1); + TclNewObj(resultPtr); + Tcl_InvalidateStringRep(resultPtr); + string2 = Tcl_InitStringRep(resultPtr, NULL, length2); if (string2 == NULL) { /* * Alloc failed. Note that in this case we try to do an error message @@ -2292,22 +2290,13 @@ StringReptCmd( "string size overflow, out of memory allocating %u bytes", length2 + 1)); Tcl_SetErrorCode(interp, "TCL", "MEMORY", NULL); + Tcl_DecrRefCount(resultPtr); return TCL_ERROR; } for (index = 0; index < count; index++) { memcpy(string2 + (length1 * index), string1, (size_t) length1); } - string2[length2] = '\0'; - - /* - * We have to directly assign this instead of using Tcl_SetStringObj (and - * indirectly TclInitStringRep) because that makes another copy of the - * data. - */ - - TclNewObj(resultPtr); - resultPtr->bytes = string2; - resultPtr->length = length2; + (void) Tcl_InitStringRep(resultPtr, NULL, length2); Tcl_SetObjResult(interp, resultPtr); done: diff --git a/generic/tclObj.c b/generic/tclObj.c index aa81588..f1f4f1d 100644 --- a/generic/tclObj.c +++ b/generic/tclObj.c @@ -1758,7 +1758,10 @@ Tcl_InitStringRep( /* Allocate only as empty - extend later if bytes copied */ objPtr->length = 0; if (numBytes) { - objPtr->bytes = (char *)ckalloc(numBytes+1); + objPtr->bytes = attemptckalloc(numBytes + 1); + if (objPtr->bytes == NULL) { + return NULL; + } if (bytes) { /* Copy */ memcpy(objPtr->bytes, bytes, numBytes); @@ -1770,7 +1773,7 @@ Tcl_InitStringRep( } } else { /* objPtr->bytes != NULL bytes == NULL - Truncate */ - objPtr->bytes = (char *)ckrealloc(objPtr->bytes, numBytes+1); + objPtr->bytes = ckrealloc(objPtr->bytes, numBytes + 1); objPtr->length = (int)numBytes; } |