fix(lexer): 在token拷贝函数中添加空指针检查
添加了对源指针的空值断言检查,防止在scc_lexer_tok_copy函数中 传入空指针导致崩溃 refactor(pproc): 优化数组到环形缓冲区转换函数的参数命名 修改scc_lexer_array_to_ring函数参数名从array改为move_array, 并在转换后初始化原数组结构,确保资源正确管理 fix(pproc): 修复宏处理中的内存泄漏问题 - 在fill_replacements函数中初始化token结构并正确释放 - 在scc_pproc_parse_function_macro中释放args向量 - 修改token消费逻辑以避免重复消费 - 在指令处理中添加token释放操作 fix(pproc): 改进宏展开过程中的资源管理 - 修复concatenate_tokens函数中的潜在内存泄漏 - 添加对输出向量的断言检查 - 正确释放输入环形缓冲区 - 重构参数拆分逻辑以避免内存泄漏 refactor(pproc): 优化条件解析和宏表设置逻辑 - 修正parse_constant_condition函数中的返回值初始化 - 在宏表设置时处理重复宏名称的情况 - 改进预处理器的整体资源清理流程 fix(pproc): 修复预处理器中的多个内存泄漏 - 在测试代码中添加正确的资源释放 - 修正格式化字符串中的类型匹配 - 优化环形缓冲区初始化逻辑以处理零容量情况
This commit is contained in:
@@ -59,19 +59,28 @@ static scc_lexer_tok_t concatenate_tokens(const scc_lexer_tok_t *left,
|
||||
scc_lexer_init(&lexer, scc_sstream_to_ring(&sstream));
|
||||
scc_lexer_tok_ring_t *ring = scc_lexer_to_ring(&lexer, 8, true);
|
||||
|
||||
scc_lexer_tok_t result;
|
||||
scc_lexer_tok_t result = {0};
|
||||
int ok;
|
||||
scc_ring_next_consume(*ring, result, ok);
|
||||
if (!ok) {
|
||||
result.type = SCC_TOK_EOF;
|
||||
return result;
|
||||
goto RETURN; // FIXME maybe memleak
|
||||
}
|
||||
scc_ring_next_consume(*ring, result, ok);
|
||||
if (ok) {
|
||||
scc_lexer_tok_drop(&result);
|
||||
return result;
|
||||
goto RETURN; // FIXME maybe memleak
|
||||
}
|
||||
|
||||
scc_lexer_tok_t dummy;
|
||||
RETURN:
|
||||
while (1) {
|
||||
scc_ring_next_consume(*ring, dummy, ok);
|
||||
if (ok == false) {
|
||||
break;
|
||||
}
|
||||
scc_lexer_tok_drop(&dummy);
|
||||
}
|
||||
scc_lexer_drop_ring(ring);
|
||||
scc_lexer_drop(&lexer);
|
||||
scc_sstream_drop(&sstream);
|
||||
@@ -122,6 +131,7 @@ void scc_pproc_expand_by_src(scc_pproc_macro_table_t *macro_table,
|
||||
|
||||
scc_lexer_tok_vec_t output_vec;
|
||||
scc_pproc_expand_by_vec(macro_table, &expaned_buffer, &output_vec, false);
|
||||
Assert(output->cap == 0 && output->data == null); // FIXME hack it
|
||||
*output = scc_lexer_array_to_ring(&output_vec);
|
||||
}
|
||||
|
||||
@@ -142,6 +152,7 @@ void scc_pproc_expand_by_vec(scc_pproc_macro_table_t *macro_table,
|
||||
scc_pproc_marco_table_init(ctx.expanded_set);
|
||||
scc_pproc_expand_macro(&ctx);
|
||||
*output = ctx.output;
|
||||
scc_ring_free(*ctx.input);
|
||||
scc_pproc_macro_table_drop(ctx.expanded_set);
|
||||
}
|
||||
|
||||
@@ -169,8 +180,8 @@ static cbool need_skip(scc_pproc_expand_t *expand_ctx,
|
||||
static inline void
|
||||
split_arguments(scc_pproc_macro_extened_params_t *splited_params,
|
||||
scc_lexer_tok_vec_t *raw_args, const scc_pproc_macro_t *macro) {
|
||||
scc_lexer_tok_vec_t arg;
|
||||
scc_vec_init(arg);
|
||||
scc_lexer_tok_vec_t args;
|
||||
scc_vec_init(args);
|
||||
|
||||
int named_count = (int)scc_vec_size(macro->params);
|
||||
cbool is_variadic =
|
||||
@@ -189,31 +200,35 @@ split_arguments(scc_pproc_macro_extened_params_t *splited_params,
|
||||
if (depth != 0 || raw_arg->type != SCC_TOK_COMMA ||
|
||||
(is_variadic &&
|
||||
(int)scc_vec_size(*splited_params) == named_count - 1)) {
|
||||
if (scc_vec_size(arg) == 0 && raw_arg->type == SCC_TOK_BLANK) {
|
||||
if (scc_vec_size(args) == 0 && raw_arg->type == SCC_TOK_BLANK) {
|
||||
scc_lexer_tok_drop(raw_arg);
|
||||
} else {
|
||||
scc_vec_push(arg, *raw_arg);
|
||||
scc_lexer_tok_t arg;
|
||||
scc_lexer_tok_move(&arg, raw_arg);
|
||||
scc_vec_push(args, arg);
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
scc_lexer_tok_drop(raw_arg);
|
||||
if (scc_vec_size(arg) &&
|
||||
scc_vec_at(arg, scc_vec_size(arg) - 1).type == SCC_TOK_BLANK) {
|
||||
scc_lexer_tok_drop(&scc_vec_pop(arg));
|
||||
if (scc_vec_size(args) &&
|
||||
scc_vec_at(args, scc_vec_size(args) - 1).type ==
|
||||
SCC_TOK_BLANK) {
|
||||
scc_lexer_tok_drop(&scc_vec_pop(args));
|
||||
}
|
||||
scc_vec_push(*splited_params, arg);
|
||||
scc_vec_init(arg);
|
||||
scc_vec_push(*splited_params, args);
|
||||
scc_vec_init(args);
|
||||
}
|
||||
}
|
||||
if (scc_vec_size(arg) &&
|
||||
scc_vec_at(arg, scc_vec_size(arg) - 1).type == SCC_TOK_BLANK) {
|
||||
scc_lexer_tok_drop(&scc_vec_pop(arg));
|
||||
scc_vec_free(*raw_args);
|
||||
|
||||
if (scc_vec_size(args) &&
|
||||
scc_vec_at(args, scc_vec_size(args) - 1).type == SCC_TOK_BLANK) {
|
||||
scc_lexer_tok_drop(&scc_vec_pop(args));
|
||||
}
|
||||
|
||||
scc_vec_push(*splited_params, arg);
|
||||
scc_vec_push(*splited_params, args);
|
||||
if (is_variadic && (int)scc_vec_size(*splited_params) == named_count - 1) {
|
||||
scc_vec_init(arg);
|
||||
scc_vec_push(*splited_params, arg);
|
||||
scc_vec_init(args);
|
||||
scc_vec_push(*splited_params, args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,26 +279,25 @@ static void rescan(scc_pproc_expand_t *expand_ctx,
|
||||
scc_pproc_expand_macro(&rescan_ctx);
|
||||
enable(expand_ctx, macro);
|
||||
|
||||
scc_ring_free(ring);
|
||||
scc_vec_foreach(rescan_ctx.output, i) {
|
||||
scc_vec_push(expand_ctx->output, scc_vec_at(rescan_ctx.output, i));
|
||||
}
|
||||
|
||||
if (scc_vec_size(expand_ctx->output) == 0) {
|
||||
return;
|
||||
goto RETURN;
|
||||
}
|
||||
|
||||
scc_lexer_tok_t *end_tok =
|
||||
&scc_vec_at(expand_ctx->output, scc_vec_size(expand_ctx->output) - 1);
|
||||
|
||||
if (scc_get_tok_subtype(end_tok->type) != SCC_TOK_SUBTYPE_IDENTIFIER) {
|
||||
return;
|
||||
goto RETURN;
|
||||
}
|
||||
|
||||
scc_pproc_macro_t *end_macro =
|
||||
scc_pproc_macro_table_get(expand_ctx->macro_table, &end_tok->lexeme);
|
||||
if (end_macro == null || end_macro->type != SCC_PP_MACRO_FUNCTION) {
|
||||
return;
|
||||
goto RETURN;
|
||||
}
|
||||
|
||||
int ok = false;
|
||||
@@ -301,7 +315,12 @@ static void rescan(scc_pproc_expand_t *expand_ctx,
|
||||
scc_vec_foreach(output, i) {
|
||||
scc_vec_push(expand_ctx->output, scc_vec_at(output, i));
|
||||
}
|
||||
scc_vec_free(output);
|
||||
}
|
||||
|
||||
RETURN:
|
||||
scc_ring_free(*rescan_ctx.input);
|
||||
scc_vec_free(rescan_ctx.output);
|
||||
}
|
||||
|
||||
static int find_params(const scc_lexer_tok_t *tok,
|
||||
@@ -378,6 +397,7 @@ static inline void expand_function_macro(scc_pproc_expand_t *expand_ctx,
|
||||
scc_pproc_macro_extened_params_t splited_params;
|
||||
scc_vec_init(splited_params);
|
||||
split_arguments(&splited_params, &raw_args, macro);
|
||||
Assert(raw_args.cap == 0); // FIXME
|
||||
Assert(scc_vec_size(splited_params) >= scc_vec_size(macro->params));
|
||||
|
||||
scc_pproc_macro_extened_params_t expanded_params;
|
||||
@@ -428,11 +448,15 @@ static inline void expand_function_macro(scc_pproc_expand_t *expand_ctx,
|
||||
|
||||
int idx = find_params(right_tok, macro);
|
||||
scc_lexer_tok_vec_t right_vec;
|
||||
scc_vec_init(right_vec);
|
||||
if (idx != -1) {
|
||||
Assert(idx < (int)scc_vec_size(splited_params));
|
||||
right_vec = scc_vec_at(splited_params, idx);
|
||||
scc_lexer_tok_vec_t *tok_vec = &scc_vec_at(splited_params, idx);
|
||||
scc_vec_foreach(*tok_vec, j) {
|
||||
scc_vec_push(right_vec,
|
||||
scc_lexer_tok_copy(&scc_vec_at(*tok_vec, j)));
|
||||
}
|
||||
} else {
|
||||
scc_vec_init(right_vec);
|
||||
scc_vec_push(right_vec, scc_lexer_tok_copy(right_tok));
|
||||
}
|
||||
|
||||
@@ -451,14 +475,18 @@ static inline void expand_function_macro(scc_pproc_expand_t *expand_ctx,
|
||||
concact(&tok_buffer, right, false);
|
||||
}
|
||||
|
||||
scc_vec_foreach(right_vec, k) {
|
||||
if (k == 0) {
|
||||
scc_vec_foreach(right_vec, j) {
|
||||
if (j == 0) {
|
||||
scc_lexer_tok_drop(&scc_vec_at(right_vec, j));
|
||||
continue;
|
||||
}
|
||||
scc_lexer_tok_t tok =
|
||||
scc_lexer_tok_copy(&scc_vec_at(right_vec, k));
|
||||
scc_lexer_tok_t tok = {0};
|
||||
scc_lexer_tok_move(&tok, &scc_vec_at(right_vec, j));
|
||||
scc_lexer_tok_drop(&scc_vec_at(right_vec, j));
|
||||
scc_vec_push(tok_buffer, tok);
|
||||
}
|
||||
scc_vec_free(right_vec);
|
||||
|
||||
i = right_idx;
|
||||
continue;
|
||||
} else {
|
||||
@@ -481,6 +509,7 @@ static inline void expand_function_macro(scc_pproc_expand_t *expand_ctx,
|
||||
expanded_params_free(&expanded_params);
|
||||
|
||||
rescan(expand_ctx, macro, &tok_buffer);
|
||||
Assert(tok_buffer.cap == 0); // FIXME
|
||||
}
|
||||
|
||||
static inline void expand_object_macro(scc_pproc_expand_t *expand_ctx,
|
||||
@@ -492,10 +521,12 @@ static inline void expand_object_macro(scc_pproc_expand_t *expand_ctx,
|
||||
scc_lexer_tok_t tok =
|
||||
scc_lexer_tok_copy(&scc_vec_at(macro->replaces, i));
|
||||
if (tok.type == SCC_TOK_BLANK) {
|
||||
// FIXME using function to warpper it
|
||||
scc_cstring_free(&tok.lexeme);
|
||||
tok.lexeme = scc_cstring_from_cstr(" ");
|
||||
} else if (tok.type == SCC_TOK_SHARP_SHARP) {
|
||||
// ## contact
|
||||
scc_lexer_tok_drop(&tok);
|
||||
int right_idx = got_right_non_blank(i, ¯o->replaces);
|
||||
|
||||
scc_lexer_tok_t *right;
|
||||
@@ -570,7 +601,7 @@ void scc_pproc_expand_macro(scc_pproc_expand_t *expand_ctx) {
|
||||
if (!ok) {
|
||||
return;
|
||||
}
|
||||
if (tok.type != SCC_TOK_IDENT) {
|
||||
if (scc_get_tok_subtype(tok.type) != SCC_TOK_SUBTYPE_IDENTIFIER) {
|
||||
scc_vec_push(expand_ctx->output, tok);
|
||||
continue;
|
||||
}
|
||||
@@ -591,7 +622,6 @@ void scc_pproc_expand_macro(scc_pproc_expand_t *expand_ctx) {
|
||||
scc_pproc_macro_table_get(expand_ctx->macro_table, &tok.lexeme);
|
||||
|
||||
if (macro == null || need_skip(expand_ctx, macro)) {
|
||||
Assert(tok.type == SCC_TOK_IDENT);
|
||||
// FIXME maybe keyword is error or don't parse c keyword or number
|
||||
// 这个地方不太清楚正确的原因
|
||||
tok.type = SCC_TOK_DISABLED;
|
||||
@@ -601,6 +631,7 @@ void scc_pproc_expand_macro(scc_pproc_expand_t *expand_ctx) {
|
||||
expand_ctx->need_rescan = true;
|
||||
|
||||
if (macro->type == SCC_PP_MACRO_OBJECT) {
|
||||
scc_lexer_tok_drop(&tok);
|
||||
expand_object_macro(expand_ctx, macro);
|
||||
} else if (macro->type == SCC_PP_MACRO_FUNCTION) {
|
||||
scc_lexer_tok_t expect_tok;
|
||||
@@ -610,6 +641,7 @@ void scc_pproc_expand_macro(scc_pproc_expand_t *expand_ctx) {
|
||||
continue;
|
||||
}
|
||||
|
||||
scc_lexer_tok_drop(&tok);
|
||||
expand_function_macro(expand_ctx, macro);
|
||||
} else {
|
||||
UNREACHABLE();
|
||||
|
||||
Reference in New Issue
Block a user