aboutsummaryrefslogtreecommitdiff
path: root/devel/nasm/files/patch-preproc.c
blob: d75b4ccdb763d0f459d2026fa8dc96ae8a122b98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

$FreeBSD$

--- preproc.c.orig
+++ preproc.c
@@ -528,7 +528,7 @@
 	    fname++;
 	fnlen = strcspn(fname, "\"");
 	line = nasm_malloc(20 + fnlen);
-	sprintf(line, "%%line %d %.*s", lineno, fnlen, fname);
+	snprintf(line, 20+fnlen,"%%line %d %.*s", lineno, fnlen, fname);
 	nasm_free(oldline);
     }
     if (tasm_compatible_mode)
@@ -833,6 +833,7 @@
 	    type = TOK_STRING;
 	    while (*p && *p != c)
 		p++;
+
 	    if (*p)
 	    {
 		p++;
@@ -840,6 +841,8 @@
 	    else
 	    {
 		error(ERR_WARNING, "unterminated string");
+		/* Handling unterminated strings by UNV */
+		/* type = -1; */
 	    }
 	}
 	else if (isnumstart(*p))
@@ -901,6 +904,15 @@
 	    }
 	    p++;
 	}
+
+	/* Handling unterminated string by UNV */
+	/*if (type == -1)
+	{
+	    *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
+	    t->text[p-line] = *line;
+	    tail = &t->next;
+	}
+	else*/
 	if (type != TOK_COMMENT)
 	{
 	    *tail = t = new_Token(NULL, type, line, p - line);
@@ -919,20 +931,20 @@
 static void *
 new_Block(size_t size)
 {
-	Blocks *b = &blocks;
+    Blocks *b = &blocks;
 	
-	/* first, get to the end of the linked list	 */
-	while (b->next)
-		b = b->next;
-	/* now allocate the requested chunk */
-	b->chunk = nasm_malloc(size);
+    /* first, get to the end of the linked list */
+    while (b->next)
+	b = b->next;
+    /* now allocate the requested chunk */
+    b->chunk = nasm_malloc(size);
 	
-	/* now allocate a new block for the next request */
-	b->next = nasm_malloc(sizeof(Blocks));
-	/* and initialize the contents of the new block */
-	b->next->next = NULL;
-	b->next->chunk = NULL;
-	return b->chunk;
+    /* now allocate a new block for the next request */
+    b->next = nasm_malloc(sizeof(Blocks));
+    /* and initialize the contents of the new block */
+    b->next->next = NULL;
+    b->next->chunk = NULL;
+    return b->chunk;
 }
 
 /*
@@ -941,22 +953,22 @@
 static void
 delete_Blocks(void)
 {
-	Blocks *a,*b = &blocks;
+    Blocks *a,*b = &blocks;
 
-	/* 
-	 * keep in mind that the first block, pointed to by blocks
-	 * is a static and not dynamically allocated, so we don't 
-	 * free it.
-	 */
-	while (b)
-	{
-		if (b->chunk)
-			nasm_free(b->chunk);
-		a = b;
-		b = b->next;
-                if (a != &blocks)
-			nasm_free(a);
-	}
+    /* 
+     * keep in mind that the first block, pointed to by blocks
+     * is a static and not dynamically allocated, so we don't 
+     * free it.
+     */
+    while (b)
+    {
+	if (b->chunk)
+	    nasm_free(b->chunk);
+	a = b;
+	b = b->next;
+	if (a != &blocks)
+	    nasm_free(a);
+    }
 }	
 
 /*
@@ -1043,7 +1055,7 @@
 		char *p, *q = t->text + 2;
 
 		q += strspn(q, "$");
-		sprintf(buffer, "..@%lu.", ctx->number);
+		snprintf(buffer, sizeof(buffer), "..@%lu.", ctx->number);
 		p = nasm_strcat(buffer, q);
 		nasm_free(t->text);
 		t->text = p;
@@ -1520,23 +1532,30 @@
 		    t = t->next;
 		    continue;
 		}
-		else if (tt->type == TOK_WHITESPACE)
+		if (tt->type == TOK_WHITESPACE)
 		{
 		    tt = tt->next;
 		    continue;
 		}
-		else if (tt->type != t->type ||
-			mstrcmp(tt->text, t->text, casesense))
+		if (tt->type != t->type)
 		{
 		    j = FALSE;	/* found mismatching tokens */
 		    break;
 		}
-		else
+		/* Unify surrounding quotes for strings */
+		if (t->type == TOK_STRING)
 		{
-		    t = t->next;
-		    tt = tt->next;
-		    continue;
+		    tt->text[0] = t->text[0];
+		    tt->text[strlen(tt->text) - 1] = t->text[0];
 		}
+		if (mstrcmp(tt->text, t->text, casesense) != 0)
+		{
+		    j = FALSE;	/* found mismatching tokens */
+		    break;
+		}
+
+		t = t->next;
+		tt = tt->next;
 	    }
 	    if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
 		j = FALSE;	/* trailing gunk on one end or other */
@@ -1954,7 +1973,7 @@
 		free_tlist(tt);
 
 		/* Now define the macro for the argument */
-		sprintf(directive, "%%define %s (%s+%d)", arg, StackPointer,
+		snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", arg, StackPointer,
 			offset);
 		do_directive(tokenise(directive));
 		offset += size;
@@ -2051,13 +2070,13 @@
 		free_tlist(tt);
 
 		/* Now define the macro for the argument */
-		sprintf(directive, "%%define %s (%s-%d)", local, StackPointer,
+		snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", local, StackPointer,
 			offset);
 		do_directive(tokenise(directive));
 		offset += size;
 
 		/* Now define the assign to setup the enter_c macro correctly */
-		sprintf(directive, "%%assign %%$localsize %%$localsize+%d",
+		snprintf(directive, sizeof(directive), "%%assign %%$localsize %%$localsize+%d",
 			size);
 		do_directive(tokenise(directive));
 
@@ -3182,12 +3201,12 @@
 			 */
 		    case '0':
 			type = TOK_NUMBER;
-			sprintf(tmpbuf, "%d", mac->nparam);
+			snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
 			text = nasm_strdup(tmpbuf);
 			break;
 		    case '%':
 			type = TOK_ID;
-			sprintf(tmpbuf, "..@%lu.", mac->unique);
+			snprintf(tmpbuf, sizeof(tmpbuf), "..@%lu.", mac->unique);
 			text = nasm_strcat(tmpbuf, t->text + 2);
 			break;
 		    case '-':
@@ -4067,7 +4086,7 @@
 	return;
 
     va_start(arg, fmt);
-    vsprintf(buff, fmt, arg);
+    vsnprintf(buff, sizeof(buff), fmt, arg);
     va_end(arg);
 
     if (istk && istk->mstk && istk->mstk->name)
@@ -4530,7 +4549,7 @@
 make_tok_num(Token * tok, long val)
 {
     char numbuf[20];
-    sprintf(numbuf, "%ld", val);
+    snprintf(numbuf, sizeof(numbuf), "%ld", val);
     tok->text = nasm_strdup(numbuf);
     tok->type = TOK_NUMBER;
 }