From 8030a18eb63d898ee9cbf4296c7f028996f80b96 Mon Sep 17 00:00:00 2001 From: Locria Cyber <74560659+iacore@users.noreply.github.com> Date: Sun, 4 Jun 2023 19:42:43 +0000 Subject: [PATCH 1/3] Add MIR_scan_string_s --- mir.c | 12 ++++++++++-- mir.h | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/mir.c b/mir.c index 20034721b2..dc243e0b40 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,9 +5459,9 @@ 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; + if (ch == '\0') assert(FALSE); // handled in MIR_scan_string with strlen. Shouldn't have \0 any more input_string_char_num++; if (ch == '\n') curr_lno++; return ch; @@ -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, strlen(str)); +} + +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); From fd8117ae3e3ac2a57b075670aec06c8e99a1c34c Mon Sep 17 00:00:00 2001 From: Locria Cyber <74560659+iacore@users.noreply.github.com> Date: Sun, 4 Jun 2023 19:53:20 +0000 Subject: [PATCH 2/3] Patch MIR.md --- MIR.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From a332e96557976899c8d5267351559b2a9bcd5862 Mon Sep 17 00:00:00 2001 From: Locria Cyber <74560659+iacore@users.noreply.github.com> Date: Thu, 30 Nov 2023 19:25:21 +0000 Subject: [PATCH 3/3] apply suggestions --- mir.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mir.c b/mir.c index dc243e0b40..34521d5294 100644 --- a/mir.c +++ b/mir.c @@ -5461,7 +5461,7 @@ 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') assert(FALSE); // handled in MIR_scan_string with strlen. Shouldn't have \0 any more + if (ch == '\0') return EOF; input_string_char_num++; if (ch == '\n') curr_lno++; return ch; @@ -5644,7 +5644,7 @@ 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, strlen(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) {