fix(encode): propagate invalid UTF-8 key errors from headers and inline tables
Assisted-by: GLM 5.3
This commit is contained in:
@@ -579,7 +579,9 @@ func (e *encoder) emitDoc(doc *tomlDoc, prefix []string) error {
|
||||
path := append(append([]string{}, prefix...), t.key)
|
||||
e.writeBlankLine()
|
||||
e.buf.WriteByte('[')
|
||||
writeKeyPath(&e.buf, path)
|
||||
if err := e.writeKeyPath(path); err != nil {
|
||||
return err
|
||||
}
|
||||
e.buf.WriteString("]\n")
|
||||
if err := e.emitDoc(t.doc, path); err != nil {
|
||||
return err
|
||||
@@ -590,7 +592,9 @@ func (e *encoder) emitDoc(doc *tomlDoc, prefix []string) error {
|
||||
for _, sub := range a.docs {
|
||||
e.writeBlankLine()
|
||||
e.buf.WriteString("[[")
|
||||
writeKeyPath(&e.buf, path)
|
||||
if err := e.writeKeyPath(path); err != nil {
|
||||
return err
|
||||
}
|
||||
e.buf.WriteString("]]\n")
|
||||
if err := e.emitDoc(sub, path); err != nil {
|
||||
return err
|
||||
@@ -615,7 +619,9 @@ func (e *encoder) emitDoc(doc *tomlDoc, prefix []string) error {
|
||||
path := append(append([]string{}, prefix...), ent.key)
|
||||
e.writeBlankLine()
|
||||
e.buf.WriteByte('[')
|
||||
writeKeyPath(&e.buf, path)
|
||||
if err := e.writeKeyPath(path); err != nil {
|
||||
return err
|
||||
}
|
||||
e.buf.WriteString("]\n")
|
||||
if err := e.emitDoc(ent.doc, path); err != nil {
|
||||
return err
|
||||
@@ -625,7 +631,9 @@ func (e *encoder) emitDoc(doc *tomlDoc, prefix []string) error {
|
||||
for _, sub := range ent.docs {
|
||||
e.writeBlankLine()
|
||||
e.buf.WriteString("[[")
|
||||
writeKeyPath(&e.buf, path)
|
||||
if err := e.writeKeyPath(path); err != nil {
|
||||
return err
|
||||
}
|
||||
e.buf.WriteString("]]\n")
|
||||
if err := e.emitDoc(sub, path); err != nil {
|
||||
return err
|
||||
@@ -637,10 +645,9 @@ func (e *encoder) emitDoc(doc *tomlDoc, prefix []string) error {
|
||||
}
|
||||
|
||||
func (e *encoder) writeKV(key string, val any) error {
|
||||
if !utf8.ValidString(key) {
|
||||
return fmt.Errorf("interpres: key %q is not valid UTF-8", key)
|
||||
if err := e.writeKey(key); err != nil {
|
||||
return err
|
||||
}
|
||||
e.writeKey(key)
|
||||
e.buf.WriteString(" = ")
|
||||
if err := e.writeValue(val); err != nil {
|
||||
return err
|
||||
@@ -649,25 +656,30 @@ func (e *encoder) writeKV(key string, val any) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeKeyPath(buf *bytes.Buffer, path []string) {
|
||||
func (e *encoder) writeKeyPath(path []string) error {
|
||||
for i, p := range path {
|
||||
if i > 0 {
|
||||
buf.WriteByte('.')
|
||||
e.buf.WriteByte('.')
|
||||
}
|
||||
if isBareKey(p) {
|
||||
buf.WriteString(p)
|
||||
continue
|
||||
if err := e.writeKey(p); err != nil {
|
||||
return err
|
||||
}
|
||||
writeQuotedString(buf, p)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *encoder) writeKey(key string) {
|
||||
// writeKey writes one key, bare when it qualifies and quoted otherwise. A key
|
||||
// that is not valid UTF-8 is an error; writing it anyway would emit corrupt
|
||||
// TOML, because the quoted form has no representation for it.
|
||||
func (e *encoder) writeKey(key string) error {
|
||||
if isBareKey(key) {
|
||||
e.buf.WriteString(key)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
writeQuotedString(&e.buf, key)
|
||||
if !utf8.ValidString(key) {
|
||||
return fmt.Errorf("interpres: key %q is not valid UTF-8", key)
|
||||
}
|
||||
return writeQuotedString(&e.buf, key)
|
||||
}
|
||||
|
||||
// writeQuotedString writes s as a TOML basic string (double-quoted) to buf.
|
||||
@@ -787,7 +799,9 @@ func (e *encoder) writeInlineTable(m map[string]any) error {
|
||||
if i > 0 {
|
||||
e.buf.WriteString(", ")
|
||||
}
|
||||
e.writeKey(k)
|
||||
if err := e.writeKey(k); err != nil {
|
||||
return err
|
||||
}
|
||||
e.buf.WriteString(" = ")
|
||||
if err := e.writeValue(m[k]); err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user