History

CStrings leakless

avoid memory leaks with lost CStrings on stack after errors. tccpp.c: - use/abuse static Cstring tokcstr where possible tccgen.c: - use/abuse static Cstring initstr where possible tcc.h/libtcc.a: - add 'stk_data' array to track memory pointer on stack - add macros stk_push/pop() and cstr_new/free_s() tccasm.c: - use that - use char[16] instead of char* for op.constraint

grischka committed 3 years ago

Commit a0454001 parent 40131b75 files · +118 −123Browse files

Unified · Split

5 files
  1. libtcc.c +7 −1
  2. tcc.h +13 −4
  3. tccasm.c +23 −25
  4. tccgen.c +39 −47
  5. tccpp.c +36 −46
libtcc.c+7−1
@@ -68,6 +68,9 @@ · 67 unmodified lines
6868 /* XXX: get rid of this ASAP (or maybe not) */
6969 ST_DATA struct TCCState *tcc_state;
7070 TCC_SEM(static tcc_compile_sem);
71+/* an array of pointers to memory to be free'd after errors */
72+ST_DATA void** stk_data;
73+ST_DATA int nb_stk_data;
7174
7275 /********************************************************/
7376 #ifdef _WIN32
@@ -609,8 +612,11 @@ static void error1(int mode, const char *fmt, va_list ap) · 535 unmodified lines
609612 cstr_free(&cs);
610613 if (mode != ERROR_WARN)
611614 s1->nb_errors++;
612− if (mode == ERROR_ERROR && s1->error_set_jmp_enabled)
615+ if (mode == ERROR_ERROR && s1->error_set_jmp_enabled) {
616+ while (nb_stk_data)
617+ tcc_free(*(void**)stk_data[--nb_stk_data]);
613618 longjmp(s1->error_jmp_buf, 1);
619+ }
614620 }
615621
616622 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc error_func)
tcc.h+13−4
@@ -495,6 +495,7 @@ typedef struct CString { · 494 unmodified lines
495495 int size; /* size in bytes */
496496 int size_allocated;
497497 void *data; /* either 'char *' or 'nwchar_t *' */
498+ struct CString *prev;
498499 } CString;
499500
500501 /* type definition */
@@ -733,7 +734,7 @@ typedef struct ExprValue { · 232 unmodified lines
733734 #define MAX_ASM_OPERANDS 30
734735 typedef struct ASMOperand {
735736 int id; /* GCC 3 optional identifier (0 if number only supported) */
736− char *constraint;
737+ char constraint[16];
737738 char asm_str[16]; /* computed asm string for operand */
738739 SValue *vt; /* C value of the expression */
739740 int ref_index; /* if >= 0, gives reference to a output constraint */
@@ -1208,6 +1209,8 @@ enum tcc_token { · 468 unmodified lines
12081209 /* ------------ libtcc.c ------------ */
12091210
12101211 ST_DATA struct TCCState *tcc_state;
1212+ST_DATA void** stk_data;
1213+ST_DATA int nb_stk_data;
12111214
12121215 /* public functions currently used by the tcc main function */
12131216 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s);
@@ -1257,11 +1260,17 @@ ST_FUNC void cstr_free(CString *cstr); · 43 unmodified lines
12571260 ST_FUNC int cstr_printf(CString *cs, const char *fmt, ...) PRINTF_LIKE(2,3);
12581261 ST_FUNC int cstr_vprintf(CString *cstr, const char *fmt, va_list ap);
12591262 ST_FUNC void cstr_reset(CString *cstr);
1260−
12611263 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
12621264 ST_FUNC int tcc_open(TCCState *s1, const char *filename);
12631265 ST_FUNC void tcc_close(void);
12641266
1267+/* mark a memory pointer on stack for cleanup after errors */
1268+#define stk_push(p) dynarray_add(&stk_data, &nb_stk_data, p)
1269+#define stk_pop() (--nb_stk_data)
1270+/* mark CString on stack for cleanup errors */
1271+#define cstr_new_s(cstr) (cstr_new(cstr), stk_push(&(cstr)->data))
1272+#define cstr_free_s(cstr) (cstr_free(cstr), stk_pop())
1273+
12651274 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
12661275 /* flags: */
12671276 #define AFF_PRINT_ERROR 0x10 /* print error if file not found */
@@ -1483,8 +1492,8 @@ ST_FUNC int type_size(CType *type, int *a); · 215 unmodified lines
14831492 ST_FUNC void mk_pointer(CType *type);
14841493 ST_FUNC void vstore(void);
14851494 ST_FUNC void inc(int post, int c);
1486−ST_FUNC void parse_mult_str (CString *astr, const char *msg);
1487−ST_FUNC void parse_asm_str(CString *astr);
1495+ST_FUNC CString* parse_mult_str(const char *msg);
1496+ST_FUNC CString* parse_asm_str(void);
14881497 ST_FUNC void indir(void);
14891498 ST_FUNC void unary(void);
14901499 ST_FUNC void gexpr(void);
tccasm.c+23−25
@@ -1064,15 +1064,12 @@ ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, · 1063 unmodified lines
10641064 }
10651065
10661066 static void subst_asm_operands(ASMOperand *operands, int nb_operands,
1067− CString *out_str, CString *in_str)
1067+ CString *out_str, const char *str)
10681068 {
10691069 int c, index, modifier;
1070− const char *str;
10711070 ASMOperand *op;
10721071 SValue sv;
10731072
1074− cstr_new(out_str);
1075− str = in_str->data;
10761073 for(;;) {
10771074 c = *str++;
10781075 if (c == '%') {
@@ -1118,11 +1115,11 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr, · 39 unmodified lines
11181115 {
11191116 ASMOperand *op;
11201117 int nb_operands;
1118+ char* astr;
11211119
11221120 if (tok != ':') {
11231121 nb_operands = *nb_operands_ptr;
11241122 for(;;) {
1125− CString astr;
11261123 if (nb_operands >= MAX_ASM_OPERANDS)
11271124 tcc_error("too many asm operands");
11281125 op = &operands[nb_operands++];
@@ -1135,10 +1132,8 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr, · 6 unmodified lines
11351132 next();
11361133 skip(']');
11371134 }
1138− parse_mult_str(&astr, "string constant");
1139− op->constraint = tcc_malloc(astr.size);
1140− strcpy(op->constraint, astr.data);
1141− cstr_free(&astr);
1135+ astr = parse_mult_str("string constant")->data;
1136+ pstrcpy(op->constraint, sizeof op->constraint, astr);
11421137 skip('(');
11431138 gexpr();
11441139 if (is_output) {
@@ -1171,7 +1166,8 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr, · 26 unmodified lines
11711166 /* parse the GCC asm() instruction */
11721167 ST_FUNC void asm_instr(void)
11731168 {
1174− CString astr, astr1;
1169+ CString astr, *astr1;
1170+
11751171 ASMOperand operands[MAX_ASM_OPERANDS];
11761172 int nb_outputs, nb_operands, i, must_subst, out_reg, nb_labels;
11771173 uint8_t clobber_regs[NB_ASM_REGS];
@@ -1183,7 +1179,11 @@ ST_FUNC void asm_instr(void) · 5 unmodified lines
11831179 || tok == TOK_GOTO) {
11841180 next();
11851181 }
1186− parse_asm_str(&astr);
1182+
1183+ astr1 = parse_asm_str();
1184+ cstr_new_s(&astr);
1185+ cstr_cat(&astr, astr1->data, astr1->size);
1186+
11871187 nb_operands = 0;
11881188 nb_outputs = 0;
11891189 nb_labels = 0;
@@ -1273,13 +1273,14 @@ ST_FUNC void asm_instr(void) · 83 unmodified lines
12731273 printf("asm: \"%s\"\n", (char *)astr.data);
12741274 #endif
12751275 if (must_subst) {
1276− subst_asm_operands(operands, nb_operands + nb_labels, &astr1, &astr);
1277− cstr_free(&astr);
1278− } else {
1279− astr1 = astr;
1276+ cstr_reset(astr1);
1277+ cstr_cat(astr1, astr.data, astr.size);
1278+ cstr_reset(&astr);
1279+ subst_asm_operands(operands, nb_operands + nb_labels, &astr, astr1->data);
12801280 }
1281+
12811282 #ifdef ASM_DEBUG
1282− printf("subst_asm: \"%s\"\n", (char *)astr1.data);
1283+ printf("subst_asm: \"%s\"\n", (char *)astr.data);
12831284 #endif
12841285
12851286 /* generate loads */
@@ -1290,7 +1291,8 @@ ST_FUNC void asm_instr(void) · 4 unmodified lines
12901291 bleed out to surrounding code. */
12911292 sec = cur_text_section;
12921293 /* assemble the string with tcc internal assembler */
1293− tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1, 0);
1294+ tcc_assemble_inline(tcc_state, astr.data, astr.size - 1, 0);
1295+ cstr_free_s(&astr);
12941296 if (sec != cur_text_section) {
12951297 tcc_warning("inline asm tries to change current section");
12961298 use_section1(tcc_state, sec);
@@ -1305,23 +1307,20 @@ ST_FUNC void asm_instr(void) · 8 unmodified lines
13051307
13061308 /* free everything */
13071309 for(i=0;i<nb_operands;i++) {
1308− ASMOperand *op;
1309− op = &operands[i];
1310− tcc_free(op->constraint);
13111310 vpop();
13121311 }
1313− cstr_free(&astr1);
1312+
13141313 }
13151314
13161315 ST_FUNC void asm_global_instr(void)
13171316 {
1318− CString astr;
1317+ CString *astr;
13191318 int saved_nocode_wanted = nocode_wanted;
13201319
13211320 /* Global asm blocks are always emitted. */
13221321 nocode_wanted = 0;
13231322 next();
1324− parse_asm_str(&astr);
1323+ astr = parse_asm_str();
13251324 skip(')');
13261325 /* NOTE: we do not eat the ';' so that we can restore the current
13271326 token after the assembler parsing */
@@ -1335,14 +1334,13 @@ ST_FUNC void asm_global_instr(void) · 7 unmodified lines
13351334 ind = cur_text_section->data_offset;
13361335
13371336 /* assemble the string with tcc internal assembler */
1338− tcc_assemble_inline(tcc_state, astr.data, astr.size - 1, 1);
1337+ tcc_assemble_inline(tcc_state, astr->data, astr->size - 1, 1);
13391338
13401339 cur_text_section->data_offset = ind;
13411340
13421341 /* restore the current C token */
13431342 next();
13441343
1345− cstr_free(&astr);
13461344 nocode_wanted = saved_nocode_wanted;
13471345 }
13481346
tccgen.c+39−47
@@ -402,7 +402,6 @@ ST_FUNC int tccgen_compile(TCCState *s1) · 401 unmodified lines
402402 ST_FUNC void tccgen_finish(TCCState *s1)
403403 {
404404 tcc_debug_end(s1); /* just in case of errors: free memory */
405− cstr_free(&initstr);
406405 free_inline_functions(s1);
407406 sym_pop(&global_stack, NULL, 0);
408407 sym_pop(&local_stack, NULL, 0);
@@ -412,6 +411,8 @@ ST_FUNC void tccgen_finish(TCCState *s1) · 3 unmodified lines
412411 dynarray_reset(&sym_pools, &nb_sym_pools);
413412 sym_free_first = NULL;
414413 global_label_stack = local_label_stack = NULL;
414+ cstr_free(&initstr);
415+ dynarray_reset(&stk_data, &nb_stk_data);
415416 }
416417
417418 /* ------------------------------------------------------------------------- */
@@ -3756,18 +3757,19 @@ ST_FUNC void inc(int post, int c) · 3338 unmodified lines
37563757 vpop(); /* if post op, return saved value */
37573758 }
37583759
3759−ST_FUNC void parse_mult_str (CString *astr, const char *msg)
3760+ST_FUNC CString* parse_mult_str (const char *msg)
37603761 {
37613762 /* read the string */
37623763 if (tok != TOK_STR)
37633764 expect(msg);
3764− cstr_new(astr);
3765+ cstr_reset(&initstr);
37653766 while (tok == TOK_STR) {
37663767 /* XXX: add \0 handling too ? */
3767− cstr_cat(astr, tokc.str.data, -1);
3768+ cstr_cat(&initstr, tokc.str.data, -1);
37683769 next();
37693770 }
3770− cstr_ccat(astr, '\0');
3771+ cstr_ccat(&initstr, '\0');
3772+ return &initstr;
37713773 }
37723774
37733775 /* If I is >= 1 and a power of two, returns log2(i)+1.
@@ -3792,7 +3794,7 @@ ST_FUNC int exact_log2p1(int i) · 18 unmodified lines
37923794 static void parse_attribute(AttributeDef *ad)
37933795 {
37943796 int t, n;
3795− CString astr;
3797+ char *astr;
37963798
37973799 redo:
37983800 if (tok != TOK_ATTRIBUTE1 && tok != TOK_ATTRIBUTE2)
@@ -3839,37 +3841,33 @@ redo: · 40 unmodified lines
38393841 case TOK_SECTION1:
38403842 case TOK_SECTION2:
38413843 skip('(');
3842− parse_mult_str(&astr, "section name");
3843− ad->section = find_section(tcc_state, (char *)astr.data);
3844+ astr = parse_mult_str("section name")->data;
3845+ ad->section = find_section(tcc_state, astr);
38443846 skip(')');
3845− cstr_free(&astr);
38463847 break;
38473848 case TOK_ALIAS1:
38483849 case TOK_ALIAS2:
38493850 skip('(');
3850− parse_mult_str(&astr, "alias(\"target\")");
3851− ad->alias_target = /* save string as token, for later */
3852− tok_alloc((char*)astr.data, astr.size-1)->tok;
3851+ astr = parse_mult_str("alias(\"target\")")->data;
3852+ /* save string as token, for later */
3853+ ad->alias_target = tok_alloc_const(astr);
38533854 skip(')');
3854− cstr_free(&astr);
38553855 break;
38563856 case TOK_VISIBILITY1:
38573857 case TOK_VISIBILITY2:
38583858 skip('(');
3859− parse_mult_str(&astr,
3860− "visibility(\"default|hidden|internal|protected\")");
3861− if (!strcmp (astr.data, "default"))
3859+ astr = parse_mult_str("visibility(\"default|hidden|internal|protected\")")->data;
3860+ if (!strcmp (astr, "default"))
38623861 ad->a.visibility = STV_DEFAULT;
3863− else if (!strcmp (astr.data, "hidden"))
3862+ else if (!strcmp (astr, "hidden"))
38643863 ad->a.visibility = STV_HIDDEN;
3865− else if (!strcmp (astr.data, "internal"))
3864+ else if (!strcmp (astr, "internal"))
38663865 ad->a.visibility = STV_INTERNAL;
3867− else if (!strcmp (astr.data, "protected"))
3866+ else if (!strcmp (astr, "protected"))
38683867 ad->a.visibility = STV_PROTECTED;
38693868 else
38703869 expect("visibility(\"default|hidden|internal|protected\")");
38713870 skip(')');
3872− cstr_free(&astr);
38733871 break;
38743872 case TOK_ALIGNED1:
38753873 case TOK_ALIGNED2:
@@ -4818,26 +4816,25 @@ static inline void convert_parameter_type(CType *pt) · 942 unmodified lines
48184816 }
48194817 }
48204818
4821−ST_FUNC void parse_asm_str(CString *astr)
4819+ST_FUNC CString* parse_asm_str(void)
48224820 {
48234821 skip('(');
4824− parse_mult_str(astr, "string constant");
4822+ return parse_mult_str("string constant");
48254823 }
48264824
48274825 /* Parse an asm label and return the token */
48284826 static int asm_label_instr(void)
48294827 {
48304828 int v;
4831− CString astr;
4829+ char *astr;
48324830
48334831 next();
4834− parse_asm_str(&astr);
4832+ astr = parse_asm_str()->data;
48354833 skip(')');
48364834 #ifdef ASM_DEBUG
4837− printf("asm_alias: \"%s\"\n", (char *)astr.data);
4835+ printf("asm_alias: \"%s\"\n", astr);
48384836 #endif
4839− v = tok_alloc(astr.data, astr.size - 1)->tok;
4840− cstr_free(&astr);
4837+ v = tok_alloc_const(astr);
48414838 return v;
48424839 }
48434840
@@ -8357,29 +8354,23 @@ static void free_inline_functions(TCCState *s) · 3513 unmodified lines
83578354 dynarray_reset(&s->inline_fns, &s->nb_inline_fns);
83588355 }
83598356
8360−static void do_Static_assert(void){
8361− CString error_str;
8357+static void do_Static_assert(void)
8358+{
83628359 int c;
8360+ const char *msg;
83638361
83648362 next();
83658363 skip('(');
83668364 c = expr_const();
8367−
8368− if (tok == ')') {
8369− if (!c)
8370− tcc_error("_Static_assert fail");
8371− next();
8372− goto static_assert_out;
8365+ msg = "_Static_assert fail";
8366+ if (tok == ',') {
8367+ next();
8368+ msg = parse_mult_str("string constant")->data;
83738369 }
8374−
8375− skip(',');
8376− parse_mult_str(&error_str, "string constant");
8377− if (c == 0)
8378− tcc_error("%s", (char *)error_str.data);
8379− cstr_free(&error_str);
83808370 skip(')');
8381− static_assert_out:
8382− skip(';');
8371+ if (c == 0)
8372+ tcc_error("%s", msg);
8373+ skip(';');
83838374 }
83848375
83858376 /* 'l' is VT_LOCAL or VT_CONST to define default storage type
@@ -8393,10 +8384,11 @@ static int decl(int l) · 7 unmodified lines
83938384 AttributeDef ad, adbase;
83948385
83958386 while (1) {
8396− if (tok == TOK_STATIC_ASSERT) {
8397− do_Static_assert();
8398− continue;
8399− }
8387+
8388+ if (tok == TOK_STATIC_ASSERT) {
8389+ do_Static_assert();
8390+ continue;
8391+ }
84008392
84018393 oldint = 0;
84028394 if (!parse_btype(&btype, &adbase, l == VT_LOCAL)) {
tccpp.c+36−46
@@ -45,7 +45,6 @@ ST_DATA TokenSym **table_ident; · 44 unmodified lines
4545 static TokenSym *hash_ident[TOK_HASH_SIZE];
4646 static char token_buf[STRING_MAX_SIZE + 1];
4747 static CString cstr_buf;
48−static CString macro_equal_buf;
4948 static TokenString tokstr_buf;
5049 static unsigned char isidnum_table[256 - CH_EOF];
5150 static int pp_debug_tok, pp_debug_symv;
@@ -399,7 +398,6 @@ ST_FUNC void cstr_new(CString *cstr) · 347 unmodified lines
399398 ST_FUNC void cstr_free(CString *cstr)
400399 {
401400 tcc_free(cstr->data);
402− cstr_new(cstr);
403401 }
404402
405403 /* reset string to empty */
@@ -1273,12 +1271,11 @@ static int macro_is_equal(const int *a, const int *b) · 867 unmodified lines
12731271 return 1;
12741272
12751273 while (*a && *b) {
1276− /* first time preallocate macro_equal_buf, next time only reset position to start */
1277− cstr_reset(&macro_equal_buf);
1274+ cstr_reset(&tokcstr);
12781275 TOK_GET(&t, &a, &cv);
1279− cstr_cat(&macro_equal_buf, get_tok_str(t, &cv), 0);
1276+ cstr_cat(&tokcstr, get_tok_str(t, &cv), 0);
12801277 TOK_GET(&t, &b, &cv);
1281− if (strcmp(macro_equal_buf.data, get_tok_str(t, &cv)))
1278+ if (strcmp(tokcstr.data, get_tok_str(t, &cv)))
12821279 return 0;
12831280 }
12841281 return !(*a || *b);
@@ -1348,14 +1345,15 @@ search_cached_include(TCCState *s1, const char *filename, int add); · 63 unmodified lines
13481345 static int parse_include(TCCState *s1, int do_next, int test)
13491346 {
13501347 int c, i;
1351− CString cs;
13521348 char name[1024], buf[1024], *p;
13531349 CachedInclude *e;
13541350
1355− cstr_new(&cs);
13561351 c = skip_spaces();
13571352 if (c == '<' || c == '\"') {
1358− file->buf_ptr = parse_pp_string(file->buf_ptr, c == '<' ? '>' : c, &cs);
1353+ cstr_reset(&tokcstr);
1354+ file->buf_ptr = parse_pp_string(file->buf_ptr, c == '<' ? '>' : c, &tokcstr);
1355+ i = tokcstr.size;
1356+ pstrncpy(name, tokcstr.data, i >= sizeof name ? sizeof name - 1 : i);
13591357 next_nomacro();
13601358 } else {
13611359 /* computed #include : concatenate tokens until result is one of
@@ -1363,24 +1361,22 @@ static int parse_include(TCCState *s1, int do_next, int test) · 1 unmodified line
13631361 parse_flags = PARSE_FLAG_PREPROCESS
13641362 | PARSE_FLAG_LINEFEED
13651363 | (parse_flags & PARSE_FLAG_ASM_FILE);
1364+ name[0] = 0;
13661365 for (;;) {
13671366 next();
1368− p = cs.data, i = cs.size - 1;
1367+ p = name, i = strlen(p) - 1;
13691368 if (i > 0
13701369 && ((p[0] == '"' && p[i] == '"')
13711370 || (p[0] == '<' && p[i] == '>')))
13721371 break;
13731372 if (tok == TOK_LINEFEED)
13741373 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1375− cstr_cat(&cs, get_tok_str(tok, &tokc), -1);
1374+ pstrcat(name, sizeof name, get_tok_str(tok, &tokc));
13761375 }
13771376 c = p[0];
13781377 /* remove '<>|""' */
1379− memmove(p, p + 1, cs.size -= 2);
1378+ memmove(p, p + 1, i - 1), p[i - 1] = 0;
13801379 }
1381− cstr_ccat(&cs, '\0');
1382− pstrcpy(name, sizeof name, cs.data);
1383− cstr_free(&cs);
13841380
13851381 i = do_next ? file->include_next_index : -1;
13861382 for (;;) {
@@ -2934,7 +2930,6 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) · 1547 unmodified lines
29342930 Sym *s;
29352931 CValue cval;
29362932 TokenString str;
2937− CString cstr;
29382933
29392934 tok_str_new(&str);
29402935 t0 = t1 = 0;
@@ -2949,8 +2944,8 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) · 8 unmodified lines
29492944 goto bad_stringy;
29502945 s = sym_find2(args, t);
29512946 if (s) {
2952− cstr_new(&cstr);
2953− cstr_ccat(&cstr, '\"');
2947+ cstr_reset(&tokcstr);
2948+ cstr_ccat(&tokcstr, '\"');
29542949 st = s->d;
29552950 spc = 0;
29562951 while (*st >= 0) {
@@ -2961,24 +2956,23 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) · 4 unmodified lines
29612956 const char *s = get_tok_str(t, &cval);
29622957 while (*s) {
29632958 if (t == TOK_PPSTR && *s != '\'')
2964− add_char(&cstr, *s);
2959+ add_char(&tokcstr, *s);
29652960 else
2966− cstr_ccat(&cstr, *s);
2961+ cstr_ccat(&tokcstr, *s);
29672962 ++s;
29682963 }
29692964 }
29702965 }
2971− cstr.size -= spc;
2972− cstr_ccat(&cstr, '\"');
2973− cstr_ccat(&cstr, '\0');
2966+ tokcstr.size -= spc;
2967+ cstr_ccat(&tokcstr, '\"');
2968+ cstr_ccat(&tokcstr, '\0');
29742969 #ifdef PP_DEBUG
2975− printf("\nstringize: <%s>\n", (char *)cstr.data);
2970+ printf("\nstringize: <%s>\n", (char *)tokcstr.data);
29762971 #endif
29772972 /* add string */
2978− cval.str.size = cstr.size;
2979− cval.str.data = cstr.data;
2973+ cval.str.size = tokcstr.size;
2974+ cval.str.data = tokcstr.data;
29802975 tok_str_add2(&str, TOK_PPSTR, &cval);
2981− cstr_free(&cstr);
29822976 } else {
29832977 bad_stringy:
29842978 expect("macro parameter after '#'");
@@ -3047,19 +3041,19 @@ static char const ab_month_name[12][4] = · 62 unmodified lines
30473041
30483042 static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
30493043 {
3050− CString cstr;
30513044 int n, ret = 1;
30523045
3053− cstr_new(&cstr);
3046+ cstr_reset(&tokcstr);
30543047 if (t1 != TOK_PLCHLDR)
3055− cstr_cat(&cstr, get_tok_str(t1, v1), -1);
3056− n = cstr.size;
3048+ cstr_cat(&tokcstr, get_tok_str(t1, v1), -1);
3049+ n = tokcstr.size;
30573050 if (t2 != TOK_PLCHLDR)
3058− cstr_cat(&cstr, get_tok_str(t2, v2), -1);
3059− cstr_ccat(&cstr, '\0');
3051+ cstr_cat(&tokcstr, get_tok_str(t2, v2), -1);
3052+ cstr_ccat(&tokcstr, '\0');
3053+ //printf("paste <%s>\n", (char*)tokcstr.data);
30603054
3061− tcc_open_bf(tcc_state, ":paste:", cstr.size);
3062− memcpy(file->buffer, cstr.data, cstr.size);
3055+ tcc_open_bf(tcc_state, ":paste:", tokcstr.size);
3056+ memcpy(file->buffer, tokcstr.data, tokcstr.size);
30633057 tok_flags = 0;
30643058 for (;;) {
30653059 next_nomacro1();
@@ -3068,13 +3062,11 @@ static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2) · 2 unmodified lines
30683062 if (is_space(tok))
30693063 continue;
30703064 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid"
3071− " preprocessing token", n, (char *)cstr.data, (char*)cstr.data + n);
3065+ " preprocessing token", n, file->buffer, file->buffer + n);
30723066 ret = 0;
30733067 break;
30743068 }
30753069 tcc_close();
3076− //printf("paste <%s>\n", (char*)cstr.data);
3077− cstr_free(&cstr);
30783070 return ret;
30793071 }
30803072
@@ -3214,7 +3206,6 @@ static int macro_subst_tok( · 133 unmodified lines
32143206 TokenString str;
32153207 char *cstrval;
32163208 CValue cval;
3217− CString cstr;
32183209 char buf[32];
32193210
32203211 /* if symbol is a macro, prepare substitution */
@@ -3245,12 +3236,11 @@ static int macro_subst_tok( · 24 unmodified lines
32453236 add_cstr:
32463237 t1 = TOK_STR;
32473238 add_cstr1:
3248− cstr_new(&cstr);
3249− cstr_cat(&cstr, cstrval, 0);
3250− cval.str.size = cstr.size;
3251− cval.str.data = cstr.data;
3239+ cstr_reset(&tokcstr);
3240+ cstr_cat(&tokcstr, cstrval, 0);
3241+ cval.str.size = tokcstr.size;
3242+ cval.str.data = tokcstr.data;
32523243 tok_str_add2(tok_str, t1, &cval);
3253− cstr_free(&cstr);
32543244 } else if (s->d) {
32553245 int saved_parse_flags = parse_flags;
32563246 int *joined_str = NULL;
@@ -3634,7 +3624,6 @@ static void tcc_predefs(TCCState *s1, CString *cs, int is_asm) · 377 unmodified lines
36343624 ST_FUNC void preprocess_start(TCCState *s1, int filetype)
36353625 {
36363626 int is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP));
3637− CString cstr;
36383627
36393628 tccpp_new(s1);
36403629
@@ -3652,6 +3641,7 @@ ST_FUNC void preprocess_start(TCCState *s1, int filetype) · 11 unmodified lines
36523641 set_idnum('.', is_asm ? IS_ID : 0);
36533642
36543643 if (!(filetype & AFF_TYPE_ASM)) {
3644+ CString cstr;
36553645 cstr_new(&cstr);
36563646 tcc_predefs(s1, &cstr, is_asm);
36573647 if (s1->cmdline_defs.size)
@@ -3710,6 +3700,7 @@ ST_FUNC void tccpp_new(TCCState *s) · 52 unmodified lines
37103700 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
37113701 memset(s->cached_includes_hash, 0, sizeof s->cached_includes_hash);
37123702
3703+ cstr_new(&tokcstr);
37133704 cstr_new(&cstr_buf);
37143705 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
37153706 tok_str_new(&tokstr_buf);
@@ -3755,7 +3746,6 @@ ST_FUNC void tccpp_delete(TCCState *s) · 39 unmodified lines
37553746 /* free static buffers */
37563747 cstr_free(&tokcstr);
37573748 cstr_free(&cstr_buf);
3758− cstr_free(&macro_equal_buf);
37593749 tok_str_free_str(tokstr_buf.str);
37603750
37613751 /* free allocators */

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