shithub: rgbds

Download patch

ref: fc2bf3d11d14170f5aeee171cbb3f3cf1bced825
parent: 8415ce3ed0562384a197cea430968bc8f414987c
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Fri Feb 12 10:27:21 EST 2021

Prefer `sizeof($$)` to `MAXSTRLEN + 1`

This makes `strsubUTF8` similar to `strrpl` and `strfmt`

--- a/src/asm/parser.y
+++ b/src/asm/parser.y
@@ -108,7 +108,7 @@
 	return len;
 }
 
-static void strsubUTF8(char *dest, const char *src, uint32_t pos, uint32_t len)
+static void strsubUTF8(char *dest, size_t destLen, const char *src, uint32_t pos, uint32_t len)
 {
 	size_t srcIndex = 0;
 	size_t destIndex = 0;
@@ -141,7 +141,7 @@
 			(unsigned long)pos);
 
 	/* Copy from source to destination. */
-	while (src[srcIndex] && destIndex < MAXSTRLEN && curLen < len) {
+	while (src[srcIndex] && destIndex < destLen - 1 && curLen < len) {
 		switch (decode(&state, &codep, src[srcIndex])) {
 		case 1:
 			fatalerror("STRSUB: Invalid UTF-8 character\n");
@@ -160,7 +160,7 @@
 	if (state != 0)
 		fatalerror("STRSUB: Invalid UTF-8 character\n");
 
-	dest[destIndex] = 0;
+	dest[destIndex] = '\0';
 }
 
 static void strrpl(char *dest, size_t destLen, char const *src, char const *old, char const *new)
@@ -1402,7 +1402,7 @@
 
 string		: T_STRING
 		| T_OP_STRSUB T_LPAREN string T_COMMA uconst T_COMMA uconst T_RPAREN {
-			strsubUTF8($$, $3, $5, $7);
+			strsubUTF8($$, sizeof($$), $3, $5, $7);
 		}
 		| T_OP_STRCAT T_LPAREN T_RPAREN {
 			$$[0] = '\0';
@@ -1427,7 +1427,7 @@
 
 strcat_args	: string
 		| strcat_args T_COMMA string {
-			if (snprintf($$, sizeof($$), "%s%s", $1, $3) > MAXSTRLEN)
+			if (snprintf($$, sizeof($$), "%s%s", $1, $3) >= sizeof($$))
 				warning(WARNING_LONG_STR, "STRCAT: String too long '%s%s'\n",
 					$1, $3);
 		}