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 <stdio.h>

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);
}
This commit is contained in:
Stephen Heumann 2021-09-17 18:25:32 -05:00
parent ee395c371b
commit 379f2f93ad
2 changed files with 19 additions and 7 deletions

12
cc.asm
View File

@ -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
****************************************************************

View File

@ -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