Skip to content

Commit a259d58

Browse files
Fix nil pointer dereference in sendHeartbeat
1 parent b9e31d8 commit a259d58

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

gateway/gateway_impl.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,14 @@ func (g *gatewayImpl) heartbeat() {
255255
func (g *gatewayImpl) sendHeartbeat() {
256256
g.config.Logger.Debug("sending heartbeat")
257257

258+
sequence := -1
259+
if g.config.LastSequenceReceived != nil {
260+
sequence = *g.config.LastSequenceReceived
261+
}
262+
258263
ctx, cancel := context.WithTimeout(context.Background(), g.heartbeatInterval)
259264
defer cancel()
260-
if err := g.Send(ctx, OpcodeHeartbeat, MessageDataHeartbeat(*g.config.LastSequenceReceived)); err != nil {
265+
if err := g.Send(ctx, OpcodeHeartbeat, MessageDataHeartbeat(sequence)); err != nil {
261266
if errors.Is(err, discord.ErrShardNotConnected) || errors.Is(err, syscall.EPIPE) {
262267
return
263268
}

gateway/gateway_messages.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gateway
22

33
import (
44
"fmt"
5+
"strconv"
56

67
"github.com/disgoorg/json"
78
"github.com/disgoorg/snowflake/v2"
@@ -496,6 +497,13 @@ func (MessageDataUnknown) messageData() {}
496497
// MessageDataHeartbeat is used to ensure the websocket connection remains open, and disconnect if not.
497498
type MessageDataHeartbeat int
498499

500+
func (m MessageDataHeartbeat) MarshalJSON() ([]byte, error) {
501+
if m == -1 {
502+
return json.NullBytes, nil
503+
}
504+
return []byte(strconv.Itoa(int(m))), nil
505+
}
506+
499507
func (MessageDataHeartbeat) messageData() {}
500508

501509
// MessageDataIdentify is the data used in IdentifyCommandData

0 commit comments

Comments
 (0)