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 {
|
if err := json.Unmarshal(sidewalk_data, &sidewalks); err != nil {
|
||||||
log.Fatalf("Error decoding JSON: %v", err)
|
log.Fatalf("Error decoding JSON: %v", err)
|
||||||
}
|
}
|
||||||
|
sidewalksLen := len(sidewalks)
|
||||||
for i := range sidewalks {
|
for i := range sidewalks {
|
||||||
sidewalks[i].Condition = sidewalk.Condition(rand.IntN(4))
|
sidewalks[i].Condition = sidewalk.Condition(rand.IntN(4))
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sidewalkID, err := strconv.Atoi(r.PathValue("id"))
|
sidewalkID, err := strconv.Atoi(r.PathValue("id"))
|
||||||
if err != nil {
|
if err != nil || sidewalkID < 0 || sidewalkID >= sidewalksLen {
|
||||||
http.Error(w, "Invalid id", http.StatusBadRequest)
|
http.Error(w, "Invalid id", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ func main() {
|
||||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
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))
|
panic(http.ListenAndServe(fmt.Sprintf(":%v", *port), nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue