From f1582be5a2d0799655e19b10865de4af13a33986 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sun, 18 Feb 2024 21:53:03 -0600 Subject: [PATCH] Fix handling of large strings in strncat. There were two issues: *If bit 15 of the n value was set, the second string would not be copied. *If the length of the second string was 64K or more, it would not be copied properly because the pointers were not updated. This test program demonstrates both 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, LEN2); puts(s1); printf("len = %zu\n", strlen(s1)); } --- string.asm | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/string.asm b/string.asm index 6a06be5..d3905b2 100644 --- a/string.asm +++ b/string.asm @@ -1021,12 +1021,14 @@ lb2a short M copy characters 'til the null is found ldy #0 ldx n beq lb4 - bmi lb4 lb3 lda [s2],Y sta [s1],Y beq lb4 iny - dex + bne lb3a + inc s1+2 + inc s2+2 +lb3a dex bne lb3 lda n+2 beq lb4