$OpenBSD: patch-gpasm_lst_c,v 1.2 2004/01/30 01:01:11 naddy Exp $
--- gpasm/lst.c.orig	2004-01-17 13:33:51.000000000 +1100
+++ gpasm/lst.c	2004-01-25 20:42:51.000000000 +1100
@@ -76,7 +76,7 @@ void lst_init()
   state.lst.symboltable = 1;
 
   /* Determine state.startdate */
-  gp_date_string(state.lst.startdate);
+  gp_date_string(state.lst.startdate, sizeof(state.lst.startdate));
 
   if (state.cmd_line.macro_expand == 0){
     state.lst.expand = 1;
@@ -93,8 +93,8 @@ void lst_init()
   state.lst.tabstop = 8;	/* Default tabstop every 8 */
 
   if (state.lstfile != named) {
-    strcpy(state.lstfilename, state.basefilename);
-    strcat(state.lstfilename, ".lst");  
+    strlcpy(state.lstfilename, state.basefilename, sizeof(state.lstfilename));
+    strlcat(state.lstfilename, ".lst", sizeof(state.lstfilename));  
   }
 
   if (state.lstfile == suppress) {
@@ -118,7 +118,6 @@ void lst_init()
 void lst_memory_map(MemBlock *m)
 {
   char buf[BUFSIZ];
-  char *e;
   int i, j, base, row_used, num_per_line, num_per_block;
 
   lst_line("");
@@ -151,29 +150,26 @@ void lst_memory_map(MemBlock *m)
       }
 
       if(row_used) {
-        e = buf;
-        sprintf(e, "%08x :", (i + base) << _16bit_core);
-	e += strlen(e);
+        snprintf(buf, sizeof(buf), "%08x :", (i + base) << _16bit_core);
 	for (j = 0; j < num_per_line; j++) {
           if ((j % num_per_block) == 0) {
-	    *e++ = ' ';
+	    strlcat(buf, " ", sizeof(buf));
           }
           if (m->memory[i + j] & MEM_USED_MASK) {
-	    *e++ = 'X';
+	    strlcat(buf, "X", sizeof(buf));
 	    if (_16bit_core) {
 	      /* each word has two bytes */
-	      *e++ = 'X';            
+	      strlcat(buf, "X", sizeof(buf));
 	    }
           } else {
-	    *e++ = '-';
+	    strlcat(buf, "-", sizeof(buf));
 	    if (_16bit_core) {
 	      /* each word has two bytes */
-	      *e++ = '-';            
+	      strlcat(buf, "-", sizeof(buf));
 	    }
           }
         }
 
-	*e = '\0';		/* terminate the new string */
         lst_line(buf);
       }
     }
@@ -185,7 +181,7 @@ void lst_memory_map(MemBlock *m)
   lst_line("All other memory blocks unused.");
   lst_line("");
 
-  sprintf(buf, "Program Memory Words Used: %i", i_memory_used(state.i_memory));
+  snprintf(buf, sizeof(buf), "Program Memory Words Used: %i", i_memory_used(state.i_memory));
   lst_line(buf);
 
 }
@@ -215,51 +211,44 @@ void lst_close()
 void lst_format_line(char *src_line, int value)
 {
   char m[BUFSIZ];
-  char *e;
+  char buf[BUFSIZ];
   unsigned int emitted = 0;
 
   assert(src_line != NULL);
   
-  e = m;
   switch (state.lst.line.linetype) {
   case equ: 
   case set:
-    sprintf(e, "  %08X", value);
-    e += strlen(e);
-    strcpy(e, "     ");
-    e += 5;
+    snprintf(m, sizeof(m), "  %08X", value);
+    strlcat(m, "     ", sizeof(m));
     break;
   case org:
-    sprintf(e, "%04X      ", state.org << _16bit_core);
-    e += strlen(e);
-    strcpy(e, "     ");
-    e += 5;
+    snprintf(m, sizeof(m), "%04X      ", state.org << _16bit_core);
+    strlcat(m, "     ", sizeof(m));
     break;
   case idlocs:
     /* not used for 16 bit devices, config is used */
-    sprintf(e, "%04X %04X %04X ",
+    snprintf(m, sizeof(m), "%04X %04X %04X ",
             state.device.id_location,
             i_memory_get(state.i_memory, state.device.id_location) & 0xffff,
             i_memory_get(state.i_memory, 
 		         state.device.id_location + 1) & 0xffff);
-    e += strlen(e);
     break;
   case insn:
-    sprintf(e, "%04X ", state.lst.line.was_org << _16bit_core);
-    e += strlen(e);
+    snprintf(m, sizeof(m), "%04X ", state.lst.line.was_org << _16bit_core);
     emitted = state.org - state.lst.line.was_org;
-    if (emitted >= 1)
-      sprintf(e, "%04X ", i_memory_get(state.i_memory, 
+    if (emitted >= 1) {
+      snprintf(buf, sizeof(buf), "%04X ", i_memory_get(state.i_memory, 
 			               state.lst.line.was_org) & 0xffff);
-    else
-      sprintf(e, "     ");
-    e += strlen(e);
-    if (emitted >= 2)
-      sprintf(e, "%04X ", i_memory_get(state.i_memory, 
+      strlcat(m, buf, sizeof(m));
+    } else
+      strlcat(m, "     ", sizeof(m));
+    if (emitted >= 2) {
+      snprintf(buf, sizeof(buf), "%04X ", i_memory_get(state.i_memory, 
 			               state.lst.line.was_org + 1) & 0xffff);
-    else
-      sprintf(e, "     ");
-      e += strlen(e);
+      strlcat(m, buf, sizeof(buf));
+    } else
+      strlcat(m, "     ", sizeof(m));
     break;
   case config:
     if(_16bit_core) {
@@ -267,33 +256,28 @@ void lst_format_line(char *src_line, int
 	 words in the list file. */
       if (state.lst.config_address == CONFIG4L) {
         /* Special case */
-        sprintf(e, "%06X %04X    ",   
+        snprintf(m, sizeof(m), "%06X %04X    ",   
                 state.lst.config_address,
                 i_memory_get(state.i_memory, 
 			     state.lst.config_address >> 1) & 0xffff);
-        e += strlen(e);	  
       } else if((state.lst.config_address & 0x1) == 0) {
         /* if it is an even address don't print anything */
-	strcpy(e, "               ");
-        e += 15;
+	strlcpy(m, "               ", sizeof(m));
       } else {
-        sprintf(e, "%06X %04X    ",   
+        snprintf(m, sizeof(m), "%06X %04X    ",   
                 state.lst.config_address - 1,
                 i_memory_get(state.i_memory, 
 			    (state.lst.config_address - 1) >> 1) & 0xffff);
-        e += strlen(e);
       }
     } else {
-      sprintf(e, "%06X %04X    ",   
+      snprintf(m, sizeof(m), "%06X %04X    ",   
               state.lst.config_address,
               i_memory_get(state.i_memory, 
 		           state.lst.config_address) & 0xffff);
-      e += strlen(e);
     }
     break;
   case res:
-    strcpy(e, "               ");
-    e += 15;
+    strlcpy(m, "               ", sizeof(m));
     if (SECTION_FLAGS & STYP_TEXT) {
       /* generate line numbers for res directives in program memory */
       emitted = state.org - state.lst.line.was_org;
@@ -303,22 +287,22 @@ void lst_format_line(char *src_line, int
   case dir:
   case none:
   default:
-    strcpy(e, "               ");
-    e += 15;
+    strlcpy(m, "               ", sizeof(m));
     break;
   }
 
   if (state.stGlobal == state.stTop) {
-    sprintf(e, "%05d ", state.src->line_number);
+    snprintf(buf, sizeof(buf), "%05d ", state.src->line_number);
   } else {		  
-    sprintf(e, "    M ");
+    snprintf(buf, sizeof(buf), "    M ");
   }
-  e += strlen(e);
+  strlcat(m, buf, sizeof(m));
 
   /* Now copy 'l' to 'e', expanding tabs as required */
   {
     int column = 0;
     char *old;
+    char *e = m + strlen(m);
         
     old = src_line;
 
@@ -355,7 +339,7 @@ void lst_format_line(char *src_line, int
 #endif
 
   if (state.lst.line.linetype == idlocs) {
-    sprintf(m, "     %04X %04X ",
+    snprintf(m, sizeof(m), "     %04X %04X ",
             i_memory_get(state.i_memory, 
 		         state.device.id_location + 2) & 0xffff,
             i_memory_get(state.i_memory, 
@@ -368,14 +352,14 @@ void lst_format_line(char *src_line, int
 
     for (i = 2; i < emitted; i += 2) {
       if ((i + 1) < emitted)
-        sprintf(m, "%04X %04X %04X",
+        snprintf(m, sizeof(m), "%04X %04X %04X",
                 ((state.lst.line.was_org + i) << _16bit_core),
                 i_memory_get(state.i_memory, 
                              state.lst.line.was_org + i) & 0xffff,
 		i_memory_get(state.i_memory, 
                              state.lst.line.was_org + i + 1) & 0xffff);
       else
-        sprintf(m, "%04X %04X",
+        snprintf(m, sizeof(m), "%04X %04X",
 		((state.lst.line.was_org + i) << _16bit_core),
 		i_memory_get(state.i_memory, 
 			     state.lst.line.was_org + i) & 0xffff);
@@ -396,7 +380,7 @@ void lst_symbol_table(struct symbol_tabl
   char buf[BUFSIZ];
 
   lst_line("SYMBOL TABLE");
-  sprintf(buf, "%-32s  %-8s", "  LABEL", "  VALUE");
+  snprintf(buf, sizeof(buf), "%-32s  %-8s", "  LABEL", "  VALUE");
   lst_line(buf);
   lst_line("");
 
@@ -414,7 +398,7 @@ void lst_symbol_table(struct symbol_tabl
     struct variable *var;
 
     var = get_symbol_annotation(lst[i]);
-    sprintf(buf,
+    snprintf(buf, sizeof(buf),
             symbol_format, 
             get_symbol_name(lst[i]),
             var ? var->value : 0);
@@ -444,7 +428,7 @@ void lst_defines_table(struct symbol_tab
     char *defined_as;
 
     defined_as = get_symbol_annotation(lst[i]);
-    sprintf(buf,
+    snprintf(buf, sizeof(buf),
             symbol_format, 
             get_symbol_name(lst[i]),
             defined_as);
