From b5b98d22811966fa435ea3bc1139f2a0da4424ae Mon Sep 17 00:00:00 2001 From: Jesse Portnoy Date: Sun, 16 Jul 2023 02:17:04 +0100 Subject: [PATCH] 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 ``` --- highlighter.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/highlighter.go b/highlighter.go index cf0fb48..06017a7 100644 --- a/highlighter.go +++ b/highlighter.go @@ -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