Skip to content

Commit

Permalink
Avoid possible performance issue
Browse files Browse the repository at this point in the history
Only check total size if in error condition.
  • Loading branch information
Joshua Davies committed Aug 5, 2018
1 parent 17d54e7 commit 39ef78f
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions sources/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ LONG EndSort(PHEAD WORD *buffer, int par)
{
GETBIDENTITY
SORTING *S = AT.SS;
WORD j, **ss, **tmpss, *to, *t;
WORD j, **ss, *to, *t;
LONG sSpace, over, tover, spare, retval = 0, jj;
POSITION position, pp;
off_t lSpace;
Expand Down Expand Up @@ -775,24 +775,21 @@ LONG EndSort(PHEAD WORD *buffer, int par)
retval = sSpace + 1;
}
else {
/* First check if the sorted argument fits in AM.MaxTer. */
sSpace = 0;
tmpss = ss;
while ( (t = *tmpss++) != 0 ) {
sSpace += *t;
}
if ( sSpace > AM.MaxTer/((LONG)sizeof(WORD)) ) {
MLOCK(ErrorMessageLock);
MesPrint("Sorted function argument too long: %d words", sSpace);
MesPrint("MaxTermSize: %d words", AM.MaxTer/(LONG)sizeof(WORD));
MUNLOCK(ErrorMessageLock);
retval = -1; goto RetRetval;
}
/* Now copy, it fits */
to = buffer;
sSpace = 0;
while ( ( t = *ss++ ) != 0 ) {
j = *t;
if ( ( sSpace += j ) > AM.MaxTer/((LONG)sizeof(WORD)) ) {
/* Too big! Get the total size for useful error message */
while ( ( t = *ss++ ) != 0 ) {
sSpace += *t;
}
MLOCK(ErrorMessageLock);
MesPrint("Sorted function argument too long: %d words", sSpace);
MesPrint("MaxTermSize: %d words", AM.MaxTer/((LONG)sizeof(WORD)) );
MUNLOCK(ErrorMessageLock);
retval = -1; goto RetRetval;
}
while ( --j >= 0 ) *to++ = *t++;
}
*to = 0;
Expand Down

0 comments on commit 39ef78f

Please sign in to comment.