diff --git a/CHANGELOG.md b/CHANGELOG.md index 75e0c9d92..2a04248dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -826,6 +826,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 those blank lines with the section, and setup appends a section after one blank line, so removing and re-adding it toggles between the same two files. A re-added section goes at the end of the file. +- **`bd config set` no longer truncates a `config.yaml` that has a line of + 64 KiB or more.** Setting an undotted key (such as `actor` or `no-db`) + rewrites the file line by line, and a line of 65,536 bytes or more ended + that read early without an error, so the command reported success and wrote + back only the lines before the long one, plus the key it set. Dotted keys + and `bd config unset` were not affected. Setting an undotted key in such a + file now fails with `bufio.Scanner: token too long`. `bd config set` leaves + the file as it was. `set-many` stops at that key, but it has already written + any `config.yaml` keys listed before it, such as a dotted `sync.remote`. - **`bd show --as-of` exits 1 when it reads no issue.** A ref the Dolt AS OF read does not take (an instant, a versioned-history address), a ref that does not exist, or an issue that did not exist at the ref was reported on diff --git a/internal/config/yaml_config.go b/internal/config/yaml_config.go index 046e0f67a..9a2630c42 100644 --- a/internal/config/yaml_config.go +++ b/internal/config/yaml_config.go @@ -913,6 +913,11 @@ func updateYamlKey(content, key, value string) (string, error) { result = append(result, line) } } + // A line of bufio.MaxScanTokenSize bytes or more ends the scan early; + // returning the lines read so far would truncate config.yaml on write. + if err := scanner.Err(); err != nil { + return "", fmt.Errorf("cannot set %q: scanning config content: %w", key, err) + } if !found { // Key not found - append at end diff --git a/internal/config/yaml_config_test.go b/internal/config/yaml_config_test.go index 929735b8d..f84a14244 100644 --- a/internal/config/yaml_config_test.go +++ b/internal/config/yaml_config_test.go @@ -1,6 +1,8 @@ package config import ( + "bufio" + "errors" "fmt" "os" "os/exec" @@ -247,6 +249,86 @@ func walkMap(m map[string]interface{}, parts []string) interface{} { return cur } +// A line of bufio.MaxScanTokenSize bytes or more stops the flat-key path's +// line scanner partway through the document. updateYamlKey used to return the +// lines read up to that point with a nil error, which setYamlConfigAtPath then +// wrote over config.yaml, dropping the long line and everything after it. +// A line one byte shorter still fits, and must get the same rewrite a short +// document gets rather than the refusal. +func TestUpdateYamlKeyRejectsOversizeLine(t *testing.T) { + doc := func(line string) string { return "k1: v1\n" + line + "\nk2: v2" } + atLimit := strings.Repeat("x", bufio.MaxScanTokenSize) + underLimit := strings.Repeat("x", bufio.MaxScanTokenSize-1) + + tests := []struct { + name string + content string + key string + want string + wantErr bool + }{ + {name: "line of MaxScanTokenSize bytes, key present", content: doc(atLimit), key: "k1", wantErr: true}, + {name: "line of MaxScanTokenSize bytes, key absent", content: doc(atLimit), key: "k3", wantErr: true}, + { + name: "line of MaxScanTokenSize-1 bytes, key present", + content: doc(underLimit), + key: "k1", + want: "k1: \"zz\"\n" + underLimit + "\nk2: v2", + }, + { + name: "line of MaxScanTokenSize-1 bytes, key absent", + content: doc(underLimit), + key: "k3", + want: doc(underLimit) + "\n\nk3: \"zz\"", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := updateYamlKey(tt.content, tt.key, "zz") + if tt.wantErr { + if !errors.Is(err, bufio.ErrTooLong) { + t.Errorf("updateYamlKey() error = %v, want %v", err, bufio.ErrTooLong) + } + if got != "" { + t.Errorf("updateYamlKey() returned %d bytes of the %d-byte document, want none", len(got), len(tt.content)) + } + return + } + if err != nil { + t.Fatalf("updateYamlKey() error = %v", err) + } + if got != tt.want { + t.Errorf("updateYamlKey() returned %d bytes, want the %d-byte rewrite", len(got), len(tt.want)) + } + }) + } +} + +// setYamlConfigAtPath is the read, rewrite, write step behind bd config set for +// an undotted key. When updateYamlKey refuses a document with an oversize line, +// the error has to come back before the write, so config.yaml keeps every byte +// it had. +func TestSetYamlConfigAtPathOversizeLineLeavesFileUntouched(t *testing.T) { + configPath := filepath.Join(t.TempDir(), "config.yaml") + before := "no-db: false\n# " + strings.Repeat("x", bufio.MaxScanTokenSize) + "\njson: true\n" + if err := os.WriteFile(configPath, []byte(before), 0o600); err != nil { + t.Fatalf("failed to write config.yaml: %v", err) + } + + if err := setYamlConfigAtPath(configPath, "no-db", "true"); !errors.Is(err, bufio.ErrTooLong) { + t.Errorf("setYamlConfigAtPath() error = %v, want %v", err, bufio.ErrTooLong) + } + + after, err := os.ReadFile(configPath) + if err != nil { + t.Fatalf("failed to read config.yaml: %v", err) + } + if string(after) != before { + t.Errorf("config.yaml is %d bytes after the refused set, want the original %d bytes unchanged", len(after), len(before)) + } +} + func TestFormatYamlValue(t *testing.T) { tests := []struct { value string