From 21721990cc658235b5dd95a9280f3f1ea2b8b6f0 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 21 Dec 2023 14:04:52 -0600 Subject: [PATCH 1/3] fix: edge case that caused strange panic error --- .changeset/calm-parents-tell.md | 5 +++++ internal/token.go | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 .changeset/calm-parents-tell.md diff --git a/.changeset/calm-parents-tell.md b/.changeset/calm-parents-tell.md new file mode 100644 index 000000000..67af42491 --- /dev/null +++ b/.changeset/calm-parents-tell.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +Fixes an edge case that caused a difficult-to-reproduce panic diff --git a/internal/token.go b/internal/token.go index afae341bc..6c1ec6b86 100644 --- a/internal/token.go +++ b/internal/token.go @@ -1248,12 +1248,12 @@ func (z *Tokenizer) readUnclosedTag() bool { var close int if z.fm == FrontmatterOpen { close = strings.Index(string(buf), "---") - if close != -1 { + if close != -1 && len(buf) < close { buf = buf[0:close] } } close = bytes.Index(buf, []byte{'>'}) - if close != -1 { + if close != -1 && len(buf) < close { buf = buf[0:close] } if close == -1 { From 091e3652355c8056cb53de0b1f796a7ff2aba6bf Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 21 Dec 2023 14:44:44 -0600 Subject: [PATCH 2/3] fix: bail out of readUnclosedTag early --- internal/token.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/token.go b/internal/token.go index 6c1ec6b86..5a58e1f79 100644 --- a/internal/token.go +++ b/internal/token.go @@ -1251,10 +1251,11 @@ func (z *Tokenizer) readUnclosedTag() bool { if close != -1 && len(buf) < close { buf = buf[0:close] } - } - close = bytes.Index(buf, []byte{'>'}) - if close != -1 && len(buf) < close { - buf = buf[0:close] + } else { + close = bytes.Index(buf, []byte{'>'}) + if close != -1 && len(buf) < close { + buf = buf[0:close] + } } if close == -1 { // We can't find a closing tag... From 624bbe57340005ec52c329c79cafee3420cb1ebc Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 21 Dec 2023 14:52:29 -0600 Subject: [PATCH 3/3] fix: handle strings before elements --- internal/token.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/token.go b/internal/token.go index 5a58e1f79..8a0531966 100644 --- a/internal/token.go +++ b/internal/token.go @@ -1879,6 +1879,14 @@ frontmatter_loop: return z.tt } + // handle string + if c == '\'' || c == '"' || c == '`' { + z.readString(c) + z.tt = TextToken + z.data.End = z.raw.End + return z.tt + } + s := z.buf[z.raw.Start : z.raw.Start+1][0] if s == '<' || s == '{' || s == '}' || c == '<' || c == '{' || c == '}' { @@ -1892,14 +1900,6 @@ frontmatter_loop: } } - // handle string - if c == '\'' || c == '"' || c == '`' { - z.readString(c) - z.tt = TextToken - z.data.End = z.raw.End - return z.tt - } - z.dashCount = 0 continue frontmatter_loop }