Skip to content

Commit

Permalink
qrchan: clone code list before using
Browse files Browse the repository at this point in the history
Closes #725
  • Loading branch information
tulir committed Feb 4, 2025
1 parent a75587a commit e43fe38
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions qrchan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package whatsmeow

import (
"context"
"slices"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -58,10 +59,10 @@ type qrChannel struct {
stopQRs chan struct{}
}

func (qrc *qrChannel) emitQRs(evt *events.QR) {
func (qrc *qrChannel) emitQRs(codes []string) {
var nextCode string
for {
if len(evt.Codes) == 0 {
if len(codes) == 0 {
if atomic.CompareAndSwapUint32(&qrc.closed, 0, 1) {
qrc.log.Debugf("Ran out of QR codes, closing channel with status %s and disconnecting client", QRChannelTimeout)
qrc.output <- QRChannelTimeout
Expand All @@ -77,10 +78,10 @@ func (qrc *qrChannel) emitQRs(evt *events.QR) {
return
}
timeout := 20 * time.Second
if len(evt.Codes) == 6 {
if len(codes) == 6 {
timeout = 60 * time.Second
}
nextCode, evt.Codes = evt.Codes[0], evt.Codes[1:]
nextCode, codes = codes[0], codes[1:]
qrc.log.Debugf("Emitting QR code %s", nextCode)
select {
case qrc.output <- QRChannelItem{Code: nextCode, Timeout: timeout, Event: QRChannelEventCode}:
Expand Down Expand Up @@ -118,7 +119,7 @@ func (qrc *qrChannel) handleEvent(rawEvt interface{}) {
switch evt := rawEvt.(type) {
case *events.QR:
qrc.log.Debugf("Received QR code event, starting to emit codes to channel")
go qrc.emitQRs(evt)
go qrc.emitQRs(slices.Clone(evt.Codes))
return
case *events.QRScannedWithoutMultidevice:
qrc.log.Debugf("QR code scanned without multidevice enabled")
Expand Down

0 comments on commit e43fe38

Please sign in to comment.