Skip to content

Commit 50a1d88

Browse files
committed
Split the large sort allocation into separate allocations.
Buffer overruns in these allocations will be visible to valgrind.
1 parent 83e3d41 commit 50a1d88

File tree

3 files changed

+108
-8
lines changed

3 files changed

+108
-8
lines changed

sources/form3.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ template<typename T> struct calc {
438438
*/
439439
#define WITHSORTBOTS
440440

441+
#define SPLITALLOC
442+
441443
#include <stdio.h>
442444
#include <stdlib.h>
443445
#include <string.h>

sources/setfile.c

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -848,20 +848,22 @@ VOID WriteSetup(VOID)
848848
To be used for the main allocation of the sort buffers, and
849849
in a later stage for the function and subroutine sort buffers.
850850
*/
851-
852851
SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsInSmall,
853852
int MaxPatches, int MaxFpatches, LONG IOsize)
854853
{
855-
LONG allocation,longer,terms2insmall,sortsize,longerp;
854+
#ifndef SPLITALLOC
855+
LONG allocation;
856+
#endif
857+
LONG longer,terms2insmall,sortsize,longerp;
856858
LONG IObuffersize = IOsize;
857859
LONG IOtry;
858860
SORTING *sort;
859-
int i = 0, j = 0;
861+
int fname2Size = 0, j = 0;
860862
char *s;
861863
if ( AM.S0 != 0 ) {
862-
s = FG.fname2; i = 0;
863-
while ( *s ) { s++; i++; }
864-
i += 16;
864+
s = FG.fname2; fname2Size = 0;
865+
while ( *s ) { s++; fname2Size++; }
866+
fname2Size += 16;
865867
}
866868
if ( MaxFpatches < 4 ) MaxFpatches = 4;
867869
longer = MaxPatches > MaxFpatches ? MaxPatches : MaxFpatches;
@@ -897,10 +899,10 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn
897899
}
898900

899901
IOtry = ((LargeSize+SmallEsize)/MaxFpatches-2*AM.MaxTer)/sizeof(WORD)-COMPINC;
900-
901902
if ( (LONG)(IObuffersize*sizeof(WORD)) < IOtry )
902903
IObuffersize = (IOtry+sizeof(WORD)-1)/sizeof(WORD);
903904

905+
#ifndef SPLITALLOC
904906
allocation =
905907
3*sizeof(POSITION)*(LONG)longer /* Filepositions!! */
906908
+2*sizeof(WORD *)*longer
@@ -914,7 +916,7 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn
914916
+LargeSize
915917
+SmallEsize
916918
+sortsize
917-
+IObuffersize*sizeof(WORD) + i + 16;
919+
+IObuffersize*sizeof(WORD) + fname2Size + 16;
918920
sort = (SORTING *)Malloc1(allocation,"sort buffers");
919921

920922
sort->LargeSize = LargeSize/sizeof(WORD);
@@ -931,11 +933,13 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn
931933
sort->pStop = sort->Patches+longer;
932934
sort->poina = sort->pStop+longer;
933935
sort->poin2a = sort->poina + longerp;
936+
934937
sort->fPatches = (POSITION *)(sort->poin2a+longerp);
935938
sort->fPatchesStop = sort->fPatches + longer;
936939
sort->inPatches = sort->fPatchesStop + longer;
937940
sort->tree = (WORD *)(sort->inPatches + longer);
938941
sort->used = sort->tree+longerp;
942+
939943
#ifdef WITHZLIB
940944
sort->fpcompressed = sort->used+longerp;
941945
sort->fpincompressed = sort->fpcompressed+longerp+2;
@@ -944,13 +948,16 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn
944948
#else
945949
sort->ktoi = sort->used + longerp;
946950
#endif
951+
947952
sort->lBuffer = (WORD *)(sort->ktoi + longerp + 2);
948953
sort->lTop = sort->lBuffer+sort->LargeSize;
954+
949955
sort->sBuffer = sort->lTop;
950956
if ( sort->LargeSize == 0 ) { sort->lBuffer = 0; sort->lTop = 0; }
951957
sort->sTop = sort->sBuffer + sort->SmallSize;
952958
sort->sTop2 = sort->sBuffer + sort->SmallEsize;
953959
sort->sHalf = sort->sBuffer + (LONG)((sort->SmallSize+sort->SmallEsize)>>1);
960+
954961
sort->file.PObuffer = (WORD *)(sort->sTop2);
955962
sort->file.POstop = sort->file.PObuffer+IObuffersize;
956963
sort->file.POsize = IObuffersize * sizeof(WORD);
@@ -975,6 +982,77 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn
975982
sort->cBufferSize = 0;
976983
sort->f = 0;
977984
sort->PolyWise = 0;
985+
#endif
986+
987+
988+
#ifdef SPLITALLOC
989+
// Separate buffers.
990+
sort = Malloc1(sizeof(*sort), "SPLITALLOC sorting struct");
991+
992+
sort->LargeSize = LargeSize/sizeof(WORD);
993+
sort->SmallSize = SmallSize/sizeof(WORD);
994+
sort->SmallEsize = SmallEsize/sizeof(WORD);
995+
sort->MaxPatches = MaxPatches;
996+
sort->MaxFpatches = MaxFpatches;
997+
sort->TermsInSmall = TermsInSmall;
998+
sort->Terms2InSmall = terms2insmall;
999+
1000+
sort->sPointer = Malloc1(sizeof(*(sort->sPointer ))*terms2insmall, "SPLITALLOC sPointer");
1001+
sort->SplitScratch = Malloc1(sizeof(*(sort->SplitScratch))*terms2insmall/2, "SPLITALLOC SplitScratch");
1002+
sort->Patches = Malloc1(sizeof(*(sort->Patches ))*longer, "SPLITALLOC Patches");
1003+
sort->pStop = Malloc1(sizeof(*(sort->pStop ))*longer, "SPLITALLOC pStop");
1004+
sort->poina = Malloc1(sizeof(*(sort->poina ))*longerp, "SPLITALLOC poina");
1005+
sort->poin2a = Malloc1(sizeof(*(sort->poin2a ))*longerp, "SPLITALLOC poin2a");
1006+
1007+
sort->fPatches = Malloc1(sizeof(*(sort->fPatches ))*longer, "SPLITALLOC fPatches");
1008+
sort->fPatchesStop = Malloc1(sizeof(*(sort->fPatchesStop))*longer, "SPLITALLOC fPatchesStop");
1009+
sort->inPatches = Malloc1(sizeof(*(sort->inPatches ))*longer, "SPLITALLOC inPatches");
1010+
sort->tree = Malloc1(sizeof(*(sort->tree ))*longerp, "SPLITALLOC tree");
1011+
sort->used = Malloc1(sizeof(*(sort->used ))*longerp, "SPLITALLOC used");
1012+
1013+
#ifdef WITHZLIB
1014+
sort->fpcompressed = Malloc1(sizeof(*(sort->fpcompressed ))*(longerp+2), "SPLITALLOC fpcompressed");
1015+
sort->fpincompressed = Malloc1(sizeof(*(sort->fpincompressed))*(longerp+2), "SPLITALLOC fpincompressed");
1016+
sort->zsparray = 0;
1017+
#endif
1018+
1019+
sort->ktoi = Malloc1(sizeof(WORD)*(longerp+2), "SPLITALLOC ktoi");
1020+
1021+
// The combined Large buffer and Small buffer (+ extension) are used.
1022+
// They must be allocated together.
1023+
sort->lBuffer = Malloc1(LargeSize+SmallEsize, "SPLITALLOC lBuffer+sBuffer");
1024+
sort->lTop = sort->lBuffer+sort->LargeSize;
1025+
1026+
sort->sBuffer = sort->lTop;
1027+
if ( sort->LargeSize == 0 ) { sort->lBuffer = 0; sort->lTop = 0; }
1028+
sort->sTop = sort->sBuffer + sort->SmallSize;
1029+
sort->sTop2 = sort->sBuffer + sort->SmallEsize;
1030+
sort->sHalf = sort->sBuffer + (LONG)((sort->SmallSize+sort->SmallEsize)>>1);
1031+
1032+
sort->file.PObuffer = Malloc1(IObuffersize*sizeof(*(sort->file.PObuffer))+fname2Size+16, "SPLITALLOC PObuffer");
1033+
sort->file.POstop = sort->file.PObuffer+IObuffersize;
1034+
sort->file.POsize = IObuffersize * sizeof(WORD);
1035+
sort->file.POfill = sort->file.POfull = sort->file.PObuffer;
1036+
sort->file.active = 0;
1037+
sort->file.handle = -1;
1038+
PUTZERO(sort->file.POposition);
1039+
#ifdef WITHPTHREADS
1040+
sort->file.pthreadslock = dummylock;
1041+
#endif
1042+
#ifdef WITHZLIB
1043+
sort->file.ziosize = IObuffersize*sizeof(WORD);
1044+
sort->file.ziobuffer = 0;
1045+
#endif
1046+
if ( AM.S0 != 0 ) {
1047+
sort->file.name = (char *)(sort->file.PObuffer + IObuffersize);
1048+
AllocSortFileName(sort);
1049+
}
1050+
else sort->file.name = 0;
1051+
sort->cBuffer = 0;
1052+
sort->cBufferSize = 0;
1053+
sort->f = 0;
1054+
sort->PolyWise = 0;
1055+
#endif
9781056

9791057
return(sort);
9801058
}

sources/sort.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4757,7 +4757,27 @@ void CleanUpSort(int num)
47574757
MUNLOCK(ErrorMessageLock);
47584758
#endif
47594759
}
4760+
#ifdef SPLITALLOC
4761+
M_free(S->sPointer, "SPLITALLOC sPointer");
4762+
M_free(S->SplitScratch, "SPLITALLOC SplitScratch");
4763+
M_free(S->Patches, "SPLITALLOC Patches");
4764+
M_free(S->pStop, "SPLITALLOC pStop");
4765+
M_free(S->poina, "SPLITALLOC poina");
4766+
M_free(S->poin2a, "SPLITALLOC poin2a");
4767+
M_free(S->fPatches, "SPLITALLOC fPatches");
4768+
M_free(S->fPatchesStop, "SPLITALLOC fPatchesStop");
4769+
M_free(S->inPatches, "SPLITALLOC inPatches");
4770+
M_free(S->tree, "SPLITALLOC tree");
4771+
M_free(S->used, "SPLITALLOC used");
4772+
M_free(S->fpcompressed, "SPLITALLOC fpcompressed");
4773+
M_free(S->fpincompressed, "SPLITALLOC fpincompressed");
4774+
M_free(S->ktoi, "SPLITALLOC ktoi");
4775+
M_free(S->lBuffer, "SPLITALLOC lBuffer+sBuffer");
4776+
M_free(S->file.PObuffer, "SPLITALLOC PObuffer");
4777+
M_free(S, "SPLITALLOC sorting struct");
4778+
#else
47604779
M_free(S,"sorting struct");
4780+
#endif
47614781
}
47624782
AN.FunSorts[i] = 0;
47634783
}

0 commit comments

Comments
 (0)