Agent review pass.

This commit is contained in:
hjozwiak committed 2026-11-24 11:12:17 -05:00
1 parent e9b2e9508b
commit 64ee749a8e
105 files changed
+2098 -320

No files matched your search

+20 -8
View File
@@ -64,7 +64,9 @@ func decodeDocument(contents []byte) (writableDocument, Settings, error) {
return writableDocument{}, Settings{}, fmt.Errorf("parse settings schema %d: %w", CurrentSchemaVersion, err)
}
if document.SchemaVersion < speechScaleSchemaVersion {
migrateSpeechScale(&document)
if err := migrateSpeechScale(&document); err != nil {
return writableDocument{}, Settings{}, err
}
}
settings, err := effectiveSettings(document)
if err != nil {
@@ -74,8 +76,9 @@ func decodeDocument(contents []byte) (writableDocument, Settings, error) {
}
// Schema 2 differs only in its legacy voice scale. The migrated document is
// written as the current schema on the next save.
func migrateSpeechScale(document *writableDocument) {
// written as the current schema on the next save. A legacy value outside
// its scale rejects the load, leaving the file untouched.
func migrateSpeechScale(document *writableDocument) error {
applicationProfiles := make(map[string]bool)
for _, name := range document.Applications {
applicationProfiles[name] = true
@@ -86,12 +89,20 @@ func migrateSpeechScale(document *writableDocument) {
if factory == nil && applicationProfiles[name] {
factory = globalFactory
}
for _, voice := range []**Voice{&profile.Speech.DefaultVoice, &profile.Speech.UppercaseVoice, &profile.Speech.HyperlinkVoice, &profile.Speech.SystemVoice, &profile.KeyEcho.EchoVoice} {
voices := []**Voice{&profile.Speech.DefaultVoice, &profile.Speech.UppercaseVoice, &profile.Speech.HyperlinkVoice, &profile.Speech.SystemVoice, &profile.KeyEcho.EchoVoice}
for index, voice := range voices {
if *voice != nil {
converted := voiceFromLegacy(**voice)
// Echo always uses Speech Dispatcher, including with serial output.
if voice != &profile.KeyEcho.EchoVoice && factory != nil && *factory == "hardwarefactory" {
converted = hardwareVoiceFromLegacy(**voice)
var converted Voice
var err error
// Echo always uses Speech Dispatcher, including with serial output,
// so it keeps the wider 0..100 / 0..10 legacy limits.
if index < len(voices)-1 && factory != nil && *factory == "hardwarefactory" {
converted, err = hardwareVoiceFromLegacy(**voice)
} else {
converted, err = voiceFromLegacy(**voice)
}
if err != nil {
return fmt.Errorf("profile %q: %w", name, err)
}
*voice = &converted
}
@@ -99,6 +110,7 @@ func migrateSpeechScale(document *writableDocument) {
document.Profiles[name] = profile
}
document.SchemaVersion = CurrentSchemaVersion
return nil
}
func effectiveSettings(document writableDocument) (Settings, error) {
+29 -6
View File
@@ -18,6 +18,8 @@
package config
import (
"math"
"git.stormux.org/storm/screenreader-common/datetime"
)
@@ -266,7 +268,7 @@ func applyLegacyEcho(settings *Settings, values rawSettings) {
settings.UseCustomEchoForSentence = *values.UseCustomEchoForSentence
}
if values.EchoVoice != nil {
mergeVoice(&settings.EchoVoice, voiceFromLegacy(*values.EchoVoice))
mergeVoice(&settings.EchoVoice, convertLegacyVoice(*values.EchoVoice, false))
}
if len(values.EchoSpeechServerInfo) > 1 {
settings.EchoSpeechServer = values.EchoSpeechServerInfo[1]
@@ -314,14 +316,35 @@ func applyLegacyEcho(settings *Settings, values rawSettings) {
func applyLegacyDefaultVoice(settings *Settings, values rawSettings) {
if voice, exists := values.Voices["default"]; exists {
converted := voiceFromLegacy(voice)
if settings.SpeechServerFactory == "hardwarefactory" {
converted = hardwareVoiceFromLegacy(voice)
}
mergeVoice(&settings.DefaultVoice, converted)
mergeVoice(&settings.DefaultVoice, convertLegacyVoice(voice, settings.SpeechServerFactory == "hardwarefactory"))
}
}
// convertLegacyVoice converts a Python legacy voice, rejecting values
// outside the legacy scale instead of clamping them. apply has no error
// path, so a rejected value is marked non-finite and the caller's
// validation of the imported settings fails the load.
func convertLegacyVoice(voice Voice, hardware bool) Voice {
var converted Voice
var err error
if hardware {
converted, err = hardwareVoiceFromLegacy(voice)
} else {
converted, err = voiceFromLegacy(voice)
}
if err == nil {
return converted
}
converted = cloneVoice(voice)
for _, field := range voiceFields(&converted) {
if *field != nil {
invalid := math.NaN()
*field = &invalid
}
}
return converted
}
func mergeVoice(target *Voice, source Voice) {
if source.Rate != nil {
target.Rate = source.Rate
+27 -6
View File
@@ -33,10 +33,34 @@ import (
// Validate checks the complete configuration without touching the filesystem.
func Validate(settings Settings) error { return validate(settings) }
// wineExclusionUnitLimit bounds the UTF-16 exclusion list handed to the
// Wine helper, NUL terminators included.
const wineExclusionUnitLimit = 32766
// WineExclusionUnits counts a program name in UTF-16 code units plus its
// NUL terminator, matching the aggregate limit validation enforces.
func WineExclusionUnits(name string) int {
return len(utf16.Encode([]rune(name))) + 1
}
// WineExclusionsTooLong returns the first entry whose addition reaches the
// aggregate UTF-16 limit, or false when the whole list fits.
func WineExclusionsTooLong(programs []string) (string, bool) {
units := 0
for _, name := range programs {
units += WineExclusionUnits(name)
if units >= wineExclusionUnitLimit {
return name, true
}
}
return "", false
}
// ValidWineProgramName reports whether an exclusion is an executable basename
// accepted by settings, preferences, and the helper launch boundary.
// accepted by settings, preferences, and the helper launch boundary. The
// 255 cap counts UTF-16 code units, as the helper boundary does.
func ValidWineProgramName(name string) bool {
return name != "" && len(name) <= 255 && strings.TrimSpace(name) == name &&
return name != "" && WineExclusionUnits(name)-1 <= 255 && strings.TrimSpace(name) == name &&
!strings.ContainsAny(name, `/\`) &&
!strings.ContainsFunc(name, func(character rune) bool { return character < ' ' })
}
@@ -48,15 +72,12 @@ func validate(settings Settings) error {
if len(settings.Wine.ExcludedPrograms) > 256 {
return errors.New("too many Wine hook exclusions")
}
exclusionUnits := 0
for _, name := range settings.Wine.ExcludedPrograms {
exclusionUnits += len(utf16.Encode([]rune(name))) + 1
if !ValidWineProgramName(name) {
return errors.New("Wine hook exclusions must be executable basenames")
}
}
if exclusionUnits >= 32766 {
if _, tooLong := WineExclusionsTooLong(settings.Wine.ExcludedPrograms); tooLong {
return errors.New("Wine hook exclusion list is too long")
}
if err := settings.SpeechInterruption.Validate(); err != nil {
+20 -6
View File
@@ -5,6 +5,7 @@
package config
import (
"fmt"
"math"
"git.stormux.org/storm/screenreader-common/hardware"
@@ -37,20 +38,30 @@ func voiceFields(voice *Voice) [3]**float64 {
return [3]**float64{&voice.Rate, &voice.AveragePitch, &voice.Gain}
}
func voiceFromLegacy(voice Voice) Voice {
// voiceFromLegacy converts a legacy 0..100 / 0..10 voice to the -100..100
// scale. Finite values outside the legacy scale are rejected rather than
// clamped, so a corrupt schema-2 or Python settings file fails to load
// instead of silently changing the voice. Non-finite values stay invalid
// so validation still rejects them. SpeechFromLegacy retains clamping for
// the remote API, which must tolerate out-of-range requests.
func voiceFromLegacy(voice Voice) (Voice, error) {
voice = cloneVoice(voice)
for field, value := range voiceFields(&voice) {
// Non-finite values stay invalid so validation still rejects them.
if *value != nil && !math.IsNaN(**value) && !math.IsInf(**value, 0) {
maximum := legacyVoiceScales[field].maximum
if **value < 0 || **value > maximum {
return Voice{}, fmt.Errorf("legacy voice %s %g is outside 0 through %g", []string{"rate", "average-pitch", "gain"}[field], **value, maximum)
}
converted := SpeechFromLegacy(field, **value)
*value = &converted
}
}
return voice
return voice, nil
}
// Serial synthesizers used a different legacy conversion from SSIP.
func hardwareVoiceFromLegacy(voice Voice) Voice {
// Serial synthesizers used a different legacy conversion from SSIP, with
// rate 0..99 and pitch/gain 0..9. The same rejection rule applies.
func hardwareVoiceFromLegacy(voice Voice) (Voice, error) {
voice = cloneVoice(voice)
for field, value := range voiceFields(&voice) {
if *value != nil && !math.IsNaN(**value) && !math.IsInf(**value, 0) {
@@ -58,11 +69,14 @@ func hardwareVoiceFromLegacy(voice Voice) Voice {
if field == VoiceRate {
maximum = 99
}
if **value < 0 || **value > maximum {
return Voice{}, fmt.Errorf("legacy hardware voice %s %g is outside 0 through %g", []string{"rate", "average-pitch", "gain"}[field], **value, maximum)
}
converted := math.Round(hardware.LegacyValue(**value, maximum))
*value = &converted
}
}
return voice
return voice, nil
}
// RoundSpeechValues keeps speech controls and persisted voices at whole
+35
View File
@@ -275,3 +275,38 @@ average-pitch = 4.5
t.Fatal(reloaded.DefaultVoice, err)
}
}
func TestSchemaTwoOutOfRangeVoicesRejected(t *testing.T) {
for name, voice := range map[string]string{
"rate above legacy scale": "rate = 150.0",
"gain below legacy scale": "gain = -3.0",
} {
t.Run(name, func(t *testing.T) {
contents := "schema-version = 2\nactive-profile = \"default\"\n[profiles.default.speech.default-voice]\n" + voice + "\n"
path := writeConfig(t, contents)
store, err := NewStore(path)
if err != nil {
t.Fatal(err)
}
if _, err := store.Load(); err == nil {
t.Fatal("out-of-range legacy voice was clamped and accepted")
}
saved, err := os.ReadFile(path)
if err != nil || string(saved) != contents {
t.Fatalf("rejected load rewrote the settings file: %v", err)
}
})
}
// Hardware voices use the narrower 0..99 / 0..9 legacy scale.
contents := "schema-version = 2\nactive-profile = \"default\"\n[profiles.default.speech]\nserver-factory = \"hardwarefactory\"\noutput-module = \"doubletalk\"\nhardware-device = \"/dev/fixture-synth\"\n[profiles.default.speech.default-voice]\nrate = 100.0\n"
if _, err := Load(writeConfig(t, contents)); err == nil {
t.Fatal("hardware rate 100 accepted on the 0..99 legacy scale")
}
}
func TestLegacyPythonOutOfRangeVoiceRejected(t *testing.T) {
path := writeConfig(t, "[general.voices.default]\nrate = 150.0\n")
if _, err := Load(path); err == nil {
t.Fatal("out-of-range Python legacy voice was clamped and accepted")
}
}
+21
View File
@@ -52,3 +52,24 @@ func TestWineSettingsRejectEmbeddedControlCharacters(t *testing.T) {
})
}
}
func TestWineProgramNameCapCountsUTF16Units(t *testing.T) {
settings := DefaultSettings()
// 100 three-byte runes plus ".exe": 104 UTF-16 units, 304 bytes.
name := ""
for range 100 {
name += "€"
}
name += ".exe"
if len(name) != 304 || WineExclusionUnits(name)-1 != 104 {
t.Fatalf("fixture name is %d bytes / %d UTF-16 units", len(name), WineExclusionUnits(name)-1)
}
settings.Wine.ExcludedPrograms = []string{name}
if err := Validate(settings); err != nil {
t.Fatalf("multibyte name within UTF-16 cap rejected: %v", err)
}
settings.Wine.ExcludedPrograms = []string{name + name + name}
if err := Validate(settings); err == nil {
t.Fatal("name beyond 255 UTF-16 units accepted")
}
}