3bl3gamer / tgclient

Telegram (mtproto) client written in golang
MIT License
19 stars 4 forks source link

The messageLen error causes the program to crash #4

Closed wind-hx closed 2 years ago

wind-hx commented 2 years ago

The messageId error causes the program to crash

2022/05/06 13:23:16 [DEBUG] >>> TL_updates
2022/05/06 13:23:16 [DEBUG] >>> TL_msg_container -> [TL_MT_message, TL_MT_message]
2022/05/06 13:23:16 [DEBUG] <<< TL_msgs_ack (#7094489669385657156)
2022/05/06 13:23:16 [DEBUG] <<< TL_msgs_ack (#7094489669385680332)
2022/05/06 13:23:16 [DEBUG] readRoutine done
2022/05/06 13:23:16 [DEBUG] <<< TL_msgs_ack (#7094489669385693884)
2022/05/06 13:23:16 [DEBUG] <<< TL_msgs_ack (#7094489669385706348)
panic: runtime error: slice bounds out of range [:-1473746637]

goroutine 19796 [running]:
wind.com/telegram-transport/src/tgclient/mtproto.(*MTProto).read(0xc00042c8c0)
        /Volumes/wind_ssd/project/go-project/telegram-transport/src/tgclient/mtproto/network.go:171 +0xa3b
wind.com/telegram-transport/src/tgclient/mtproto.(*MTProto).readRoutine(0xc00042c8c0)
        /Volumes/wind_ssd/project/go-project/telegram-transport/src/tgclient/mtproto/mtproto.go:862 +0x9a
created by wind.com/telegram-transport/src/tgclient/mtproto.(*MTProto).Connect
        /Volumes/wind_ssd/project/go-project/telegram-transport/src/tgclient/mtproto/mtproto.go:309 +0x337
telegram-transport runnning...
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)
3bl3gamer commented 2 years ago

Did you manage to reproduce that error? I've encountered strange messageLen multiple times myself, but it was very rare and random, and I was unable to catch the error.

wind-hx commented 2 years ago

https://github.com/3bl3gamer/tgclient/blob/7bad8a0ac224eaaf82a6d051e1d9d0a60d5796fe/mtproto/network.go#L171

before adding

if int(messageLen)+32 < 0 {
    return nil, merry.New("Wrong message_len")
}

I'm also not sure how this error came about, but I avoided the error this way

3bl3gamer commented 2 years ago

Added this check in v0.138.2. But still, it's a temporary fix, and I have no idea why message length becomes negative.