Skip to content

Commit 7b1fdf9

Browse files
author
Hadi Jalilzade
committed
15 * time.Second * time.Second causes an int64 overflow and httpserver fails without any error log. Issue:golang/go#39177
1 parent 7077c83 commit 7b1fdf9

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

cmd/server/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"time"
76
)
87

98
func main() {
@@ -19,10 +18,15 @@ func main() {
1918

2019
config := &ServerConfig{
2120
Address: fmt.Sprintf("%s:%d", *ip, *port),
22-
WriteTimeout: 15 * time.Second,
23-
ReadTimeout: 15 * time.Second,
21+
WriteTimeout: 15,
22+
ReadTimeout: 15,
23+
}
24+
25+
srv, err := NewServer(config)
26+
if err != nil {
27+
fmt.Println(err)
28+
return
2429
}
2530

26-
srv := NewServer(config)
2731
srv.start()
2832
}

cmd/server/server.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package main
22

33
import (
4-
"encoding/json"
4+
"errors"
55
"fmt"
6+
"math"
67
"net/http"
78
"time"
89

@@ -17,34 +18,38 @@ type Server struct {
1718
}
1819

1920
//NewServer is to create httpserver
20-
func NewServer(config *ServerConfig) *Server {
21+
func NewServer(config *ServerConfig) (*Server, error) {
2122
// todo: Create HttppServer
2223

2324
router := mux.NewRouter()
2425
registerRouters(router)
25-
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
26-
// an example API handler
27-
json.NewEncoder(w).Encode(map[string]bool{"ok": true})
28-
})
26+
27+
//Check for Int64 Overflow
28+
if math.MaxInt64/time.Second < config.WriteTimeout {
29+
return nil, errors.New("WriteTimeout overflows int64")
30+
}
31+
32+
if math.MaxInt64/time.Second < config.ReadTimeout {
33+
return nil, errors.New("ReadTimeout overflows int64")
34+
}
2935

3036
server := &Server{
3137
config: config,
3238
router: router,
39+
httpServer: &http.Server{
40+
Handler: router,
41+
Addr: config.Address,
42+
WriteTimeout: config.WriteTimeout * time.Second,
43+
ReadTimeout: config.ReadTimeout * time.Second,
44+
},
3345
}
3446

35-
server.httpServer = &http.Server{
36-
Handler: router,
37-
Addr: config.Address,
38-
WriteTimeout: config.WriteTimeout * time.Second,
39-
ReadTimeout: config.ReadTimeout * time.Second,
40-
}
41-
42-
return server
47+
return server, nil
4348
}
4449

4550
func (s *Server) start() error {
4651
// todo: Start and Listen HttpServer
47-
fmt.Printf("Listening on %s \n", s.httpServer.Addr)
52+
fmt.Printf("Listening on http://%s \n", s.httpServer.Addr)
4853
return s.httpServer.ListenAndServe()
4954
}
5055

0 commit comments

Comments
 (0)