You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
first off, thank you for this amazingly productive language :) I'm writing you because I believe that I have found an issue with the streaming JSON decoder in the standard library.
This crashes with "invalid character '\r' in string literal":
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
)
type RequestDataItem struct {
Name string
Data []byte
}
type Request struct {
Data []RequestDataItem
}
func main() {
dec := json.NewDecoder(os.Stdin)
for {
var request Request
err := dec.Decode(&request)
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
fmt.Println("OK")
}
}
(or exactly 512 bytes)
and then crash, because it erroneously tries to base64-decode the "A[..]AA " string generated by appending a space to the unfinished base64 data.
Thank you for pointing that out. I agree with you that this is most likely an issue with my terminal when data is being read from it with a small buffer, like the default 512 that json.NewDecoder uses.
Replacing dec := json.NewDecoder(os.Stdin)
with dec := json.NewDecoder(bufio.NewReaderSize(os.Stdin, 2048))
will also fix the problem.
In other words, the exact same terminal emulator works if I force Go to read from it with a larger 2048 byte buffer.
I'll close this issue, then. Let's hope that the next person observing the problem will find this thread so that they can pinpoint the cause more quickly than me.
Dear Go team,
first off, thank you for this amazingly productive language :) I'm writing you because I believe that I have found an issue with the streaming JSON decoder in the standard library.
This crashes with "invalid character '\r' in string literal":
if presented with this input data:
In contrast, this main function works, because the JSON is now accumulated in the scanner before parsing:
Example data that works in both cases would be
In my opinion, the issue is the space character invented here:
go/src/encoding/json/stream.go
Line 114 in ba108c9
in combination with reading the input in 512 byte chunks here:
go/src/encoding/json/stream.go
Line 157 in ba108c9
So while parsing with the streaming JSON decoder, it will only read up until
(or exactly 512 bytes)
and then crash, because it erroneously tries to base64-decode the "A[..]AA " string generated by appending a space to the unfinished base64 data.
As expected, changing
go/src/encoding/json/stream.go
Line 157 in ba108c9
const minRead = 5120
fixes the issue for my test case here, but of course it'll still break for larger JSON payloads.The text was updated successfully, but these errors were encountered: