diff --git a/src/locfile.c b/src/locfile.c index 7e1c394eaf..c3551b84d4 100644 --- a/src/locfile.c +++ b/src/locfile.c @@ -78,8 +78,7 @@ void locfile_locate(struct locfile* l, location loc, const char* fmt, ...) { int startline = locfile_get_line(l, loc.start); int offset = l->linemap[startline]; - int end = MIN(loc.end, l->linemap[startline+1] - 1); - assert(end > loc.start); + int end = MIN(loc.end, MAX(l->linemap[startline+1] - 1, loc.start + 1)); jv underline = jv_string_repeat(jv_string("^"), end - loc.start); jv m2 = jv_string_fmt("%s at %s, line %d, column %d:\n %.*s\n %*s", jv_string_value(m1), jv_string_value(l->fname), diff --git a/tests/shtest b/tests/shtest index 88278a857d..887a6bb4ed 100755 --- a/tests/shtest +++ b/tests/shtest @@ -711,6 +711,20 @@ jq: 3 compile errors EOF diff $d/out $d/expected +# Test syntax error messages when error happens at the end. +if $VALGRIND $Q $JQ -n 'if +' > $d/out 2>&1; then + echo 'syntax error not detected' + exit 1 +fi +cat > $d/expected <, line 1, column 3: + if + ^ +jq: 1 compile error +EOF +diff $d/out $d/expected + # Comments! if ! x=$($JQ -n '123 # comment') || [ "$x" != 123 ]; then echo 'comment did not work'