Browse Source

Update to disallow new Go 1.13 float formats. (#5984)

Don't inadvertantly expand the acceptable ways to specify
a float.

Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
pull/5897/head
Brian Brazil 5 years ago committed by GitHub
parent
commit
94b1af1471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      pkg/textparse/openmetricsparse.go
  2. 16
      pkg/textparse/openmetricsparse_test.go
  3. 10
      pkg/textparse/promparse.go
  4. 16
      pkg/textparse/promparse_test.go

5
pkg/textparse/openmetricsparse.go

@ -20,7 +20,6 @@ import (
"io"
"math"
"sort"
"strconv"
"strings"
"unicode/utf8"
@ -268,7 +267,7 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
if t2 != tValue {
return EntryInvalid, parseError("expected value after metric", t)
}
if p.val, err = strconv.ParseFloat(yoloString(p.l.buf()[1:]), 64); err != nil {
if p.val, err = parseFloat(yoloString(p.l.buf()[1:])); err != nil {
return EntryInvalid, err
}
// Ensure canonical NaN value.
@ -283,7 +282,7 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
p.hasTS = true
var ts float64
// A float is enough to hold what we need for millisecond resolution.
if ts, err = strconv.ParseFloat(yoloString(p.l.buf()[1:]), 64); err != nil {
if ts, err = parseFloat(yoloString(p.l.buf()[1:])); err != nil {
return EntryInvalid, err
}
p.ts = int64(ts * 1000)

16
pkg/textparse/openmetricsparse_test.go

@ -370,6 +370,22 @@ func TestOpenMetricsParseErrors(t *testing.T) {
input: "empty_label_name{=\"\"} 0",
err: "expected label name or left brace, got \"EQUAL\"",
},
{
input: "foo 1_2\n",
err: "unsupported character in float",
},
{
input: "foo 0x1p-3\n",
err: "unsupported character in float",
},
{
input: "foo 0x1P-3\n",
err: "unsupported character in float",
},
{
input: "foo 0 1_2\n",
err: "unsupported character in float",
},
}
for i, c := range cases {

10
pkg/textparse/promparse.go

@ -332,7 +332,7 @@ func (p *PromParser) Next() (Entry, error) {
if t2 != tValue {
return EntryInvalid, parseError("expected value after metric", t)
}
if p.val, err = strconv.ParseFloat(yoloString(p.l.buf()), 64); err != nil {
if p.val, err = parseFloat(yoloString(p.l.buf())); err != nil {
return EntryInvalid, err
}
// Ensure canonical NaN value.
@ -409,3 +409,11 @@ var helpReplacer = strings.NewReplacer(
func yoloString(b []byte) string {
return *((*string)(unsafe.Pointer(&b)))
}
func parseFloat(s string) (float64, error) {
// Keep to pre-Go 1.13 float formats.
if strings.ContainsAny(s, "pP_") {
return 0, fmt.Errorf("unsupported character in float")
}
return strconv.ParseFloat(s, 64)
}

16
pkg/textparse/promparse_test.go

@ -249,6 +249,22 @@ func TestPromParseErrors(t *testing.T) {
input: "empty_label_name{=\"\"} 0",
err: "expected label name, got \"EQUAL\"",
},
{
input: "foo 1_2\n",
err: "unsupported character in float",
},
{
input: "foo 0x1p-3\n",
err: "unsupported character in float",
},
{
input: "foo 0x1P-3\n",
err: "unsupported character in float",
},
{
input: "foo 0 1_2\n",
err: "expected next entry after timestamp, got \"MNAME\"",
},
}
for i, c := range cases {

Loading…
Cancel
Save