History

make OpenBSD happy about strcpy and sprintf

Sigrid Solveig Haflínudóttir committed 10 months ago

Commit 9ccc64c1 parent a7542d05 files · +32 −19Browse files

Unified · Split

5 files
  1. conftest.c +9 −4
  2. tccasm.c +3 −2
  3. tccelf.c +7 −5
  4. tccgen.c +7 −4
  5. tccpp.c +6 −4
conftest.c+9−4
@@ -41,9 +41,9 @@ static int isspc(int c) · 40 unmodified lines
4141
4242 int main(int argc, char **argv)
4343 {
44− char l[1000], l2[1000], *p, *q, *p0;
44+ char l[1000], l2[1000], *p, *q, *qe, *p0;
4545 FILE *fp, *op;
46− int c, e, f, s, cmt, cmt_n;
46+ int c, e, f, s, cmt, cmt_n, n;
4747 const char *r;
4848
4949 if (argc < 3)
@@ -75,7 +75,7 @@ int main(int argc, char **argv) · 25 unmodified lines
7575 }
7676
7777 /* count & skip leading spaces */
78− p = l, q = l2, f = 0;
78+ p = l, q = l2, qe = l2+sizeof(l2), f = 0;
7979 while (*p && isspc(*p))
8080 ++p, ++f;
8181
@@ -110,7 +110,12 @@ int main(int argc, char **argv) · 28 unmodified lines
110110 //e = memcmp(p, "defined ", 8) ? 0 : 8;
111111 if (0 == memcmp(p + e, r, c)) {
112112 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);
114119 break;
115120 }
116121
tccasm.c+3−2
@@ -790,9 +790,10 @@ static void asm_parse_directive(TCCState *s1, int global) · 789 unmodified lines
790790 next();
791791 }
792792 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);
794794 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;
796797 use_section(s1, sname);
797798 }
798799 break;
tccelf.c+7−5
@@ -229,10 +229,12 @@ ST_FUNC void tccelf_end_file(TCCState *s1) · 228 unmodified lines
229229 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
230230 {
231231 Section *sec;
232+ int n;
232233
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);
234237 sec->s1 = s1;
235− strcpy(sec->name, name);
236238 sec->sh_type = sh_type;
237239 sec->sh_flags = sh_flags;
238240 switch(sh_type) {
@@ -1401,7 +1403,7 @@ static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type, · 1162 unmodified lines
14011403 if (len > sizeof plt_name - 5)
14021404 len = sizeof plt_name - 5;
14031405 memcpy(plt_name, name, len);
1404− strcpy(plt_name + len, "@plt");
1406+ pstrcpy(plt_name + len, sizeof(plt_name) - len, "@plt");
14051407 attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, 0,
14061408 ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name);
14071409 } else {
@@ -1737,7 +1739,7 @@ static void tcc_tcov_add_file(TCCState *s1, const char *filename) · 329 unmodified lines
17371739 cstr_printf (&cstr, "%s/%s.tcov", wd, filename);
17381740 }
17391741 ptr = section_ptr_add(tcov_section, cstr.size + 1);
1740− strcpy((char *)ptr, cstr.data);
1742+ memcpy(ptr, cstr.data, cstr.size + 1);
17411743 unlink((char *)ptr);
17421744 #ifdef _WIN32
17431745 normalize_slashes((char *)ptr);
@@ -2913,7 +2915,7 @@ static void fill_bsd_note(Section *s, int type, · 1169 unmodified lines
29132915 note->n_namesz = 8;
29142916 note->n_descsz = 4;
29152917 note->n_type = type;
2916− strcpy (ptr + sizeof(ElfW(Nhdr)), value);
2918+ pstrcpy (ptr + sizeof(ElfW(Nhdr)), 8, value);
29172919 memcpy (ptr + sizeof(ElfW(Nhdr)) + 8, &data, 4);
29182920 }
29192921
tccgen.c+7−4
@@ -546,7 +546,8 @@ ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, · 545 unmodified lines
546546 can_add_underscore = 0;
547547 }
548548 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;
550551 name = buf1;
551552 other |= ST_PE_STDCALL;
552553 can_add_underscore = 0;
@@ -5568,7 +5569,8 @@ static void parse_atomic(int atok) · 5015 unmodified lines
55685569 break;
55695570 }
55705571
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;
55725574 vpush_helper_func(tok_alloc_const(buf));
55735575 vrott(arg - save + 1);
55745576 gfunc_call(arg - save);
@@ -8866,8 +8868,9 @@ static int decl(int l) · 3291 unmodified lines
88668868 the compilation unit only if they are used */
88678869 if (sym->type.t & VT_INLINE) {
88688870 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);
88718874 fn->sym = sym;
88728875 dynarray_add(&tcc_state->inline_fns,
88738876 &tcc_state->nb_inline_fns, fn);
tccpp.c+6−4
@@ -543,11 +543,12 @@ ST_FUNC int tok_alloc_const(const char *str) · 542 unmodified lines
543543 /* XXX: float tokens */
544544 ST_FUNC const char *get_tok_str(int v, CValue *cv)
545545 {
546− char *p;
546+ char *p, *e;
547547 int i, len;
548548
549549 cstr_reset(&cstr_buf);
550550 p = cstr_buf.data;
551+ e = p + cstr_buf.size_allocated;
551552
552553 switch(v) {
553554 case TOK_CINT:
@@ -557,7 +558,7 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 3 unmodified lines
557558 case TOK_CLLONG:
558559 case TOK_CULLONG:
559560 /* 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);
561562 break;
562563 case TOK_LCHAR:
563564 cstr_ccat(&cstr_buf, 'L');
@@ -628,7 +629,7 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 64 unmodified lines
628629 q += 3;
629630 }
630631 if (v >= 127 || (v < 32 && !is_space(v) && v != '\n')) {
631− sprintf(p, "<\\x%02x>", v);
632+ snprintf(p, e-p, "<\\x%02x>", v);
632633 break;
633634 }
634635 addv:
@@ -638,13 +639,14 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 3 unmodified lines
638639 return table_ident[v - TOK_IDENT]->str;
639640 } else if (v >= SYM_FIRST_ANOM) {
640641 /* 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);
642643 } else {
643644 /* should never happen */
644645 return NULL;
645646 }
646647 break;
647648 }
649+ e[-1] = '\0';
648650 return cstr_buf.data;
649651 }
650652

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t