Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Preserve whitespace in FTS table names during optimization so a similarly named table is not modified instead.

- Keep scoped mirror commits limited to literal file and directory selections, so bracketed filenames do not include matching neighboring files.

- Preserve whitespace in selected mirror commit paths so a similarly named file is not committed instead.
Expand Down
3 changes: 1 addition & 2 deletions store/fts.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ type contextExecer interface {
}

func OptimizeFTS5(ctx context.Context, db contextExecer, table string) error {
table = strings.TrimSpace(table)
if table == "" || strings.ContainsAny(table, "\"\x00") {
if strings.TrimSpace(table) == "" || strings.ContainsAny(table, "\"\x00") {
return fmt.Errorf("invalid FTS5 table %q", table)
}
quoted := QuoteIdent(table)
Expand Down
39 changes: 37 additions & 2 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,43 @@ func TestFTS5Helpers(t *testing.T) {
if err := OptimizeFTS5(ctx, st.DB(), "docs"); err != nil {
t.Fatal(err)
}
if err := OptimizeFTS5(ctx, st.DB(), `bad"table`); err == nil {
t.Fatal("unsafe FTS table should fail")
for _, table := range []string{`bad"table`, "", " \t\n", "bad\x00table"} {
if err := OptimizeFTS5(ctx, st.DB(), table); err == nil {
t.Errorf("unsafe FTS table %q should fail", table)
}
}
}

func TestOptimizeFTS5PreservesTableName(t *testing.T) {
for _, name := range []string{" docs", "docs ", " docs "} {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
st, err := Open(ctx, Options{Path: filepath.Join(t.TempDir(), "archive.db")})
if err != nil {
t.Fatal(err)
}
defer st.Close()
if _, err := st.DB().ExecContext(ctx, `create virtual table `+QuoteIdent(name)+` using fts5(body);
create table docs(docs text);`); err != nil {
t.Fatal(err)
}
if err := OptimizeFTS5(ctx, st.DB(), name); err != nil {
t.Fatal(err)
}
var count int
if err := st.DB().QueryRowContext(ctx, `select count(*) from docs`).Scan(&count); err != nil {
t.Fatal(err)
}
if count != 0 {
t.Fatalf("optimization wrote %d rows to the unrelated docs table", count)
}
if _, err := st.DB().ExecContext(ctx, `drop table docs`); err != nil {
t.Fatal(err)
}
if err := OptimizeFTS5(ctx, st.DB(), name); err != nil {
t.Fatalf("optimize literal FTS table without neighbor: %v", err)
}
})
}
}

Expand Down
Loading