From fe0c6a3df9480cc3d0be7412cd24bba4366c18a1 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 27 Jan 2016 12:06:47 -0700 Subject: [PATCH] fix crash when compiling empty file closes #90 --- src/errmsg.cpp | 7 +++++-- test/run_tests.cpp | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/errmsg.cpp b/src/errmsg.cpp index 8780f33a52..e8ab353efc 100644 --- a/src/errmsg.cpp +++ b/src/errmsg.cpp @@ -80,9 +80,12 @@ ErrorMsg *err_msg_create_with_line(Buf *path, int line, int column, int line_start_offset = line_offsets->at(line); int end_line = line + 1; int line_end_offset = (end_line >= line_offsets->length) ? buf_len(source) : line_offsets->at(line + 1); + int len = line_end_offset - line_start_offset - 1; + if (len < 0) { + len = 0; + } - buf_init_from_mem(&err_msg->line_buf, buf_ptr(source) + line_start_offset, - line_end_offset - line_start_offset - 1); + buf_init_from_mem(&err_msg->line_buf, buf_ptr(source) + line_start_offset, len); return err_msg; } diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 8adc44b5df..5825dba018 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -1680,6 +1680,9 @@ c_import { )SOURCE", 2, ".tmp_source.zig:2:1: error: C import failed", ".h:1:10: error: 'bogus.h' file not found"); + add_compile_fail_case("empty file", "", + 1, ".tmp_source.zig:1:1: error: missing export declaration and output name not provided"); + } static void print_compiler_invocation(TestCase *test_case) {