From bbfad1e299d0e612c97307efa04368eded81ae4b Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Mon, 19 Feb 2024 22:01:53 -0600 Subject: [PATCH] strncat: fix more issues related to large n values. This addresses the following issues: *If the low-order 16 bits of n were 0x0000, no concatenation would be performed. *If n was 0x1000000 or greater, the output could be cut off prematurely because the high byte of n was effectively ignored. The following test program demonstrates these issues: #pragma memorymodel 1 #include #include #include #define LEN2 100000 int main(void) { char *s1 = malloc(LEN2+2); char *s2 = malloc(LEN2+1); if (!s1 || !s2) return 0; for (unsigned long i = 0; i < LEN2; i++) s2[i] = '0' + (i & 0x07); strcpy(s1,"a"); strncat(s1, s2, 0x1000000); puts(s1); printf("len = %zu\n", strlen(s1)); } --- string.asm | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/string.asm b/string.asm index d3905b2..b180556 100644 --- a/string.asm +++ b/string.asm @@ -1017,29 +1017,33 @@ lb2 long M sta s1 bcc lb2a inc s1+2 -lb2a short M copy characters 'til the null is found +lb2a ldx n copy characters 'til the null is found + bne lb2b + lda n+2 + beq lb6 +lb2b short M ldy #0 - ldx n - beq lb4 lb3 lda [s2],Y sta [s1],Y - beq lb4 + beq lb5 iny bne lb3a inc s1+2 inc s2+2 lb3a dex bne lb3 - lda n+2 + ldx n+2 beq lb4 - dec n+2 + dex + stx n+2 + ldx #0 bra lb3 lb4 lda #0 write the terminating null sta [s1],Y - long M return to the caller +lb5 long M return to the caller - creturn 4:rval +lb6 creturn 4:rval end ****************************************************************