| @@ -32,11 +32,6 @@ · 31 unmodified lines |
| 32 | 32 | /* preprocessor debug */ |
| 33 | 33 | //#define PP_DEBUG |
| 34 | 34 | |
| 35 | | −/* these sizes are dummy for unix, because malloc() does not use |
| 36 | | − memory when the pages are not used */ |
| 37 | | −#define TEXT_SIZE (4*1024*1024) |
| 38 | | −#define DATA_SIZE (4*1024*1024) |
| 39 | | − |
| 40 | 35 | /* amount of virtual memory associate to a section (currently, we do |
| 41 | 36 | not realloc them) */ |
| 42 | 37 | #define SECTION_VSIZE (1024 * 1024) |
| @@ -398,6 +393,8 @@ void gen_op(int op); · 355 unmodified lines |
| 398 | 393 | void force_charshort_cast(int t); |
| 399 | 394 | void gen_cast(int t); |
| 400 | 395 | void vstore(void); |
| 396 | +Sym *sym_find(int v); |
| 397 | +Sym *sym_push(int v, int t, int r, int c); |
| 401 | 398 | |
| 402 | 399 | /* type handling */ |
| 403 | 400 | int type_size(int t, int *a); |
| @@ -546,7 +543,7 @@ unsigned long long __ldtoull(long double a) · 142 unmodified lines |
| 546 | 543 | |
| 547 | 544 | /********************************************************/ |
| 548 | 545 | |
| 549 | | −/* copy a string and trucate it */ |
| 546 | +/* copy a string and truncate it */ |
| 550 | 547 | char *pstrcpy(char *buf, int buf_size, const char *s) |
| 551 | 548 | { |
| 552 | 549 | char *q, *q_end; |
| @@ -564,6 +561,16 @@ char *pstrcpy(char *buf, int buf_size, const char *s) · 11 unmodified lines |
| 564 | 561 | return buf; |
| 565 | 562 | } |
| 566 | 563 | |
| 564 | +/* strcat and truncate */ |
| 565 | +char *pstrcat(char *buf, int buf_size, const char *s) |
| 566 | +{ |
| 567 | + int len; |
| 568 | + len = strlen(buf); |
| 569 | + if (len < buf_size) |
| 570 | + pstrcpy(buf + len, buf_size - len, s); |
| 571 | + return buf; |
| 572 | +} |
| 573 | + |
| 567 | 574 | Section *new_section(const char *name) |
| 568 | 575 | { |
| 569 | 576 | Section *sec, **psec; |
| @@ -1235,8 +1242,7 @@ void preprocess(void) · 665 unmodified lines |
| 1235 | 1242 | next(); |
| 1236 | 1243 | if (tok != TOK_STR) |
| 1237 | 1244 | error("#include syntax error"); |
| 1238 | | − /* XXX: buffer overflow */ |
| 1239 | | − strcpy(buf, get_tok_str(tok, &tokc)); |
| 1245 | + pstrcpy(buf, sizeof(buf), get_tok_str(tok, &tokc)); |
| 1240 | 1246 | c = '\"'; |
| 1241 | 1247 | } |
| 1242 | 1248 | /* eat all spaces and comments after include */ |
| @@ -1248,14 +1254,15 @@ void preprocess(void) · 5 unmodified lines |
| 1248 | 1254 | error("memory full"); |
| 1249 | 1255 | if (c == '\"') { |
| 1250 | 1256 | /* first search in current dir if "header.h" */ |
| 1251 | | − /* XXX: buffer overflow */ |
| 1252 | 1257 | size = 0; |
| 1253 | 1258 | p = strrchr(filename, '/'); |
| 1254 | 1259 | if (p) |
| 1255 | 1260 | size = p + 1 - filename; |
| 1261 | + if (size > sizeof(buf1) - 1) |
| 1262 | + size = sizeof(buf1) - 1; |
| 1256 | 1263 | memcpy(buf1, filename, size); |
| 1257 | 1264 | buf1[size] = '\0'; |
| 1258 | | − strcat(buf1, buf); |
| 1265 | + pstrcat(buf1, sizeof(buf1), buf); |
| 1259 | 1266 | f = fopen(buf1, "r"); |
| 1260 | 1267 | if (f) |
| 1261 | 1268 | goto found; |
| @@ -1850,13 +1857,12 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 588 unmodified lines |
| 1850 | 1857 | if (s) { |
| 1851 | 1858 | token_buf[0] = '\0'; |
| 1852 | 1859 | st = (int *)s->c; |
| 1853 | | − /* XXX: buffer overflow */ |
| 1854 | 1860 | notfirst = 0; |
| 1855 | 1861 | while (*st) { |
| 1856 | 1862 | if (notfirst) |
| 1857 | | − strcat(token_buf, " "); |
| 1863 | + pstrcat(token_buf, sizeof(token_buf), " "); |
| 1858 | 1864 | t = tok_get(&st, &cval); |
| 1859 | | − strcat(token_buf, get_tok_str(t, &cval)); |
| 1865 | + pstrcat(token_buf, sizeof(token_buf), get_tok_str(t, &cval)); |
| 1860 | 1866 | notfirst = 1; |
| 1861 | 1867 | } |
| 1862 | 1868 | #ifdef PP_DEBUG |
| @@ -1918,11 +1924,10 @@ int *macro_twosharps(int *macro_str) · 55 unmodified lines |
| 1918 | 1924 | ident + ident or ident + number */ |
| 1919 | 1925 | if (tok >= TOK_IDENT && |
| 1920 | 1926 | (t >= TOK_IDENT || t == TOK_CINT)) { |
| 1921 | | − /* XXX: buffer overflow */ |
| 1922 | 1927 | p = get_tok_str(tok, &tokc); |
| 1923 | | − strcpy(token_buf, p); |
| 1928 | + pstrcpy(token_buf, sizeof(token_buf), p); |
| 1924 | 1929 | p = get_tok_str(t, &cval); |
| 1925 | | − strcat(token_buf, p); |
| 1930 | + pstrcat(token_buf, sizeof(token_buf), p); |
| 1926 | 1931 | ts = tok_alloc(token_buf, 0); |
| 1927 | 1932 | tok = ts->tok; /* modify current token */ |
| 1928 | 1933 | } else { |
| @@ -3241,87 +3246,90 @@ int is_compatible_types(int t1, int t2) · 1312 unmodified lines |
| 3241 | 3246 | printed in the type */ |
| 3242 | 3247 | /* XXX: union */ |
| 3243 | 3248 | /* XXX: add array and function pointers */ |
| 3244 | | −/* XXX: buffer overflows */ |
| 3245 | 3249 | void type_to_str(char *buf, int buf_size, |
| 3246 | 3250 | int t, const char *varstr) |
| 3247 | 3251 | { |
| 3248 | 3252 | int bt, v; |
| 3249 | 3253 | Sym *s, *sa; |
| 3250 | 3254 | char buf1[256]; |
| 3255 | + const char *tstr; |
| 3251 | 3256 | |
| 3252 | 3257 | t = t & VT_TYPE; |
| 3253 | 3258 | bt = t & VT_BTYPE; |
| 3254 | 3259 | buf[0] = '\0'; |
| 3255 | 3260 | if (t & VT_UNSIGNED) |
| 3256 | | − strcat(buf, "unsigned "); |
| 3261 | + pstrcat(buf, buf_size, "unsigned "); |
| 3257 | 3262 | switch(bt) { |
| 3258 | 3263 | case VT_VOID: |
| 3259 | | − strcat(buf, "void"); |
| 3260 | | − break; |
| 3264 | + tstr = "void"; |
| 3265 | + goto add_tstr; |
| 3261 | 3266 | case VT_BOOL: |
| 3262 | | − strcat(buf, "_Bool"); |
| 3263 | | − break; |
| 3267 | + tstr = "_Bool"; |
| 3268 | + goto add_tstr; |
| 3264 | 3269 | case VT_BYTE: |
| 3265 | | − strcat(buf, "char"); |
| 3266 | | − break; |
| 3270 | + tstr = "char"; |
| 3271 | + goto add_tstr; |
| 3267 | 3272 | case VT_SHORT: |
| 3268 | | − strcat(buf, "short"); |
| 3269 | | − break; |
| 3273 | + tstr = "short"; |
| 3274 | + goto add_tstr; |
| 3270 | 3275 | case VT_INT: |
| 3271 | | − strcat(buf, "int"); |
| 3272 | | − break; |
| 3276 | + tstr = "int"; |
| 3277 | + goto add_tstr; |
| 3273 | 3278 | case VT_LONG: |
| 3274 | | − strcat(buf, "long"); |
| 3275 | | − break; |
| 3279 | + tstr = "long"; |
| 3280 | + goto add_tstr; |
| 3276 | 3281 | case VT_LLONG: |
| 3277 | | − strcat(buf, "long long"); |
| 3278 | | − break; |
| 3282 | + tstr = "long long"; |
| 3283 | + goto add_tstr; |
| 3279 | 3284 | case VT_FLOAT: |
| 3280 | | − strcat(buf, "float"); |
| 3281 | | − break; |
| 3285 | + tstr = "float"; |
| 3286 | + goto add_tstr; |
| 3282 | 3287 | case VT_DOUBLE: |
| 3283 | | − strcat(buf, "double"); |
| 3284 | | − break; |
| 3288 | + tstr = "double"; |
| 3289 | + goto add_tstr; |
| 3285 | 3290 | case VT_LDOUBLE: |
| 3286 | | − strcat(buf, "long double"); |
| 3291 | + tstr = "long double"; |
| 3292 | + add_tstr: |
| 3293 | + pstrcat(buf, buf_size, tstr); |
| 3287 | 3294 | break; |
| 3288 | 3295 | case VT_ENUM: |
| 3289 | 3296 | case VT_STRUCT: |
| 3290 | 3297 | if (bt == VT_STRUCT) |
| 3291 | | − strcat(buf, "struct "); |
| 3298 | + tstr = "struct "; |
| 3292 | 3299 | else |
| 3293 | | − strcat(buf, "enum "); |
| 3300 | + tstr = "enum "; |
| 3301 | + pstrcat(buf, buf_size, tstr); |
| 3294 | 3302 | v = (unsigned)t >> VT_STRUCT_SHIFT; |
| 3295 | 3303 | if (v >= SYM_FIRST_ANOM) |
| 3296 | | − strcat(buf, "<anonymous>"); |
| 3304 | + pstrcat(buf, buf_size, "<anonymous>"); |
| 3297 | 3305 | else |
| 3298 | | − strcat(buf, get_tok_str(v, NULL)); |
| 3306 | + pstrcat(buf, buf_size, get_tok_str(v, NULL)); |
| 3299 | 3307 | break; |
| 3300 | 3308 | case VT_FUNC: |
| 3301 | 3309 | s = sym_find((unsigned)t >> VT_STRUCT_SHIFT); |
| 3302 | 3310 | type_to_str(buf, buf_size, s->t, varstr); |
| 3303 | | − strcat(buf, "("); |
| 3311 | + pstrcat(buf, buf_size, "("); |
| 3304 | 3312 | sa = s->next; |
| 3305 | 3313 | while (sa != NULL) { |
| 3306 | 3314 | type_to_str(buf1, sizeof(buf1), sa->t, NULL); |
| 3307 | | − strcat(buf, buf1); |
| 3315 | + pstrcat(buf, buf_size, buf1); |
| 3308 | 3316 | sa = sa->next; |
| 3309 | 3317 | if (sa) |
| 3310 | | − strcat(buf, ", "); |
| 3318 | + pstrcat(buf, buf_size, ", "); |
| 3311 | 3319 | } |
| 3312 | | − strcat(buf, ")"); |
| 3320 | + pstrcat(buf, buf_size, ")"); |
| 3313 | 3321 | goto no_var; |
| 3314 | 3322 | case VT_PTR: |
| 3315 | 3323 | s = sym_find((unsigned)t >> VT_STRUCT_SHIFT); |
| 3316 | | − strcpy(buf1, "*"); |
| 3324 | + pstrcpy(buf1, sizeof(buf1), "*"); |
| 3317 | 3325 | if (varstr) |
| 3318 | | − strcat(buf1, varstr); |
| 3326 | + pstrcat(buf1, sizeof(buf1), varstr); |
| 3319 | 3327 | type_to_str(buf, buf_size, s->t, buf1); |
| 3320 | 3328 | goto no_var; |
| 3321 | 3329 | } |
| 3322 | 3330 | if (varstr) { |
| 3323 | | − strcat(buf, " "); |
| 3324 | | − strcat(buf, varstr); |
| 3331 | + pstrcat(buf, buf_size, " "); |
| 3332 | + pstrcat(buf, buf_size, varstr); |
| 3325 | 3333 | } |
| 3326 | 3334 | no_var: ; |
| 3327 | 3335 | } |
| @@ -5147,7 +5155,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 1819 unmodified lines |
| 5147 | 5155 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ |
| 5148 | 5156 | void decl(int l) |
| 5149 | 5157 | { |
| 5150 | | − int *a, t, b, v, addr, has_init, size, align, r, u; |
| 5158 | + int t, b, v, addr, has_init, r; |
| 5151 | 5159 | Sym *sym; |
| 5152 | 5160 | AttributeDef ad; |
| 5153 | 5161 | |
| @@ -5207,41 +5215,12 @@ void decl(int l) · 53 unmodified lines |
| 5207 | 5215 | funcname = get_tok_str(v, NULL); |
| 5208 | 5216 | /* push a dummy symbol to enable local sym storage */ |
| 5209 | 5217 | sym_push1(&local_stack, 0, 0, 0); |
| 5210 | | − /* define parameters */ |
| 5211 | | − sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT); |
| 5212 | | − /* XXX: the following is x86 dependant -> move it to |
| 5213 | | − x86 code gen */ |
| 5214 | | − addr = 8; |
| 5215 | | − /* if the function returns a structure, then add an |
| 5216 | | − implicit pointer parameter */ |
| 5217 | | − func_vt = sym->t; |
| 5218 | | − if ((func_vt & VT_BTYPE) == VT_STRUCT) { |
| 5219 | | − func_vc = addr; |
| 5220 | | − addr += 4; |
| 5221 | | − } |
| 5222 | | − while ((sym = sym->next) != NULL) { |
| 5223 | | − u = sym->t; |
| 5224 | | − sym_push(sym->v & ~SYM_FIELD, u, |
| 5225 | | − VT_LOCAL | VT_LVAL, addr); |
| 5226 | | − size = type_size(u, &align); |
| 5227 | | − size = (size + 3) & ~3; |
| 5228 | | −#ifdef FUNC_STRUCT_PARAM_AS_PTR |
| 5229 | | − /* structs are passed as pointer */ |
| 5230 | | − if ((u & VT_BTYPE) == VT_STRUCT) { |
| 5231 | | − size = 4; |
| 5232 | | − } |
| 5233 | | −#endif |
| 5234 | | − addr += size; |
| 5235 | | − } |
| 5218 | + gfunc_prolog(t); |
| 5236 | 5219 | loc = 0; |
| 5237 | | − o(0xe58955); /* push %ebp, mov %esp, %ebp */ |
| 5238 | | − a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */ |
| 5239 | 5220 | rsym = 0; |
| 5240 | 5221 | block(NULL, NULL, NULL, NULL, 0); |
| 5241 | 5222 | gsym(rsym); |
| 5242 | | − o(0xc3c9); /* leave, ret */ |
| 5243 | | − *a = (-loc + 3) & -4; /* align local size to word & |
| 5244 | | − save local variables */ |
| 5223 | + gfunc_epilog(); |
| 5245 | 5224 | cur_text_section->data_ptr = (unsigned char *)ind; |
| 5246 | 5225 | sym_pop(&label_stack, NULL); /* reset label stack */ |
| 5247 | 5226 | sym_pop(&local_stack, NULL); /* reset local stack */ |
| @@ -5398,8 +5377,7 @@ void define_symbol(const char *sym) · 150 unmodified lines |
| 5398 | 5377 | |
| 5399 | 5378 | p = strchr(sym, '='); |
| 5400 | 5379 | if (!p) { |
| 5401 | | − /* XXX: buffer overflow */ |
| 5402 | | − strcpy(buf, sym); |
| 5380 | + pstrcpy(buf, sizeof(buf), sym); |
| 5403 | 5381 | p = "1"; |
| 5404 | 5382 | } else { |
| 5405 | 5383 | len = p - sym; |