Skip to content

Commit e9c0846

Browse files
committed
Fix uint64 overflow issue
1 parent 8ebb458 commit e9c0846

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

daze.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ func (l *Locale) Run() error {
627627
func NewLocale(listen string, dialer Dialer) *Locale {
628628
return &Locale{
629629
Dialer: dialer,
630-
Limits: rate.NewLimits(math.MaxUint64, time.Second),
630+
Limits: rate.NewLimits(math.MaxUint32, time.Second),
631631
Listen: listen,
632632
}
633633
}

lib/rate/rate.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,50 @@
11
package rate
22

33
import (
4+
"math"
45
"sync"
56
"time"
7+
8+
"github.com/mohanson/daze/lib/doa"
69
)
710

811
// Limits represents a rate limiter that controls resource allocation over time.
912
type Limits struct {
1013
addition uint64
1114
capacity uint64
1215
last time.Time
16+
loop uint64
1317
mu sync.Mutex
1418
size uint64
1519
step time.Duration
1620
}
1721

1822
// Wait ensures there are enough resources (n) available, blocking if necessary.
1923
func (l *Limits) Wait(n uint64) {
24+
doa.Doa(n > 0 && n < math.MaxUint64/2)
2025
l.mu.Lock()
2126
defer l.mu.Unlock()
22-
loop := uint64(time.Since(l.last) / l.step)
23-
l.last = l.last.Add(l.step * time.Duration(loop))
24-
l.size = l.size + loop*l.addition
25-
l.size = min(l.size, l.capacity)
27+
l.loop = uint64(time.Since(l.last) / l.step)
28+
if l.loop > 0 {
29+
l.last = l.last.Add(l.step * time.Duration(l.loop))
30+
doa.Doa(l.loop <= math.MaxUint64/l.addition)
31+
doa.Doa(l.size <= math.MaxUint64-l.addition*l.loop)
32+
l.size = l.size + l.addition*l.loop
33+
l.size = min(l.size, l.capacity)
34+
}
2635
if l.size < n {
27-
loop := (n - l.size + l.addition - 1) / l.addition
28-
time.Sleep(l.step * time.Duration(loop))
29-
l.last = l.last.Add(l.step * time.Duration(loop))
30-
l.size = l.size + loop*l.addition
36+
l.loop = (n - l.size + l.addition - 1) / l.addition
37+
time.Sleep(l.step * time.Duration(l.loop))
38+
l.last = l.last.Add(l.step * time.Duration(l.loop))
39+
l.size = l.size + l.addition*l.loop
3140
}
3241
l.size -= n
3342
}
3443

3544
// NewLimits creates a new rate limiter with rate r over period p.
3645
func NewLimits(r uint64, p time.Duration) *Limits {
46+
doa.Doa(r > 0 && r < math.MaxUint64/2)
47+
doa.Doa(p > 0)
3748
g := func(a, b uint64) uint64 {
3849
t := uint64(0)
3950
for b != 0 {

protocol/ashe/engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func (s *Server) Run() error {
247247
func NewServer(listen string, cipher string) *Server {
248248
return &Server{
249249
Cipher: daze.Salt(cipher),
250-
Limits: rate.NewLimits(math.MaxUint64, time.Second),
250+
Limits: rate.NewLimits(math.MaxUint32, time.Second),
251251
Listen: listen,
252252
}
253253
}

protocol/baboon/engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (s *Server) Run() error {
149149
func NewServer(listen string, cipher string) *Server {
150150
return &Server{
151151
Cipher: daze.Salt(cipher),
152-
Limits: rate.NewLimits(math.MaxUint64, time.Second),
152+
Limits: rate.NewLimits(math.MaxUint32, time.Second),
153153
Listen: listen,
154154
Masker: Conf.Masker,
155155
NextID: uint32(math.MaxUint32),

protocol/czar/engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (s *Server) Run() error {
126126
func NewServer(listen string, cipher string) *Server {
127127
return &Server{
128128
Cipher: daze.Salt(cipher),
129-
Limits: rate.NewLimits(math.MaxUint64, time.Second),
129+
Limits: rate.NewLimits(math.MaxUint32, time.Second),
130130
Listen: listen,
131131
}
132132
}

protocol/dahlia/engine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func NewServer(listen string, server string, cipher string) *Server {
9191
return &Server{
9292
Cipher: daze.Salt(cipher),
9393
Listen: listen,
94-
Limits: rate.NewLimits(math.MaxUint64, time.Second),
94+
Limits: rate.NewLimits(math.MaxUint32, time.Second),
9595
Server: server,
9696
}
9797
}
@@ -171,7 +171,7 @@ func (c *Client) Run() error {
171171
func NewClient(listen string, server string, cipher string) *Client {
172172
return &Client{
173173
Cipher: daze.Salt(cipher),
174-
Limits: rate.NewLimits(math.MaxUint64, time.Second),
174+
Limits: rate.NewLimits(math.MaxUint32, time.Second),
175175
Listen: listen,
176176
Server: server,
177177
}

0 commit comments

Comments
 (0)