Bound parsing work and fix writer, lexer and keyswitch bugs

- Count variable expansions against MaxBytes and stop substituting past it, so
  nested #defines can't grow exponentially. Read at most MaxBytes from a reader.
- Lex in linear time: end an opcode value at the next line break with the same
  regexp, and merge text nodes by slicing. Cap variable names at 128 bytes.
- Read a byte order mark as space so a directive after it is recognised.
- Refuse in WriteFiles an include path that is absolute or leaves the folder, and
  report include.outside and sample.outside from Validate.
- Give a lone sw_lokey or sw_hikey a one-key range.
- Sum controller modulations in a fixed order so Instrument is deterministic.
- Stop samplePrefix looping on a sample at the root.
- Validate without lexing plain opcodes again, and cache registry lookups.
- Add regression tests and FuzzWrite, FuzzEdit and FuzzBuild.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 18:12:16 +00:00
co-authored by Claude Sonnet 5.5
parent 3d2ab78a32
commit cf54471778
8 changed files with 499 additions and 54 deletions
+44 -3
View File
@@ -2,6 +2,9 @@ package sfz
import (
"fmt"
"io/fs"
"path"
"regexp"
"slices"
"strings"
)
@@ -10,7 +13,7 @@ import (
// read. It reports what is wrong with the text and with values the format
// describes, and never fails on an opcode it doesn't know.
func (d *Document) Validate() []Problem {
v := &validator{defines: map[string]string{}}
v := &validator{defines: map[string]string{}, known: map[string]known{}}
v.problems = append(v.problems, d.problems...)
v.file(d.Root)
v.endHeader()
@@ -24,8 +27,17 @@ type opcodeAt struct {
file string
}
// known is the result of a [Lookup].
type known struct {
info OpcodeInfo
ok bool
}
type validator struct {
defines map[string]string
defines map[string]string
// known holds the lookups made, as a document names the same few opcodes
// many times.
known map[string]known
problems []Problem
// inHeader is whether a header has been read.
inHeader bool
@@ -46,12 +58,19 @@ func (v *validator) file(f *File) {
v.defines[n.Name] = strings.TrimSpace(substitute(v.defines, n.Value))
case KindInclude:
v.undefined(f, n, n.Value)
if n.Resolved != "" && !fs.ValidPath(n.Resolved) {
v.add(f.Path, n, SeverityWarning, "include.outside", fmt.Sprintf("%s is outside the folder of the main file", n.Resolved))
}
if n.File != nil {
v.file(n.File)
}
case KindHeader:
v.header(f, n, n.Name)
case KindOpcode, KindText:
if n.Kind == KindOpcode && !strings.Contains(n.Raw, "$") {
v.opcode(f, n, n.Name, n.Value)
continue
}
v.undefined(f, n, n.Raw)
text := n.Raw
if strings.Contains(text, "$") {
@@ -121,7 +140,12 @@ func (v *validator) opcode(f *File, n *Node, name, value string) {
if strings.Contains(name, "$") {
return
}
info, ok := Lookup(name)
k, seen := v.known[name]
if !seen {
k.info, k.ok = Lookup(name)
v.known[name] = k
}
info, ok := k.info, k.ok
if !ok {
v.add(f.Path, n, SeverityInfo, "opcode.unknown", fmt.Sprintf("%s isn't an opcode of the format", name))
return
@@ -132,6 +156,9 @@ func (v *validator) opcode(f *File, n *Node, name, value string) {
if strings.Contains(value, "$") {
return
}
if (name == "sample" || name == "default_path") && outsideFolder(value) {
v.add(f.Path, n, SeverityInfo, "sample.outside", fmt.Sprintf("%s=%s is outside the folder of the main file", name, value))
}
v.value(f, n, name, value, info)
}
@@ -195,3 +222,17 @@ func (v *validator) undefined(f *File, n *Node, text string) {
}
}
}
// driveRE reads the drive of a Windows path.
var driveRE = regexp.MustCompile(`^[A-Za-z]:`)
// outsideFolder reports whether a sample path is absolute or leaves the
// folder it is relative to.
func outsideFolder(p string) bool {
p = strings.ReplaceAll(strings.TrimSpace(p), `\`, "/")
if strings.HasPrefix(p, "/") || driveRE.MatchString(p) {
return true
}
p = path.Clean(p)
return p == ".." || strings.HasPrefix(p, "../")
}