In highlightEmptyRegion(), if no rules are defined, return

immediately.

The following snippet will trigger SIGSEGV otherwise:
```go
    syntaxFile, _ := ioutil.ReadFile("/non/existing/path"  + "yml.yaml")
    syntaxDef, err := highlight.ParseDef(syntaxFile)
    if err != nil {
        return err
    }
    h := highlight.NewHighlighter(syntaxDef)
    matches := h.HighlightString(string(codeBlock.Literal))
```

```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x574656]

goroutine 1 [running]:
github.com/zyedidia/highlight.(*Highlighter).highlightEmptyRegion(0xc000129b98, 0xc0000db980?, 0x0, 0x1, 0x0?, {0xc0001880b0, 0x2a, 0x2c}, 0x0)
	/home/jesse/go/pkg/mod/github.com/zyedidia/highlight@v0.0.0-20200217010119-291680feaca1/highlighter.go:215 +0x96
github.com/zyedidia/highlight.(*Highlighter).HighlightString(0xc000129b98, {0xc0000db980?, 0x2b?})
	/home/jesse/go/pkg/mod/github.com/zyedidia/highlight@v0.0.0-20200217010119-291680feaca1/highlighter.go:278 +0x130
```
This commit is contained in:
Jesse Portnoy
2023-07-16 02:17:04 +01:00
parent 5fa7eb841e
commit b5b98d2281
+5
View File
@@ -199,6 +199,11 @@ func (h *Highlighter) highlightRegion(highlights LineMatch, start int, canMatchE
}
func (h *Highlighter) highlightEmptyRegion(highlights LineMatch, start int, canMatchEnd bool, lineNum int, line []rune, statesOnly bool) LineMatch {
if h.Def.rules == nil {
return nil
}
if len(line) == 0 {
if canMatchEnd {
h.lastRegion = nil