Skip to content

Commit 710d340

Browse files
committed
Put pointer-zeroing back into SplitMerge, so GarbHand works
Commit f1b83ae removed various zeroed pointers from SplitMerge, which does not produce wrong results, since they are "out of range" of the sorted and merged region of the pointer array, but it leads to GarbHand computing the total size of the terms incorrectly. This causes valgrind errors when we write over the end of the SmallExtension, and causes termination due to a "too small SmallExtension" which might not actually be the case.
1 parent 8765478 commit 710d340

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

check/fixes.frm

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,3 +2886,58 @@ EndTerm;
28862886
#pend_if mpi?
28872887
assert runtime_error?("Please increase SubSmallExtension setup parameter.")
28882888
*--#] Issue512_2 :
2889+
*--#[ Issue512_3 :
2890+
#-
2891+
2892+
* Sort which fits in SmallExtension, but needs GarbHand
2893+
2894+
#: SmallSize 1000K
2895+
#: SmallExtension 5090K
2896+
#: TermsInSmall 2M
2897+
#: SubSmallSize 1000K
2898+
#: SubSmallExtension 1200K
2899+
#: SubTermsInSmall 2M
2900+
2901+
Symbol x,n;
2902+
CFunction f,g,prf;
2903+
2904+
Local test = (<f(1)>+...+<f(150)>)*(<g(1)>+...+<g(100)>);
2905+
.sort
2906+
2907+
PolyRatFun prf;
2908+
Identify f(x?) = prf(n-x,n+x);
2909+
2910+
.end
2911+
# Fails due to polynomial size on 32bit builds
2912+
#require wordsize >= 4
2913+
assert succeeded?
2914+
*--#] Issue512_3 :
2915+
*--#[ Issue512_4 :
2916+
#-
2917+
2918+
* Sort which fits in SubSmallExtension, but needs GarbHand
2919+
2920+
#: SmallSize 1000K
2921+
#: SmallExtension 1200K
2922+
#: TermsInSmall 2M
2923+
#: SubSmallSize 1000K
2924+
#: SubSmallExtension 5090K
2925+
#: SubTermsInSmall 2M
2926+
2927+
Symbol x,n;
2928+
CFunction f,g,prf;
2929+
2930+
Local test = 1;
2931+
.sort
2932+
2933+
PolyRatFun prf;
2934+
Term;
2935+
Multiply (<f(1)>+...+<f(150)>)*(<g(1)>+...+<g(100)>);
2936+
Identify f(x?) = prf(n-x,n+x);
2937+
EndTerm;
2938+
2939+
.end
2940+
# Fails due to polynomial size on 32bit builds
2941+
#require wordsize >= 4
2942+
assert succeeded?
2943+
*--#] Issue512_4 :

sources/sort.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3315,7 +3315,7 @@ LONG SplitMerge(PHEAD WORD **Pointer, LONG number)
33153315
{
33163316
GETBIDENTITY
33173317
SORTING *S = AT.SS;
3318-
WORD **pp3, **pp1, **pp2;
3318+
WORD **pp3, **pp1, **pp2, **pptop;
33193319
LONG i, newleft, newright, split;
33203320

33213321
if ( number < 2 ) return(number);
@@ -3331,6 +3331,7 @@ LONG SplitMerge(PHEAD WORD **Pointer, LONG number)
33313331
}
33323332
return(number);
33333333
}
3334+
pptop = Pointer + number;
33343335
split = number/2;
33353336
newleft = SplitMerge(BHEAD Pointer,split);
33363337
newright = SplitMerge(BHEAD Pointer+split,number-split);
@@ -3354,12 +3355,13 @@ LONG SplitMerge(PHEAD WORD **Pointer, LONG number)
33543355
if ( AddCoef(BHEAD pp1,pp2) > 0 ) pp1++;
33553356
else newleft--;
33563357
}
3357-
pp2++; newright--;
3358+
*pp2++ = 0; newright--;
33583359
}
33593360
else pp1++;
33603361
newleft += newright;
33613362
if ( pp1 < pp2 ) {
33623363
while ( --newright >= 0 ) *pp1++ = *pp2++;
3364+
while ( pp1 < pptop ) *pp1++ = 0;
33633365
}
33643366
return(newleft);
33653367
}
@@ -3371,8 +3373,9 @@ LONG SplitMerge(PHEAD WORD **Pointer, LONG number)
33713373
if ( AN.SplitScratch ) M_free(AN.SplitScratch,"AN.SplitScratch");
33723374
AN.SplitScratch = (WORD **)Malloc1(AN.SplitScratchSize*sizeof(WORD *),"AN.SplitScratch");
33733375
}
3376+
33743377
pp3 = AN.SplitScratch; pp1 = Pointer;
3375-
for ( i = 0; i < newleft; i++ ) *pp3++ = *pp1++;
3378+
for ( i = 0; i < newleft; i++ ) { *pp3++ = *pp1++; }
33763379
AN.InScratch = newleft;
33773380
pp1 = AN.SplitScratch; pp2 = Pointer + split; pp3 = Pointer;
33783381
/*
@@ -3396,27 +3399,31 @@ LONG SplitMerge(PHEAD WORD **Pointer, LONG number)
33963399

33973400
while ( newleft > 0 && newright > 0 ) {
33983401
if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3399-
*pp3++ = *pp2++;
3402+
*pp3++ = *pp2;
3403+
*pp2++ = 0;
34003404
newright--;
34013405
}
34023406
else if ( i > 0 ) {
3403-
*pp3++ = *pp1++;
3407+
*pp3++ = *pp1;
3408+
*pp1++ = 0;
34043409
newleft--;
34053410
}
34063411
else {
34073412
if ( S->PolyWise ) { if ( AddPoly(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
34083413
else { if ( AddCoef(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3409-
pp1++; pp2++; newleft--; newright--;
3414+
*pp1++ = 0; *pp2++ = 0; newleft--; newright--;
34103415
}
34113416
}
3412-
for ( i = 0; i < newleft; i++ ) *pp3++ = *pp1++;
3417+
for ( i = 0; i < newleft; i++ ) { *pp3++ = *pp1; *pp1++ = 0; }
34133418
if ( pp3 == pp2 ) {
34143419
pp3 += newright;
34153420
} else {
3416-
for ( i = 0; i < newright; i++ ) *pp3++ = *pp2++;
3421+
for ( i = 0; i < newright; i++ ) { *pp3++ = *pp2++; }
34173422
}
3423+
newleft = pp3 - Pointer;
3424+
while ( pp3 < pptop ) *pp3++ = 0;
34183425
AN.InScratch = 0;
3419-
return(pp3 - Pointer);
3426+
return(newleft);
34203427
}
34213428

34223429
#else
@@ -3545,7 +3552,7 @@ VOID GarbHand(VOID)
35453552
*/
35463553
#ifdef TESTGARB
35473554
MLOCK(ErrorMessageLock);
3548-
MesPrint("in: S->sFill = %x, S->sTop2 = %x",S->sFill,S->sTop2);
3555+
MesPrint("in: S->sFill = %l, S->sTop2 = %l",S->sFill-S->sBuffer,S->sTop2-S->sBuffer);
35493556
#endif
35503557
Point = S->sPointer;
35513558
k = S->sTerms;
@@ -3558,7 +3565,7 @@ VOID GarbHand(VOID)
35583565
if ( ( s2 = *Point++ ) != 0 ) { total += *s2; }
35593566
}
35603567
#ifdef TESTGARB
3561-
MesPrint("total = %l, nterms = %l",2*total,AN.InScratch);
3568+
MesPrint("total = %l, nterms = %l",total,AN.InScratch);
35623569
MUNLOCK(ErrorMessageLock);
35633570
#endif
35643571
/*
@@ -3618,7 +3625,7 @@ VOID GarbHand(VOID)
36183625
S->sFill = s2;
36193626
#ifdef TESTGARB
36203627
MLOCK(ErrorMessageLock);
3621-
MesPrint("out: S->sFill = %x, S->sTop2 = %x",S->sFill,S->sTop2);
3628+
MesPrint("out: S->sFill = %l, S->sTop2 = %l",S->sFill-S->sBuffer,S->sTop2-S->sBuffer);
36223629
if ( S->sFill >= S->sTop2 ) {
36233630
MesPrint("We are in deep trouble");
36243631
}

0 commit comments

Comments
 (0)