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

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