s3: libsmb: cleanup resolve_hosts() - don't change return values on fail.
authorJeremy Allison <jra@samba.org>
Tue, 8 Sep 2020 21:04:41 +0000 (14:04 -0700)
committerNoel Power <npower@samba.org>
Tue, 15 Sep 2020 10:09:37 +0000 (10:09 +0000)
Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Noel Power <npower@samba.org>
source3/libsmb/namequery.c

index 67f3f29c7cb3fe93862dfa532f891c023414c5f6..649ba8a6de54b838d11e7191fa02f8dc8e8ab5a8 100644 (file)
@@ -2854,7 +2854,9 @@ static NTSTATUS resolve_hosts(TALLOC_CTX *mem_ctx,
        struct addrinfo *ailist = NULL;
        struct addrinfo *res = NULL;
        int ret = -1;
-       int i = 0;
+       size_t i = 0;
+       size_t ret_count = 0;
+       struct sockaddr_storage *iplist = NULL;
 
        if ( name_type != 0x20 && name_type != 0x0) {
                DEBUG(5, ("resolve_hosts: not appropriate "
@@ -2863,9 +2865,6 @@ static NTSTATUS resolve_hosts(TALLOC_CTX *mem_ctx,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       *return_iplist = NULL;
-       *return_count = 0;
-
        DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n",
                                name, name_type));
 
@@ -2903,26 +2902,36 @@ static NTSTATUS resolve_hosts(TALLOC_CTX *mem_ctx,
                        continue;
                }
 
-               *return_count += 1;
+               /* wrap check. */
+               if (ret_count + 1 < ret_count) {
+                       freeaddrinfo(ailist);
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+               ret_count += 1;
 
-               *return_iplist = talloc_realloc(
-                       mem_ctx, *return_iplist, struct sockaddr_storage,
-                       *return_count);
-               if (!*return_iplist) {
+               iplist = talloc_realloc(
+                       mem_ctx, iplist, struct sockaddr_storage,
+                       ret_count);
+               if (iplist == NULL) {
                        DEBUG(3,("resolve_hosts: malloc fail !\n"));
                        freeaddrinfo(ailist);
                        return NT_STATUS_NO_MEMORY;
                }
-               (*return_iplist)[i] = ss;
+               iplist[i] = ss;
                i++;
        }
        if (ailist) {
                freeaddrinfo(ailist);
        }
-       if (*return_count) {
-               return NT_STATUS_OK;
+       if (ret_count == 0) {
+               return NT_STATUS_UNSUCCESSFUL;
        }
-       return NT_STATUS_UNSUCCESSFUL;
+       if ((int)ret_count < 0) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+       *return_count = (int)ret_count;
+       *return_iplist = iplist;
+       return NT_STATUS_OK;
 }
 
 /********************************************************