fwhezfwhez / tcpx

a cross-language tcp framwork in golang
MIT License
235 stars 45 forks source link

当收到错误报文时,会产生fatal error: runtime: out of memory致命错误 #41

Closed NilCent closed 4 years ago

NilCent commented 4 years ago
func LengthOf(stream []byte) (int32, error) {
    if len(stream) < 4 {
        return 0, errors.New(fmt.Sprintf("stream lenth should be bigger than 4"))
    }
    length := binary.BigEndian.Uint32(stream[0:4])
    return int32(length), nil
}

当收到任意报文时,没有对length上限进行判断,可能会开辟过大的内存。 测试代码

func main() {
    var tcpAddr *net.TCPAddr
    tcpAddr, _ = net.ResolveTCPAddr("tcp", "127.0.0.1:7170")

    conn, _ := net.DialTCP("tcp", nil, tcpAddr)
    b := []byte("time\n")
    conn.Write(b)

    time.Sleep(time.Second * 1)
}

测试机内存为2G,当收到错误的报文时,会产生fatal error: runtime: out of memory致命错误

fwhezfwhez commented 4 years ago

3ks for providing this report, message max byte limit has been added into latest pull request.

srv.SetMaxBytePerMessage(25 * tcpx.MB)