From f4151cb7443a463d71b5e7f1b9071fc4369bebe5 Mon Sep 17 00:00:00 2001 From: GeertJohan Date: Wed, 2 Apr 2014 21:19:03 +0200 Subject: [PATCH] Send nice error when version number is out of range --- main.go | 4 ++++ version.go | 12 ++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index d8ef9eb..cfdab5d 100644 --- a/main.go +++ b/main.go @@ -138,6 +138,10 @@ func handler(resp http.ResponseWriter, req *http.Request) { var ok bool repo.Version, ok = parseVersion(m[3]) if !ok { + if repo.Version == TooHighVersion { + sendNotFound(resp, "Version %q contains a number which is out of range.", m[3]) + return + } sendNotFound(resp, "Version %q improperly considered invalid; please warn the service maintainers.", m[3]) return } diff --git a/version.go b/version.go index 35fc800..f23b254 100644 --- a/version.go +++ b/version.go @@ -48,6 +48,7 @@ func (v Version) IsValid() bool { } var InvalidVersion = Version{-1, -1, -1} +var TooHighVersion = Version{-2, -2, -2} func parseVersion(s string) (Version, bool) { if len(s) < 2 { @@ -66,17 +67,20 @@ func parseVersion(s string) (Version, bool) { if len(part) == 0 || part[0] == '0' { return InvalidVersion, false } - num, err := strconv.Atoi(part) + num, err := strconv.ParseInt(part, 10, 32) if err != nil { + if err.(*strconv.NumError).Err == strconv.ErrRange { + return TooHighVersion, false + } return InvalidVersion, false } switch i { case 0: - v.Major = num + v.Major = int(num) case 1: - v.Minor = num + v.Minor = int(num) case 2: - v.Patch = num + v.Patch = int(num) } }