diff lwasm/list.c @ 508:10f62dc61a75

Fix bad usage of sprintf() Usage of sprintf() to append to a string in the form of sprintf(buf, "%s...", buf...) is undefined, regardless whether it worked on a lot of older systems. It was always a bad idea and it now breaks on current glibc and gcc development environments. The moral: if any of your code uses sprintf() in a way similar to the above, fix it. It may not fail in a benign way.
author William Astle <lost@l-w.ca>
date Sun, 10 May 2020 22:38:24 -0600
parents 40c32a0af8c8
children f3018ed5e30e
line wrap: on
line diff
--- a/lwasm/list.c	Thu Feb 06 15:04:05 2020 -0700
+++ b/lwasm/list.c	Sun May 10 22:38:24 2020 -0600
@@ -231,28 +231,25 @@
 		char s[64] = "";
 		if (CURPRAGMA(cl, PRAGMA_C) || CURPRAGMA(cl, PRAGMA_CD))
 		{
+			char sch = '(', ech = ')';
+			if (CURPRAGMA(cl, PRAGMA_6809))
+	 		{
+				sch = '[';
+				ech = ']';
+			}
 			if (cl->cycle_base != 0)
 			{
-				char ch = '(';
-				if (CURPRAGMA(cl, PRAGMA_6809)) ch = '[';
+				int est = cl -> cycle_flags & CYCLE_ESTIMATED;
 
 				if (CURPRAGMA(cl, PRAGMA_CD) && cl->cycle_flags & CYCLE_ADJ)
 				{
-					sprintf(s, "%c%d+%d", ch, cl->cycle_base, cl->cycle_adj);	/* detailed cycle count */
+					sprintf(s, "%c%d+%d%s%c", sch, cl->cycle_base, cl->cycle_adj, est ? "+?" : "", ech);	/* detailed cycle count */
 				}
 				else
 				{
-					sprintf(s, "%c%d", ch, cl->cycle_base + cl->cycle_adj);   /* normal cycle count*/
+					sprintf(s, "%c%d%s%c", sch, cl->cycle_base + cl->cycle_adj, est ? "+?" : "", ech);   /* normal cycle count*/
 				}
-
-				if (cl->cycle_flags & CYCLE_ESTIMATED)
-					strcat(s, "+?");
-
 				as->cycle_total += cl->cycle_base + cl->cycle_adj;
-
-				ch = ')';
-				if (CURPRAGMA(cl, PRAGMA_6809)) ch = ']';
-				sprintf(s, "%s%c", s, ch);
 			}
 		}