Commit 1987330a authored by liaoqiang's avatar liaoqiang

when line is too long, bufio.ReadLine may not read all data

parent 1b10364c
// Package ogórek is a library for decoding Python's pickle format. // Package gopickle is a library for decoding Python's pickle format.
// //
// ogórek is Polish for "pickle". // gopickle is Polish for "pickle".
package ogórek package gopickle
import ( import (
"bufio" "bufio"
...@@ -238,6 +238,22 @@ func (d Decoder) Decode() (interface{}, error) { ...@@ -238,6 +238,22 @@ func (d Decoder) Decode() (interface{}, error) {
return d.pop(), nil return d.pop(), nil
} }
func (d *Decoder) readLine() ([]byte, error) {
var has_more bool = true
var line []byte = []byte{}
for has_more {
read_data, is_prefix, err := d.r.ReadLine()
if err != nil {
return line, err
}
has_more = is_prefix
for i := 0; i < len(read_data); i++ {
line = append(line, read_data[i])
}
}
return line, nil
}
// Push a marker // Push a marker
func (d *Decoder) mark() { func (d *Decoder) mark() {
d.push(mark{}) d.push(mark{})
...@@ -280,7 +296,7 @@ func (d *Decoder) dup() { ...@@ -280,7 +296,7 @@ func (d *Decoder) dup() {
// Push a float // Push a float
func (d *Decoder) loadFloat() error { func (d *Decoder) loadFloat() error {
line, _, err := d.r.ReadLine() line, err := d.readLine()
if err != nil { if err != nil {
return err return err
} }
...@@ -294,7 +310,7 @@ func (d *Decoder) loadFloat() error { ...@@ -294,7 +310,7 @@ func (d *Decoder) loadFloat() error {
// Push an int // Push an int
func (d *Decoder) loadInt() error { func (d *Decoder) loadInt() error {
line, _, err := d.r.ReadLine() line, err := d.readLine()
if err != nil { if err != nil {
return err return err
} }
...@@ -342,7 +358,7 @@ func (d *Decoder) loadBinInt1() error { ...@@ -342,7 +358,7 @@ func (d *Decoder) loadBinInt1() error {
// Push a long // Push a long
func (d *Decoder) loadLong() error { func (d *Decoder) loadLong() error {
line, _, err := d.r.ReadLine() line, err := d.readLine()
if err != nil { if err != nil {
return err return err
} }
...@@ -422,7 +438,7 @@ func decodeStringEscape(b []byte) string { ...@@ -422,7 +438,7 @@ func decodeStringEscape(b []byte) string {
// Push a string // Push a string
func (d *Decoder) loadString() error { func (d *Decoder) loadString() error {
line, err := d.r.ReadBytes('\n') line, err := d.readLine()
if err != nil { if err != nil {
return err return err
} }
...@@ -437,11 +453,11 @@ func (d *Decoder) loadString() error { ...@@ -437,11 +453,11 @@ func (d *Decoder) loadString() error {
return fmt.Errorf("invalid string delimiter: %c", line[0]) return fmt.Errorf("invalid string delimiter: %c", line[0])
} }
if line[len(line)-2] != delim { if line[len(line)-1] != delim {
return fmt.Errorf("insecure string") return fmt.Errorf("insecure string")
} }
d.push(decodeStringEscape(line[1 : len(line)-2])) d.push(decodeStringEscape(line[1 : len(line)-1]))
return nil return nil
} }
...@@ -477,7 +493,8 @@ func (d *Decoder) loadShortBinString() error { ...@@ -477,7 +493,8 @@ func (d *Decoder) loadShortBinString() error {
} }
func (d *Decoder) loadUnicode() error { func (d *Decoder) loadUnicode() error {
line, _, err := d.r.ReadLine() line, err := d.readLine()
if err != nil { if err != nil {
return err return err
} }
...@@ -485,9 +502,10 @@ func (d *Decoder) loadUnicode() error { ...@@ -485,9 +502,10 @@ func (d *Decoder) loadUnicode() error {
buf := bytes.Buffer{} buf := bytes.Buffer{}
for len(sline) >= 6 { for len(sline) > 0 {
var r rune var r rune
var err error var err error
r, _, sline, err = strconv.UnquoteChar(sline, '\'') r, _, sline, err = strconv.UnquoteChar(sline, '\'')
if err != nil { if err != nil {
return err return err
...@@ -548,11 +566,11 @@ type Class struct { ...@@ -548,11 +566,11 @@ type Class struct {
} }
func (d *Decoder) global() error { func (d *Decoder) global() error {
module, _, err := d.r.ReadLine() module, err := d.readLine()
if err != nil { if err != nil {
return err return err
} }
name, _, err := d.r.ReadLine() name, err := d.readLine()
if err != nil { if err != nil {
return err return err
} }
...@@ -602,7 +620,7 @@ func (d *Decoder) loadAppends() error { ...@@ -602,7 +620,7 @@ func (d *Decoder) loadAppends() error {
} }
func (d *Decoder) get() error { func (d *Decoder) get() error {
line, _, err := d.r.ReadLine() line, err := d.readLine()
if err != nil { if err != nil {
return err return err
} }
...@@ -688,7 +706,7 @@ func (d *Decoder) obj() error { ...@@ -688,7 +706,7 @@ func (d *Decoder) obj() error {
} }
func (d *Decoder) loadPut() error { func (d *Decoder) loadPut() error {
line, _, err := d.r.ReadLine() line, err := d.readLine()
if err != nil { if err != nil {
return err return err
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment