diff options
Diffstat (limited to 'generic/tclCmdIL.c')
-rw-r--r-- | generic/tclCmdIL.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/generic/tclCmdIL.c b/generic/tclCmdIL.c index a2858fa..44bec4b 100644 --- a/generic/tclCmdIL.c +++ b/generic/tclCmdIL.c @@ -4032,9 +4032,11 @@ Tcl_LsortObjCmd( int sortMode = SORTMODE_ASCII; int group, groupSize, groupOffset, idx, allocatedIndexVector = 0; Tcl_Obj *resultPtr, *cmdPtr, **listObjPtrs, *listObj, *indexPtr; + size_t elmArrSize; SortElement *elementArray = NULL, *elementPtr; SortInfo sortInfo; /* Information about this sort that needs to * be passed to the comparison function. */ +# define MAXCALLOC 1024000 # define NUM_LISTS 30 SortElement *subList[NUM_LISTS+1]; /* This array holds pointers to temporary @@ -4358,7 +4360,19 @@ Tcl_LsortObjCmd( * begins sorting it into the sublists as it appears. */ - elementArray = (SortElement *)ckalloc(length * sizeof(SortElement)); + elmArrSize = length * sizeof(SortElement); + if (elmArrSize <= MAXCALLOC) { + elementArray = (SortElement *)ckalloc(elmArrSize); + } else { + elementArray = (SortElement *)malloc(elmArrSize); + } + if (!elementArray) { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "no enough memory to proccess sort of %d items", length)); + Tcl_SetErrorCode(interp, "TCL", "MEMORY", NULL); + sortInfo.resultCode = TCL_ERROR; + goto done; + } for (i=0; i < length; i++) { idx = groupSize * i + groupOffset; @@ -4491,7 +4505,11 @@ Tcl_LsortObjCmd( TclStackFree(interp, sortInfo.indexv); } if (elementArray) { - ckfree(elementArray); + if (elmArrSize <= MAXCALLOC) { + ckfree((char *)elementArray); + } else { + free((char *)elementArray); + } } return sortInfo.resultCode; } |