From 413706af15c4f39a0cec781c067faa9eefafd5b8 Mon Sep 17 00:00:00 2001 From: dgp Date: Thu, 24 Mar 2016 20:15:01 +0000 Subject: Update Tcl_InitStringRep callers to handle OOM condition. --- generic/tclBinary.c | 4 +++- generic/tclDictObj.c | 3 +++ generic/tclEnsemble.c | 5 +++-- generic/tclInt.h | 7 +++++++ generic/tclObj.c | 12 ++++++++++-- generic/tclUtil.c | 2 ++ macosx/tclMacOSXFCmd.c | 7 +++++-- 7 files changed, 33 insertions(+), 7 deletions(-) diff --git a/generic/tclBinary.c b/generic/tclBinary.c index 7abb5c5..b7fea30 100644 --- a/generic/tclBinary.c +++ b/generic/tclBinary.c @@ -589,9 +589,11 @@ UpdateStringOfByteArray( } if (size == length) { - (void) Tcl_InitStringRep(objPtr, (char *)src, size); + char *dst = Tcl_InitStringRep(objPtr, (char *)src, size); + TclOOM(dst, size); } else { char *dst = Tcl_InitStringRep(objPtr, NULL, size); + TclOOM(dst, size); for (i = 0; i < length; i++) { dst += Tcl_UniCharToUtf(src[i], dst); } diff --git a/generic/tclDictObj.c b/generic/tclDictObj.c index 46fa623..ae7280a 100644 --- a/generic/tclDictObj.c +++ b/generic/tclDictObj.c @@ -560,6 +560,7 @@ UpdateStringOfDict( */ dst = Tcl_InitStringRep(dictPtr, NULL, bytesNeeded - 1); + TclOOM(dst, bytesNeeded) for (i=0,cPtr=dict->entryChainHead; inextPtr) { flagPtr[i] |= ( i ? TCL_DONT_QUOTE_HASH : 0 ); keyPtr = Tcl_GetHashKey(&dict->table, &cPtr->entry); @@ -677,6 +678,7 @@ SetDictFromAny( TclNewObj(keyPtr); Tcl_InvalidateStringRep(keyPtr); dst = Tcl_InitStringRep(keyPtr, NULL, elemSize); + TclOOM(dst, elemSize); /* Consider error */ (void)Tcl_InitStringRep(keyPtr, NULL, TclCopyAndCollapse(elemSize, elemStart, dst)); } @@ -696,6 +698,7 @@ SetDictFromAny( TclNewObj(valuePtr); Tcl_InvalidateStringRep(valuePtr); dst = Tcl_InitStringRep(valuePtr, NULL, elemSize); + TclOOM(dst, elemSize); /* Consider error */ (void)Tcl_InitStringRep(valuePtr, NULL, TclCopyAndCollapse(elemSize, elemStart, dst)); } diff --git a/generic/tclEnsemble.c b/generic/tclEnsemble.c index 2ef2861..e034d6e 100644 --- a/generic/tclEnsemble.c +++ b/generic/tclEnsemble.c @@ -2703,9 +2703,10 @@ StringOfEnsembleCmdRep( Tcl_Obj *objPtr) { EnsembleCmdRep *ensembleCmd = objPtr->internalRep.twoPtrValue.ptr1; + unsigned int size = strlen(ensembleCmd->fullSubcmdName); + char *dst = Tcl_InitStringRep(objPtr, ensembleCmd->fullSubcmdName, size); - Tcl_InitStringRep(objPtr, ensembleCmd->fullSubcmdName, - strlen(ensembleCmd->fullSubcmdName)); + TclOOM(dst, size); } /* diff --git a/generic/tclInt.h b/generic/tclInt.h index 462e1e0..50f7a76 100644 --- a/generic/tclInt.h +++ b/generic/tclInt.h @@ -2314,6 +2314,13 @@ typedef struct Interp { #define TCL_ALIGN(x) (((int)(x) + 7) & ~7) /* + * A common panic alert when memory allocation fails. + */ + +#define TclOOM(ptr, size) \ + ((size) && ((ptr)||(Tcl_Panic("unable to alloc %u bytes", (size)),1))) + +/* * The following enum values are used to specify the runtime platform setting * of the tclPlatform variable. */ diff --git a/generic/tclObj.c b/generic/tclObj.c index f1f4f1d..2a35539 100644 --- a/generic/tclObj.c +++ b/generic/tclObj.c @@ -2481,6 +2481,8 @@ UpdateStringOfDouble( { char *dst = Tcl_InitStringRep(objPtr, NULL, TCL_DOUBLE_SPACE); + TclOOM(dst, TCL_DOUBLE_SPACE + 1); + Tcl_PrintDouble(NULL, objPtr->internalRep.doubleValue, dst); (void) Tcl_InitStringRep(objPtr, NULL, strlen(dst)); } @@ -2672,9 +2674,11 @@ static void UpdateStringOfInt( register Tcl_Obj *objPtr) /* Int object whose string rep to update. */ { + char *dst = Tcl_InitStringRep( objPtr, NULL, TCL_INTEGER_SPACE); + + TclOOM(dst, TCL_INTEGER_SPACE + 1); (void) Tcl_InitStringRep(objPtr, NULL, - TclFormatInt(Tcl_InitStringRep( objPtr, NULL, TCL_INTEGER_SPACE), - objPtr->internalRep.longValue)); + TclFormatInt(dst, objPtr->internalRep.longValue)); } /* @@ -2965,6 +2969,8 @@ UpdateStringOfWideInt( { char *dst = Tcl_InitStringRep(objPtr, NULL, TCL_INTEGER_SPACE + 2); + TclOOM(dst, TCL_INTEGER_SPACE + 3); + /* * Note that sprintf will generate a compiler warning under Mingw claiming * %I64 is an unknown format specifier. Just ignore this warning. We can't @@ -3366,6 +3372,8 @@ UpdateStringOfBignum( } stringVal = Tcl_InitStringRep(objPtr, NULL, size - 1); + + TclOOM(stringVal, size); if (MP_OKAY != mp_toradix_n(&bignumVal, stringVal, 10, size)) { Tcl_Panic("conversion failure in UpdateStringOfBignum"); } diff --git a/generic/tclUtil.c b/generic/tclUtil.c index 1d5e8fe..01f8225 100644 --- a/generic/tclUtil.c +++ b/generic/tclUtil.c @@ -3656,6 +3656,8 @@ UpdateStringOfEndOffset( char *dst = Tcl_InitStringRep(objPtr, NULL, TCL_INTEGER_SPACE + 5); int len = 3; + TclOOM(dst, TCL_INTEGER_SPACE + 6); + memcpy(dst, "end", len); if (objPtr->internalRep.longValue != 0) { dst[len++] = '-'; diff --git a/macosx/tclMacOSXFCmd.c b/macosx/tclMacOSXFCmd.c index 13a939f..8659e95 100644 --- a/macosx/tclMacOSXFCmd.c +++ b/macosx/tclMacOSXFCmd.c @@ -695,9 +695,11 @@ UpdateStringOfOSType( const int size = TCL_UTF_MAX * 4; char *dst = Tcl_InitStringRep(objPtr, NULL, size); OSType osType = (OSType) objPtr->internalRep.longValue; - Tcl_Encoding encoding = Tcl_GetEncoding(NULL, "macRoman"); + int written = 0; + Tcl_Encoding encoding; char src[5]; - int written; + + TclOOM(dst, size); src[0] = (char) (osType >> 24); src[1] = (char) (osType >> 16); @@ -705,6 +707,7 @@ UpdateStringOfOSType( src[3] = (char) (osType); src[4] = '\0'; + encoding = Tcl_GetEncoding(NULL, "macRoman"); Tcl_ExternalToUtf(NULL, encoding, src, -1, /* flags */ 0, /* statePtr */ NULL, dst, size, /* srcReadPtr */ NULL, /* dstWrotePtr */ &written, /* dstCharsPtr */ NULL); -- cgit v0.12