CertainLach/jrsonnet

jrsonnet-evaluator 0.4.2 `EvaluationState::stringify_err` panics with empty source code

huin opened this issue · 2 comments

huin commented

Thanks for the library - I've found it very powerful.

I've run into the following edge case when evaluating empty files. The following code panics, but it should correctly format the error.

use std::{
    path::{Path, PathBuf},
    rc::Rc,
};

use jrsonnet_evaluator::{EvaluationState, FileImportResolver, ManifestFormat};

fn main() {
    let vm = EvaluationState::default();
    vm.with_stdlib();
    vm.set_manifest_format(ManifestFormat::Json(0));
    vm.set_import_resolver(Box::new(FileImportResolver::default()));

    // println!("Evaluating 0");
    // run(&vm, "0");
    // println!("Evaluating syntax error");
    // run(&vm, "syntax error");
    println!("Evaluating empty");
    run(&vm, "");
}

fn run(vm: &EvaluationState, code: &str) {
    let source: Rc<Path> = PathBuf::new().into();
    let result = vm.evaluate_snippet_raw(source.clone(), code.into());

    match result {
        Ok(v) => eprintln!("  no error: {:?}", v),
        Err(e) => {
            eprintln!("  LocError: {:?}", e);
            eprintln!("  stringify_err:");
            eprintln!("    {}", vm.stringify_err(&e));  // This panics when code == "".
        }
    }
}

Stack trace:

Evaluating empty
  LocError: LocError((ImportSyntaxError { path: "", source_code: "", error: ParseError { location: LineCol { line: 1, column: 1, offset: 0 }, expected: ExpectedSet { expected: {"\"[\"", "\"{\"", "<number>", "\"(\"", "<string>", "<whitespace>", "<identifier>", "<unary op>"} } } }, StackTrace([])))
  stringify_err:
thread 'main' panicked at 'attempt to subtract with overflow', /home/huin/.cargo/registry/src/github.com-1ecc6299db9ec823/jrsonnet-evaluator-0.4.2/src/trace/mod.rs:101:26
stack backtrace:
   0: rust_begin_unwind
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:142:14
   2: core::panicking::panic
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:48:5
   3: <jrsonnet_evaluator::trace::CompactFormat as jrsonnet_evaluator::trace::TraceFormat>::write_trace
             at /home/huin/.cargo/registry/src/github.com-1ecc6299db9ec823/jrsonnet-evaluator-0.4.2/src/trace/mod.rs:101:14
   4: jrsonnet_evaluator::EvaluationState::stringify_err
             at /home/huin/.cargo/registry/src/github.com-1ecc6299db9ec823/jrsonnet-evaluator-0.4.2/src/lib.rs:328:3
   5: jrsonnet_panic::run
             at ./src/main.rs:31:33
   6: jrsonnet_panic::main
             at ./src/main.rs:19:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/ops/function.rs:248:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

The commented out code (if uncommented) runs without a panic, for comparison.

It looks like the affected line has already been fixed in [df9bc99], so it's just pending a release.

I recommend using not yet released code for development, I think I'll prepare a new release (v0.5.0) after #82 is merged

huin commented

Thanks, that works. I'll stay on 0.4.2 for now as this issue isn't causing me immediate problems.