Add bounds checking on user-supplied input
This commit is contained in:
parent
3b523f7742
commit
4068146c44
6
main.go
6
main.go
|
@ -46,7 +46,7 @@ func main() {
|
|||
if err := json.Unmarshal(sidewalk_data, &sidewalks); err != nil {
|
||||
log.Fatalf("Error decoding JSON: %v", err)
|
||||
}
|
||||
|
||||
sidewalksLen := len(sidewalks)
|
||||
for i := range sidewalks {
|
||||
sidewalks[i].Condition = sidewalk.Condition(rand.IntN(4))
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ func main() {
|
|||
return
|
||||
}
|
||||
sidewalkID, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
if err != nil || sidewalkID < 0 || sidewalkID >= sidewalksLen {
|
||||
http.Error(w, "Invalid id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ func main() {
|
|||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
})
|
||||
|
||||
fmt.Printf("Serving on :%v\n", *port)
|
||||
fmt.Printf("Serving %v sidewalks on :%v\n", &sidewalksLen, *port)
|
||||
panic(http.ListenAndServe(fmt.Sprintf(":%v", *port), nil))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue