]> git.lizzy.rs Git - go-anidb.git/commitdiff
udpapi: Avoid out-of-bounds access on malformed packets
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Sun, 14 Jul 2013 17:32:50 +0000 (14:32 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Sun, 14 Jul 2013 17:32:50 +0000 (14:32 -0300)
Also reports the strconv.ParseInt error if it's not possible to decode
the reply's code.

udp/reply.go

index b517f213345eac0456a59969dcaa17fbbfcb9bea..b3227250dfc75d2a8cf23eedda291e469a0eb4a0 100755 (executable)
@@ -128,7 +128,7 @@ func newGenericReply(raw []byte) (r *genericReply) {
        tag := ""
        text := ""
        code, err := strconv.ParseInt(parts[0], 10, 16)
-       if err != nil {
+       if err != nil && len(parts) > 1 {
                tag = parts[0]
                code, err = strconv.ParseInt(parts[1], 10, 16)
 
@@ -139,10 +139,10 @@ func newGenericReply(raw []byte) (r *genericReply) {
                text = strings.Join(parts[1:], " ")
        }
 
-       var e *APIError = nil
+       e := err
        // 720-799 range is for notifications
        // 799 is an API server shutdown notice, so I guess it's okay to be an error
-       if code < 200 || (code > 299 && code < 720) || code > 798 {
+       if err == nil && code < 200 || (code > 299 && code < 720) || code > 798 {
                e = &APIError{Code: int(code), Desc: text}
        }