Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic when previewing seemingly innocent JavaScript text file #967

Closed
kidonng opened this issue Dec 30, 2024 · 5 comments · Fixed by #968
Closed

Panic when previewing seemingly innocent JavaScript text file #967

kidonng opened this issue Dec 30, 2024 · 5 comments · Fixed by #968
Labels
bug Something isn't working

Comments

@kidonng
Copy link
Contributor

kidonng commented Dec 30, 2024

broot 1.44.3 via Homebrew

$ echo 'export default new Map();' > test.js
$ broot test.js
thread 'main' panicked at /Users/brew/Library/Caches/Homebrew/cargo_cache/registry/src/index.crates.io-6f17d22bba15001f/syntect-no-panic-4.6.1/src/parsing/regex.rs:69:53:
regex string should be pre-tested: InvalidEscape("\\g")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
@kidonng kidonng added the bug Something isn't working label Dec 30, 2024
@Canop
Copy link
Owner

Canop commented Dec 30, 2024

Damn.

This is in syntect again, which I had already forked to patch a previous case of uncatched panic.

I'll have a look.

@Canop
Copy link
Owner

Canop commented Dec 30, 2024

(reference: #485)

Trying to use syntect 5

@Canop
Copy link
Owner

Canop commented Dec 30, 2024

The bug is the same in syntect 5.

It's related to the regex engine used. If, in Cargo.toml, I change

syntect = { version = "5.2", default-features = false, features = ["default-fancy"] }

to

syntect = { version = "5.2" } 

then the crash disappears.

But the reason I use this feature was to avoid the onig crate: #956

(see https://github.com/trishume/syntect?tab=readme-ov-file#pure-rust-fancy-regex-mode-without-onig)

@Canop
Copy link
Owner

Canop commented Dec 30, 2024

I can reproduce the same error with bat (which also uses syntect) when using the regex-fancy feature with

cargo run --no-default-features --features "clap,etcetera,paging,wild,regex-fancy" -- ../broot/trav/test.js

image

Canop added a commit to Canop/syntect that referenced this issue Dec 30, 2024
Canop added a commit that referenced this issue Dec 30, 2024
to have non compiling regexes catched

Fix #967
@Canop Canop closed this as completed in #968 Jan 1, 2025
@Canop Canop closed this as completed in e516208 Jan 1, 2025
@Canop
Copy link
Owner

Canop commented Jan 1, 2025

Summary, for anybody who would want to understand:

  • broot uses syntect for syntax coloring, with syntaxes coming from sublime (the largest and best managed set of syntaxes to my knowledge)
  • a key element in syntaxes is regexes, which are lazily compiled on need by syntect
  • syntect panics on a invalid syntax (eg a regex not compiling), on the reasonable basis that a syntax should be tested before being embedded
  • syntect lets you choose between 2 regex engines (onig, in C) and fancy-regex (pure rust)
  • broot uses fancy-regex because onig is badly maintained and doesn't compile on gcc 15
  • some sublime syntaxes use regex features which aren't supported by fancy-regex
  • so syntect crashes on trying to use a regex

The current solution is to use a patched fork of syntect. This fork doesn't panic on compiling regexes. It doesn't apply the failing rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants