From 39ef78f8b21f02f94cbda148be1a3e9770223c7b Mon Sep 17 00:00:00 2001 From: Joshua Davies Date: Sun, 5 Aug 2018 18:36:08 +0200 Subject: [PATCH] Avoid possible performance issue Only check total size if in error condition. --- sources/sort.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/sources/sort.c b/sources/sort.c index a095d1b2..8d7d3c96 100644 --- a/sources/sort.c +++ b/sources/sort.c @@ -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; @@ -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;