Buffer overflow hunt: removing strcpy, strcat, sprintf
[fw/sdcc] / src / SDCCmacro.c
1 /*-------------------------------------------------------------------------
2   SDCCmain.c - Macro support code.
3
4              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 #include "common.h"
26
27 enum 
28   {
29     MAX_STRING_LENGTH     = 2048,
30     MAX_MACRO_NAME_LENGTH = 128
31   };
32
33 void
34 _evalMacros(char *apinto, hTab *pvals, const char *pfrom, size_t alen)
35 {
36   bool  fdidsomething = FALSE;
37   char  *pinto = apinto;
38   size_t plen = alen;
39
40   assert(pinto);
41   assert(pvals);
42   assert(pfrom);
43
44   while (plen && *pfrom)
45     {
46       if (*pfrom == '{')
47         {
48           const char *pend = ++pfrom;
49           char name[MAX_MACRO_NAME_LENGTH];
50           const char *pval;
51
52           while (*pend && *pend != '}')
53             {
54               pend++;
55             }
56           if (*pend != '}')
57             {
58               wassertl(0, "Unterminated macro expansion");
59             }
60           /* Pull out the macro name */
61           if (pend - pfrom >= MAX_MACRO_NAME_LENGTH)
62           {
63               wassertl(0, "macro name too long");
64           }
65             
66           strncpy(name, pfrom, pend-pfrom);
67           name[pend-pfrom] = '\0';
68
69           /* Look up the value in the hash table */
70           pval = shash_find (pvals, name);
71           
72           if (pval == NULL)
73             {
74               fprintf (stderr, "Cant find macro \"%s\"\n", name);
75               wassertl (0, "Invalid macro name");
76             }
77
78           /* Replace */
79           strncpy(pinto, pval, plen);
80           pinto += strlen(pval);
81           plen -= plen > strlen(pval) ? strlen(pval) : plen;
82           fdidsomething = TRUE;
83
84           pfrom = pend+1;
85         }
86       else
87         {
88           /* Pass through */
89           *pinto++ = *pfrom++;
90           plen--;
91         }
92     }
93
94   if (!plen)
95   {
96       wassertl(0, "macro expansion too long");
97   }
98     
99   *pinto = '\0';
100
101   /* If we did something then recursivly expand any expanded macros */
102   if (fdidsomething)
103     {
104       char ainto[MAX_STRING_LENGTH];
105       _evalMacros(ainto, pvals, apinto, MAX_STRING_LENGTH);
106       strncpyz(apinto, ainto, alen);
107     }
108 }
109
110 char *
111 mvsprintf(hTab *pvals, const char *pformat, va_list ap)
112 {
113   char ainto[MAX_STRING_LENGTH];
114   char atmp[MAX_STRING_LENGTH];
115
116   /* Recursivly evaluate all the macros in the string */
117   _evalMacros(ainto, pvals, pformat, MAX_STRING_LENGTH);
118   /* Evaluate all the arguments */
119   vsprintf(atmp, ainto, ap);
120   /* Recursivly evaluate any macros that were used as arguments */
121   _evalMacros(ainto, pvals, atmp, MAX_STRING_LENGTH);
122
123   /* Return a copy of the evaluated string. */
124   return Safe_strdup(ainto);
125 }
126
127 char *msprintf(hTab *pvals, const char *pformat, ...)
128 {
129   va_list ap;
130   char *pret;
131
132   va_start(ap, pformat);
133
134   pret = mvsprintf(pvals, pformat, ap);
135
136   va_end(ap);
137
138   return pret;
139 }
140
141 void
142 mfprintf(FILE *fp, hTab *pvals, const char *pformat, ...)
143 {
144   va_list ap;
145   char *p;
146
147   va_start(ap, pformat);
148
149   p = mvsprintf(pvals, pformat, ap);
150
151   va_end(ap);
152
153   fputs(p, fp);
154   Safe_free(p);
155 }