Unified · Split
| @@ -41,9 +41,9 @@ static int isspc(int c) · 40 unmodified lines | ||
| 41 | 41 | |
| 42 | 42 | int main(int argc, char **argv) |
| 43 | 43 | { |
| 44 | − char l[1000], l2[1000], *p, *q, *p0; | |
| 44 | + char l[1000], l2[1000], *p, *q, *qe, *p0; | |
| 45 | 45 | FILE *fp, *op; |
| 46 | − int c, e, f, s, cmt, cmt_n; | |
| 46 | + int c, e, f, s, cmt, cmt_n, n; | |
| 47 | 47 | const char *r; |
| 48 | 48 | |
| 49 | 49 | if (argc < 3) |
| @@ -75,7 +75,7 @@ int main(int argc, char **argv) · 25 unmodified lines | ||
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | /* count & skip leading spaces */ |
| 78 | − p = l, q = l2, f = 0; | |
| 78 | + p = l, q = l2, qe = l2+sizeof(l2), f = 0; | |
| 79 | 79 | while (*p && isspc(*p)) |
| 80 | 80 | ++p, ++f; |
| 81 | 81 | |
| @@ -110,7 +110,12 @@ int main(int argc, char **argv) · 28 unmodified lines | ||
| 110 | 110 | //e = memcmp(p, "defined ", 8) ? 0 : 8; |
| 111 | 111 | if (0 == memcmp(p + e, r, c)) { |
| 112 | 112 | p += e + c; |
| 113 | − q = strchr(strcpy(q, platform_macros[f + 1]), 0); | |
| 113 | + n = qe - q; | |
| 114 | + if (snprintf(q, n, "%s", platform_macros[f + 1]) >= n) { | |
| 115 | + fprintf(stderr, "string too big\n"); | |
| 116 | + return 1; | |
| 117 | + } | |
| 118 | + q = strchr(q, 0); | |
| 114 | 119 | break; |
| 115 | 120 | } |
| 116 | 121 | |
| @@ -790,9 +790,10 @@ static void asm_parse_directive(TCCState *s1, int global) · 789 unmodified lines | ||
| 790 | 790 | next(); |
| 791 | 791 | } |
| 792 | 792 | if (n) |
| 793 | − sprintf(sname, "%s%d", get_tok_str(tok1, NULL), n); | |
| 793 | + snprintf(sname, sizeof(sname), "%s%d", get_tok_str(tok1, NULL), n); | |
| 794 | 794 | else |
| 795 | − sprintf(sname, "%s", get_tok_str(tok1, NULL)); | |
| 795 | + snprintf(sname, sizeof(sname), "%s", get_tok_str(tok1, NULL)); | |
| 796 | + sname[sizeof(sname)-1] = 0; | |
| 796 | 797 | use_section(s1, sname); |
| 797 | 798 | } |
| 798 | 799 | break; |
| @@ -229,10 +229,12 @@ ST_FUNC void tccelf_end_file(TCCState *s1) · 228 unmodified lines | ||
| 229 | 229 | ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags) |
| 230 | 230 | { |
| 231 | 231 | Section *sec; |
| 232 | + int n; | |
| 232 | 233 | |
| 233 | − sec = tcc_mallocz(sizeof(Section) + strlen(name)); | |
| 234 | + n = strlen(name); // Section->name already has 1 extra char | |
| 235 | + sec = tcc_mallocz(sizeof(Section) + n); | |
| 236 | + memcpy(sec->name, name, n + 1); | |
| 234 | 237 | sec->s1 = s1; |
| 235 | − strcpy(sec->name, name); | |
| 236 | 238 | sec->sh_type = sh_type; |
| 237 | 239 | sec->sh_flags = sh_flags; |
| 238 | 240 | switch(sh_type) { |
| @@ -1401,7 +1403,7 @@ static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type, · 1162 unmodified lines | ||
| 1401 | 1403 | if (len > sizeof plt_name - 5) |
| 1402 | 1404 | len = sizeof plt_name - 5; |
| 1403 | 1405 | memcpy(plt_name, name, len); |
| 1404 | − strcpy(plt_name + len, "@plt"); | |
| 1406 | + pstrcpy(plt_name + len, sizeof(plt_name) - len, "@plt"); | |
| 1405 | 1407 | attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, 0, |
| 1406 | 1408 | ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name); |
| 1407 | 1409 | } else { |
| @@ -1737,7 +1739,7 @@ static void tcc_tcov_add_file(TCCState *s1, const char *filename) · 329 unmodified lines | ||
| 1737 | 1739 | cstr_printf (&cstr, "%s/%s.tcov", wd, filename); |
| 1738 | 1740 | } |
| 1739 | 1741 | ptr = section_ptr_add(tcov_section, cstr.size + 1); |
| 1740 | − strcpy((char *)ptr, cstr.data); | |
| 1742 | + memcpy(ptr, cstr.data, cstr.size + 1); | |
| 1741 | 1743 | unlink((char *)ptr); |
| 1742 | 1744 | #ifdef _WIN32 |
| 1743 | 1745 | normalize_slashes((char *)ptr); |
| @@ -2913,7 +2915,7 @@ static void fill_bsd_note(Section *s, int type, · 1169 unmodified lines | ||
| 2913 | 2915 | note->n_namesz = 8; |
| 2914 | 2916 | note->n_descsz = 4; |
| 2915 | 2917 | note->n_type = type; |
| 2916 | − strcpy (ptr + sizeof(ElfW(Nhdr)), value); | |
| 2918 | + pstrcpy (ptr + sizeof(ElfW(Nhdr)), 8, value); | |
| 2917 | 2919 | memcpy (ptr + sizeof(ElfW(Nhdr)) + 8, &data, 4); |
| 2918 | 2920 | } |
| 2919 | 2921 | |
| @@ -546,7 +546,8 @@ ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, · 545 unmodified lines | ||
| 546 | 546 | can_add_underscore = 0; |
| 547 | 547 | } |
| 548 | 548 | if (ref->f.func_call == FUNC_STDCALL && can_add_underscore) { |
| 549 | − sprintf(buf1, "_%s@%d", name, ref->f.func_args * PTR_SIZE); | |
| 549 | + snprintf(buf1, sizeof(buf1), "_%s@%d", name, ref->f.func_args * PTR_SIZE); | |
| 550 | + buf1[sizeof(buf1)-1] = 0; | |
| 550 | 551 | name = buf1; |
| 551 | 552 | other |= ST_PE_STDCALL; |
| 552 | 553 | can_add_underscore = 0; |
| @@ -5568,7 +5569,8 @@ static void parse_atomic(int atok) · 5015 unmodified lines | ||
| 5568 | 5569 | break; |
| 5569 | 5570 | } |
| 5570 | 5571 | |
| 5571 | − sprintf(buf, "%s_%d", get_tok_str(atok, 0), size); | |
| 5572 | + snprintf(buf, sizeof(buf), "%s_%d", get_tok_str(atok, 0), size); | |
| 5573 | + buf[sizeof(buf)-1] = 0; | |
| 5572 | 5574 | vpush_helper_func(tok_alloc_const(buf)); |
| 5573 | 5575 | vrott(arg - save + 1); |
| 5574 | 5576 | gfunc_call(arg - save); |
| @@ -8866,8 +8868,9 @@ static int decl(int l) · 3291 unmodified lines | ||
| 8866 | 8868 | the compilation unit only if they are used */ |
| 8867 | 8869 | if (sym->type.t & VT_INLINE) { |
| 8868 | 8870 | struct InlineFunc *fn; |
| 8869 | − fn = tcc_malloc(sizeof *fn + strlen(file->filename)); | |
| 8870 | − strcpy(fn->filename, file->filename); | |
| 8871 | + int n = strlen(file->filename); | |
| 8872 | + fn = tcc_malloc(sizeof *fn + n); | |
| 8873 | + pstrcpy(fn->filename, n+1, file->filename); | |
| 8871 | 8874 | fn->sym = sym; |
| 8872 | 8875 | dynarray_add(&tcc_state->inline_fns, |
| 8873 | 8876 | &tcc_state->nb_inline_fns, fn); |
| @@ -543,11 +543,12 @@ ST_FUNC int tok_alloc_const(const char *str) · 542 unmodified lines | ||
| 543 | 543 | /* XXX: float tokens */ |
| 544 | 544 | ST_FUNC const char *get_tok_str(int v, CValue *cv) |
| 545 | 545 | { |
| 546 | − char *p; | |
| 546 | + char *p, *e; | |
| 547 | 547 | int i, len; |
| 548 | 548 | |
| 549 | 549 | cstr_reset(&cstr_buf); |
| 550 | 550 | p = cstr_buf.data; |
| 551 | + e = p + cstr_buf.size_allocated; | |
| 551 | 552 | |
| 552 | 553 | switch(v) { |
| 553 | 554 | case TOK_CINT: |
| @@ -557,7 +558,7 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 3 unmodified lines | ||
| 557 | 558 | case TOK_CLLONG: |
| 558 | 559 | case TOK_CULLONG: |
| 559 | 560 | /* XXX: not quite exact, but only useful for testing */ |
| 560 | − sprintf(p, "%llu", (unsigned long long)cv->i); | |
| 561 | + snprintf(p, e-p, "%llu", (unsigned long long)cv->i); | |
| 561 | 562 | break; |
| 562 | 563 | case TOK_LCHAR: |
| 563 | 564 | cstr_ccat(&cstr_buf, 'L'); |
| @@ -628,7 +629,7 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 64 unmodified lines | ||
| 628 | 629 | q += 3; |
| 629 | 630 | } |
| 630 | 631 | if (v >= 127 || (v < 32 && !is_space(v) && v != '\n')) { |
| 631 | − sprintf(p, "<\\x%02x>", v); | |
| 632 | + snprintf(p, e-p, "<\\x%02x>", v); | |
| 632 | 633 | break; |
| 633 | 634 | } |
| 634 | 635 | addv: |
| @@ -638,13 +639,14 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 3 unmodified lines | ||
| 638 | 639 | return table_ident[v - TOK_IDENT]->str; |
| 639 | 640 | } else if (v >= SYM_FIRST_ANOM) { |
| 640 | 641 | /* special name for anonymous symbol */ |
| 641 | − sprintf(p, "L.%u", v - SYM_FIRST_ANOM); | |
| 642 | + snprintf(p, e-p, "L.%u", v - SYM_FIRST_ANOM); | |
| 642 | 643 | } else { |
| 643 | 644 | /* should never happen */ |
| 644 | 645 | return NULL; |
| 645 | 646 | } |
| 646 | 647 | break; |
| 647 | 648 | } |
| 649 | + e[-1] = '\0'; | |
| 648 | 650 | return cstr_buf.data; |
| 649 | 651 | } |
| 650 | 652 | |