lrh
[fw/sdcc] / src / SDCCmacro.c
index ec961f99adf1b95d5eef55e45356d00e5846a289..2502b04ebffea1ffa1d0391b9e02b223baac1e0d 100644 (file)
@@ -74,12 +74,14 @@ _evalMacros(char *apinto, hTab *pvals, const char *pfrom, size_t alen)
              fprintf (stderr, "Cant find macro \"%s\"\n", name);
              wassertl (0, "Invalid macro name");
             }
-
-          /* Replace */
-          strncpy(pinto, pval, plen);
-          pinto += strlen(pval);
-         plen -= plen > strlen(pval) ? strlen(pval) : plen;
-          fdidsomething = TRUE;
+           else
+           {
+               /* Replace */
+               strncpy(pinto, pval, plen);
+               pinto += strlen(pval);
+               plen -= plen > strlen(pval) ? strlen(pval) : plen;
+               fdidsomething = TRUE;
+           }
 
           pfrom = pend+1;
         }
@@ -116,7 +118,24 @@ mvsprintf(hTab *pvals, const char *pformat, va_list ap)
   /* Recursivly evaluate all the macros in the string */
   _evalMacros(ainto, pvals, pformat, MAX_STRING_LENGTH);
   /* Evaluate all the arguments */
-  vsprintf(atmp, ainto, ap);
+#if defined(HAVE_VSNPRINTF)
+    if (vsnprintf(atmp, MAX_STRING_LENGTH, ainto, ap) >= MAX_STRING_LENGTH)
+    {
+       fprintf(stderr, "Internal error: mvsprintf output truncated.\n");
+    }
+#else    
+    {  
+       int wlen; 
+       
+       wlen = vsprintf(atmp, ainto, ap);
+       
+       if (wlen < 0 || wlen >= MAX_STRING_LENGTH)
+       {
+           wassertl(0, "mvsprintf overflowed.");
+       }
+    }
+#endif    
+    
   /* Recursivly evaluate any macros that were used as arguments */
   _evalMacros(ainto, pvals, atmp, MAX_STRING_LENGTH);