diff options
author | Ben Smith <binjimin@gmail.com> | 2017-03-13 16:32:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-13 16:32:11 -0700 |
commit | a02beb8e12b21f63dcfbf862366d3e44f4eb62d3 (patch) | |
tree | 64833025e93a154d7166dfbcddf04fcfea85ce03 /src/binary-reader.cc | |
parent | a17662511d4e7c277f5add3b61453e7b708b2756 (diff) | |
download | wabt-a02beb8e12b21f63dcfbf862366d3e44f4eb62d3.tar.gz wabt-a02beb8e12b21f63dcfbf862366d3e44f4eb62d3.tar.bz2 wabt-a02beb8e12b21f63dcfbf862366d3e44f4eb62d3.zip |
Fix crash using binary reader logging with error (#351)
When the binary logging is turned on, it can't use the
`reader->on_error` directly since that function expects that the
user_data is the BinaryReaderInterpreter context (for example), but it
is actually the LoggingContext.
To make it easier for users to use the default error handler, now they
return a bool saying whether the error was handled.
Some additional changes:
* Add a test that uses logging and succeeds
* Add a test that uses logging and has an error
* Change the `run-*-interp.py` scripts to use `-t` for tracing and `-v`
for logging
Diffstat (limited to 'src/binary-reader.cc')
-rw-r--r-- | src/binary-reader.cc | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/binary-reader.cc b/src/binary-reader.cc index 8237b43a..eda66edc 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -149,11 +149,14 @@ static BinaryReaderContext* get_user_context(Context* ctx) { static void WABT_PRINTF_FORMAT(2, 3) raise_error(Context* ctx, const char* format, ...) { WABT_SNPRINTF_ALLOCA(buffer, length, format); + bool handled = false; if (ctx->reader->on_error) { - ctx->reader->on_error(get_user_context(ctx), buffer); - } else { + handled = ctx->reader->on_error(get_user_context(ctx), buffer); + } + + if (!handled) { /* Not great to just print, but we don't want to eat the error either. */ - fprintf(stderr, "*ERROR*: %s\n", buffer); + fprintf(stderr, "*ERROR*: @0x%08zx: %s\n", ctx->offset, buffer); } longjmp(ctx->error_jmp_buf, 1); } @@ -466,6 +469,17 @@ static void write_indent(LoggingContext* ctx) { LOGF_NOINDENT(__VA_ARGS__); \ } while (0) +static bool logging_on_error(BinaryReaderContext* context, + const char* message) { + LoggingContext* ctx = static_cast<LoggingContext*>(context->user_data); + // Can't use FORWARD_CTX because it returns Result by default. + if (!ctx->reader->on_error) + return false; + BinaryReaderContext new_ctx = *context; + new_ctx.user_data = ctx->reader->user_data; + return ctx->reader->on_error(&new_ctx, message); +} + static Result logging_begin_section(BinaryReaderContext* context, BinarySection section_type, uint32_t size) { @@ -2053,7 +2067,7 @@ Result read_binary(const void* data, WABT_ZERO_MEMORY(logging_reader); logging_reader.user_data = &logging_context; - logging_reader.on_error = reader->on_error; + logging_reader.on_error = logging_on_error; logging_reader.begin_section = logging_begin_section; logging_reader.begin_module = logging_begin_module; logging_reader.end_module = logging_end_module; |