Skip to content

Commit 011fa2b

Browse files
committed
Ensure constant format strings in wf calls
Fixes: ./encode.go:231:11: non-constant format string in call to (*github.com/BurntSushi/toml.Encoder).wf ./encode.go:233:11: non-constant format string in call to (*github.com/BurntSushi/toml.Encoder).wf ./encode.go:282:10: non-constant format string in call to (*github.com/BurntSushi/toml.Encoder).wf ./encode.go:284:10: non-constant format string in call to (*github.com/BurntSushi/toml.Encoder).wf ./encode.go:286:10: non-constant format string in call to (*github.com/BurntSushi/toml.Encoder).wf ./encode.go:300:11: non-constant format string in call to (*github.com/BurntSushi/toml.Encoder).wf ./encode.go:315:11: non-constant format string in call to (*github.com/BurntSushi/toml.Encoder).wf Fixes #445
1 parent 4b439bf commit 011fa2b

1 file changed

Lines changed: 38 additions & 30 deletions

File tree

encode.go

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ func (enc *Encoder) eElement(rv reflect.Value) {
228228
}
229229
switch v.Location() {
230230
default:
231-
enc.wf(v.Format(format))
231+
enc.write(v.Format(format))
232232
case internal.LocalDatetime, internal.LocalDate, internal.LocalTime:
233-
enc.wf(v.In(time.UTC).Format(format))
233+
enc.write(v.In(time.UTC).Format(format))
234234
}
235235
return
236236
case Marshaler:
@@ -279,40 +279,40 @@ func (enc *Encoder) eElement(rv reflect.Value) {
279279
case reflect.String:
280280
enc.writeQuoted(rv.String())
281281
case reflect.Bool:
282-
enc.wf(strconv.FormatBool(rv.Bool()))
282+
enc.write(strconv.FormatBool(rv.Bool()))
283283
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
284-
enc.wf(strconv.FormatInt(rv.Int(), 10))
284+
enc.write(strconv.FormatInt(rv.Int(), 10))
285285
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
286-
enc.wf(strconv.FormatUint(rv.Uint(), 10))
286+
enc.write(strconv.FormatUint(rv.Uint(), 10))
287287
case reflect.Float32:
288288
f := rv.Float()
289289
if math.IsNaN(f) {
290290
if math.Signbit(f) {
291-
enc.wf("-")
291+
enc.write("-")
292292
}
293-
enc.wf("nan")
293+
enc.write("nan")
294294
} else if math.IsInf(f, 0) {
295295
if math.Signbit(f) {
296-
enc.wf("-")
296+
enc.write("-")
297297
}
298-
enc.wf("inf")
298+
enc.write("inf")
299299
} else {
300-
enc.wf(floatAddDecimal(strconv.FormatFloat(f, 'g', -1, 32)))
300+
enc.write(floatAddDecimal(strconv.FormatFloat(f, 'g', -1, 32)))
301301
}
302302
case reflect.Float64:
303303
f := rv.Float()
304304
if math.IsNaN(f) {
305305
if math.Signbit(f) {
306-
enc.wf("-")
306+
enc.write("-")
307307
}
308-
enc.wf("nan")
308+
enc.write("nan")
309309
} else if math.IsInf(f, 0) {
310310
if math.Signbit(f) {
311-
enc.wf("-")
311+
enc.write("-")
312312
}
313-
enc.wf("inf")
313+
enc.write("inf")
314314
} else {
315-
enc.wf(floatAddDecimal(strconv.FormatFloat(f, 'g', -1, 64)))
315+
enc.write(floatAddDecimal(strconv.FormatFloat(f, 'g', -1, 64)))
316316
}
317317
case reflect.Array, reflect.Slice:
318318
enc.eArrayOrSliceElement(rv)
@@ -342,20 +342,20 @@ func floatAddDecimal(fstr string) string {
342342
}
343343

344344
func (enc *Encoder) writeQuoted(s string) {
345-
enc.wf("\"%s\"", dblQuotedReplacer.Replace(s))
345+
enc.write(`"` + dblQuotedReplacer.Replace(s) + `"`)
346346
}
347347

348348
func (enc *Encoder) eArrayOrSliceElement(rv reflect.Value) {
349349
length := rv.Len()
350-
enc.wf("[")
350+
enc.write("[")
351351
for i := 0; i < length; i++ {
352352
elem := eindirect(rv.Index(i))
353353
enc.eElement(elem)
354354
if i != length-1 {
355-
enc.wf(", ")
355+
enc.write(", ")
356356
}
357357
}
358-
enc.wf("]")
358+
enc.write("]")
359359
}
360360

361361
func (enc *Encoder) eArrayOfTables(key Key, rv reflect.Value) {
@@ -368,7 +368,7 @@ func (enc *Encoder) eArrayOfTables(key Key, rv reflect.Value) {
368368
continue
369369
}
370370
enc.newline()
371-
enc.wf("%s[[%s]]", enc.indentStr(key), key)
371+
enc.writef("%s[[%s]]", enc.indentStr(key), key)
372372
enc.newline()
373373
enc.eMapOrStruct(key, trv, false)
374374
}
@@ -381,7 +381,7 @@ func (enc *Encoder) eTable(key Key, rv reflect.Value) {
381381
enc.newline()
382382
}
383383
if len(key) > 0 {
384-
enc.wf("%s[%s]", enc.indentStr(key), key)
384+
enc.writef("%s[%s]", enc.indentStr(key), key)
385385
enc.newline()
386386
}
387387
enc.eMapOrStruct(key, rv, false)
@@ -427,7 +427,7 @@ func (enc *Encoder) eMap(key Key, rv reflect.Value, inline bool) {
427427
if inline {
428428
enc.writeKeyValue(Key{mapKey.String()}, val, true)
429429
if trailC || i != len(mapKeys)-1 {
430-
enc.wf(", ")
430+
enc.write(", ")
431431
}
432432
} else {
433433
enc.encode(key.add(mapKey.String()), val)
@@ -436,12 +436,12 @@ func (enc *Encoder) eMap(key Key, rv reflect.Value, inline bool) {
436436
}
437437

438438
if inline {
439-
enc.wf("{")
439+
enc.write("{")
440440
}
441441
writeMapKeys(mapKeysDirect, len(mapKeysSub) > 0)
442442
writeMapKeys(mapKeysSub, false)
443443
if inline {
444-
enc.wf("}")
444+
enc.write("}")
445445
}
446446
}
447447

@@ -539,7 +539,7 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {
539539
if inline {
540540
enc.writeKeyValue(Key{keyName}, fieldVal, true)
541541
if fieldIndex[0] != totalFields-1 {
542-
enc.wf(", ")
542+
enc.write(", ")
543543
}
544544
} else {
545545
enc.encode(key.add(keyName), fieldVal)
@@ -548,14 +548,14 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {
548548
}
549549

550550
if inline {
551-
enc.wf("{")
551+
enc.write("{")
552552
}
553553

554554
l := len(fieldsDirect) + len(fieldsSub)
555555
writeFields(fieldsDirect, l)
556556
writeFields(fieldsSub, l)
557557
if inline {
558-
enc.wf("}")
558+
enc.write("}")
559559
}
560560
}
561561

@@ -705,7 +705,7 @@ func isEmpty(rv reflect.Value) bool {
705705

706706
func (enc *Encoder) newline() {
707707
if enc.hasWritten {
708-
enc.wf("\n")
708+
enc.write("\n")
709709
}
710710
}
711711

@@ -727,14 +727,22 @@ func (enc *Encoder) writeKeyValue(key Key, val reflect.Value, inline bool) {
727727
enc.eElement(val)
728728
return
729729
}
730-
enc.wf("%s%s = ", enc.indentStr(key), key.maybeQuoted(len(key)-1))
730+
enc.writef("%s%s = ", enc.indentStr(key), key.maybeQuoted(len(key)-1))
731731
enc.eElement(val)
732732
if !inline {
733733
enc.newline()
734734
}
735735
}
736736

737-
func (enc *Encoder) wf(format string, v ...any) {
737+
func (enc *Encoder) write(s string) {
738+
_, err := enc.w.WriteString(s)
739+
if err != nil {
740+
encPanic(err)
741+
}
742+
enc.hasWritten = true
743+
}
744+
745+
func (enc *Encoder) writef(format string, v ...any) {
738746
_, err := fmt.Fprintf(enc.w, format, v...)
739747
if err != nil {
740748
encPanic(err)

0 commit comments

Comments
 (0)