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
+12 -4
View File
@@ -3,6 +3,7 @@ package sfz
import (
"fmt"
"io"
"io/fs"
"path"
"sort"
"strings"
@@ -58,21 +59,28 @@ func (d *Document) Write(w io.Writer, opts WriteOptions) error {
}
// WriteFiles writes the main file with the path root, and each file it
// includes at its own path, in the folder of root, using fw.
// includes at its own path, in the folder of root, using fw. It writes nothing
// and returns [ErrInvalid] if an include's path is absolute or leaves the
// folder.
func (d *Document) WriteFiles(root string, fw FileWriter, opts WriteOptions) error {
r := &renderer{opts: opts, defines: map[string]string{}, outputs: map[string]string{}}
text := r.file(d.Root)
if r.err != nil {
return r.err
}
if err := fw(root, []byte(r.lineEndings(text))); err != nil {
return err
}
paths := make([]string, 0, len(r.outputs))
for p := range r.outputs {
// An include path comes from the text, so it can't be trusted to stay in
// the folder.
if !fs.ValidPath(p) {
return fmt.Errorf("%w: the include %q is outside the folder of the main file", ErrInvalid, p)
}
paths = append(paths, p)
}
sort.Strings(paths)
if err := fw(root, []byte(r.lineEndings(text))); err != nil {
return err
}
dir := path.Dir(root)
for _, p := range paths {
if err := fw(path.Join(dir, p), []byte(r.lineEndings(r.outputs[p]))); err != nil {