diff --git a/MIR.md b/MIR.md index 1f682f491b..3f697e35d2 100644 --- a/MIR.md +++ b/MIR.md @@ -19,7 +19,7 @@ * To start work with MIR program, you should first call API function `MIR_init` * API function `MIR_finish (MIR_context_t ctx)` should be called last. It frees all internal data used to work with MIR program and all IR (insns, functions, items, and modules) created in this context * API function `MIR_output (MIR_context_t ctx, FILE *f)` outputs MIR textual representation of the program into given file - * API function `MIR_scan_string (MIR_context_t ctx, const char *str)` reads textual MIR representation given by a string + * API function `MIR_scan_string_s (MIR_context_t ctx, const char *str, size_t str_len)` reads textual MIR representation given by a string * API functions `MIR_write (MIR_context_t ctx, FILE *f)` and `MIR_read (MIR_context_t ctx, FILE *f)` outputs and reads **binary MIR representation** to/from given file. There are also diff --git a/mir.c b/mir.c index 20034721b2..34521d5294 100644 --- a/mir.c +++ b/mir.c @@ -5267,6 +5267,7 @@ struct scan_ctx { HTAB (insn_name_t) * insn_name_tab; const char *input_string; size_t input_string_char_num; + size_t input_string_char_limit; VARR (label_name_t) * label_names; HTAB (label_desc_t) * label_desc_tab; }; @@ -5280,6 +5281,7 @@ struct scan_ctx { #define insn_name_tab ctx->scan_ctx->insn_name_tab #define input_string ctx->scan_ctx->input_string #define input_string_char_num ctx->scan_ctx->input_string_char_num +#define input_string_char_limit ctx->scan_ctx->input_string_char_limit #define label_names ctx->scan_ctx->label_names #define label_desc_tab ctx->scan_ctx->label_desc_tab @@ -5457,8 +5459,8 @@ static void scan_string (MIR_context_t ctx, token_t *t, int c, int get_char (MIR } static int get_string_char (MIR_context_t ctx) { + if (input_string_char_num >= input_string_char_limit) return EOF; int ch = input_string[input_string_char_num]; - if (ch == '\0') return EOF; input_string_char_num++; if (ch == '\n') curr_lno++; @@ -5640,7 +5642,12 @@ static MIR_type_t str2type (const char *type_name) { */ +void MIR_scan_string_s (MIR_context_t ctx, const char *str, size_t str_len); void MIR_scan_string (MIR_context_t ctx, const char *str) { + MIR_scan_string_s(ctx, str, SIZE_MAX); +} + +void MIR_scan_string_s (MIR_context_t ctx, const char *str, size_t str_len) { token_t t; const char *name; MIR_module_t module = NULL; @@ -5660,6 +5667,7 @@ void MIR_scan_string (MIR_context_t ctx, const char *str) { curr_lno = 1; input_string = str; input_string_char_num = 0; + input_string_char_limit = str_len; t.code = TC_NL; for (;;) { if (setjmp (error_jmp_buf)) { diff --git a/mir.h b/mir.h index 820397b06a..d806d199bb 100644 --- a/mir.h +++ b/mir.h @@ -581,7 +581,9 @@ extern void MIR_read_with_func (MIR_context_t ctx, int (*const reader_func) (MIR #endif #if !MIR_NO_SCAN +/** deprecated: use MIR_scan_string_s */ extern void MIR_scan_string (MIR_context_t ctx, const char *str); +extern void MIR_scan_string_s (MIR_context_t ctx, const char *str, size_t str_len); #endif extern MIR_item_t MIR_get_global_item (MIR_context_t ctx, const char *name);