1
0
mirror of https://github.com/golang/go synced 2024-11-25 06:07:58 -07:00

encoding/json: do not read beyond array literal

Fixes #3942.

R=golang-dev, mike.rosset, r
CC=golang-dev
https://golang.org/cl/6524043
This commit is contained in:
Russ Cox 2012-09-18 14:22:55 -04:00
parent 370ae05545
commit ccf2b8843e
2 changed files with 23 additions and 1 deletions

View File

@ -78,7 +78,7 @@ Input:
// scanEnd is delayed one byte. // scanEnd is delayed one byte.
// We might block trying to get that byte from src, // We might block trying to get that byte from src,
// so instead invent a space byte. // so instead invent a space byte.
if v == scanEndObject && dec.scan.step(&dec.scan, ' ') == scanEnd { if (v == scanEndObject || v == scanEndArray) && dec.scan.step(&dec.scan, ' ') == scanEnd {
scanp += i + 1 scanp += i + 1
break Input break Input
} }

View File

@ -6,6 +6,7 @@ package json
import ( import (
"bytes" "bytes"
"net"
"reflect" "reflect"
"testing" "testing"
) )
@ -145,3 +146,24 @@ func TestNullRawMessage(t *testing.T) {
t.Fatalf("Marshal: have %#q want %#q", b, msg) t.Fatalf("Marshal: have %#q want %#q", b, msg)
} }
} }
var blockingTests = []string{
`{"x": 1}`,
`[1, 2, 3]`,
}
func TestBlocking(t *testing.T) {
for _, enc := range blockingTests {
r, w := net.Pipe()
go w.Write([]byte(enc))
var val interface{}
// If Decode reads beyond what w.Write writes above,
// it will block, and the test will deadlock.
if err := NewDecoder(r).Decode(&val); err != nil {
t.Errorf("decoding %s: %v", enc, err)
}
r.Close()
w.Close()
}
}