mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-11 16:14:05 -08:00
promql: fix potential panic in the query logger (#6094)
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
parent
6970f725c9
commit
cf3d74ed69
|
@ -41,12 +41,14 @@ const (
|
|||
entrySize int = 1000
|
||||
)
|
||||
|
||||
func parseBrokenJson(brokenJson []byte, logger log.Logger) (bool, string) {
|
||||
func parseBrokenJson(brokenJson []byte) (bool, string) {
|
||||
queries := strings.ReplaceAll(string(brokenJson), "\x00", "")
|
||||
queries = queries[:len(queries)-1] + "]"
|
||||
if len(queries) > 0 {
|
||||
queries = queries[:len(queries)-1] + "]"
|
||||
}
|
||||
|
||||
// Conditional because of implementation detail: len() = 1 implies file consisted of a single char: '['.
|
||||
if len(queries) == 1 {
|
||||
if len(queries) <= 1 {
|
||||
return false, "[]"
|
||||
}
|
||||
|
||||
|
@ -68,7 +70,7 @@ func logUnfinishedQueries(filename string, filesize int, logger log.Logger) {
|
|||
return
|
||||
}
|
||||
|
||||
queriesExist, queries := parseBrokenJson(brokenJson, logger)
|
||||
queriesExist, queries := parseBrokenJson(brokenJson)
|
||||
if !queriesExist {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -136,3 +136,43 @@ func TestMMapFile(t *testing.T) {
|
|||
t.Fatalf("Mmap failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBrokenJson(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
b []byte
|
||||
|
||||
ok bool
|
||||
out string
|
||||
}{
|
||||
{
|
||||
b: []byte(""),
|
||||
},
|
||||
{
|
||||
b: []byte("\x00\x00"),
|
||||
},
|
||||
{
|
||||
b: []byte("\x00[\x00"),
|
||||
},
|
||||
{
|
||||
b: []byte("\x00[]\x00"),
|
||||
ok: true,
|
||||
out: "[]",
|
||||
},
|
||||
{
|
||||
b: []byte("[\"up == 0\",\"rate(http_requests[2w]\"]\x00\x00\x00"),
|
||||
ok: true,
|
||||
out: "[\"up == 0\",\"rate(http_requests[2w]\"]",
|
||||
},
|
||||
} {
|
||||
t.Run("", func(t *testing.T) {
|
||||
ok, out := parseBrokenJson(tc.b)
|
||||
if tc.ok != ok {
|
||||
t.Fatalf("expected %t, got %t", tc.ok, ok)
|
||||
return
|
||||
}
|
||||
if ok && tc.out != out {
|
||||
t.Fatalf("expected %s, got %s", tc.out, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue