From 379f2f93ad6fa2cce2eba85a4df2f1d059a0052a Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Fri, 17 Sep 2021 18:25:32 -0500 Subject: [PATCH] Fix bug causing data corruption when assigning to multiple structs. This affects code where multiple structs or unions are assigned by successive = operators in one expression, e.g. "s1=s2=s3". The middle struct assignment(s) would use the ~Move2 or ~LongMove2 helper functions (for <64k or >=64k moves, respectively). These functions are supposed to leave the destination pointer on the stack so it can be used as the source of a subsequent move, but they both had bugs where they could modify dest and leave that modified value on the stack, which would cause subsequent moves to use the wrong source location. In the case of ~Move2, this only happened if the size was odd. Here is a program that demonstrated the problems with both functions: #pragma memorymodel 1 #include struct S1 { char s[80000]; } a,b,c; int main(void) { struct S2 { int x,y; char z; } d,e,f; c.s[66000] = 123; f.y = 5678; a = b = c; d = e = f; printf("%i %i %i\n", a.s[66000], b.s[66000], c.s[66000]); printf("%i %i %i\n", d.y, e.y, f.y); } --- cc.asm | 12 ++++++++++-- cc.macros | 14 +++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/cc.asm b/cc.asm index 298fc6a..77227b5 100644 --- a/cc.asm +++ b/cc.asm @@ -727,6 +727,8 @@ lb3 sec csubroutine (4:len,4:source),0 dest equ source+4 + pei dest+2 save original dest value + pei dest ldx len+2 move whole banks beq lm2 ldy #0 @@ -761,7 +763,11 @@ lb2 lda [source],Y bne lb2 lb3 lda [source] sta [dest] -lb4 creturn +lb4 pla restore original dest value + sta dest + pla + sta dest+2 + creturn end **************************************************************** @@ -862,7 +868,9 @@ lb2 lda [source],Y bne lb2 lb3 lda [source] sta [dest] -lb4 creturn +lb4 bcc lb5 if the move length was odd + dec4 dest restore original dest value +lb5 creturn end **************************************************************** diff --git a/cc.macros b/cc.macros index 52b8821..c86c357 100644 --- a/cc.macros +++ b/cc.macros @@ -280,11 +280,6 @@ ~&SYSCNT ~RESTM MEND MACRO -&LAB JEQ &BP -&LAB BNE *+5 - BRL &BP - MEND - MACRO &LAB LONG &A,&B LCLB &I LCLB &M @@ -539,3 +534,12 @@ .j rtl mend + macro +&l dec4 &a +&l ~setm + lda &a + bne ~&SYSCNT + dec 2+&a +~&SYSCNT dec &a + ~restm + mend