- ptr->hosts= (memcached_host_st *)realloc(ptr->hosts, sizeof(memcached_host_st) * (size_t)ptr->number_of_hosts);
- ptr->hosts[ptr->number_of_hosts].hostname=
- (char *)malloc(sizeof(char) * strlen(hostname));
- memcpy(ptr->hosts[ptr->number_of_hosts].hostname, hostname, strlen(hostname));
+ new_host_list= (memcached_host_st *)realloc(ptr->hosts, sizeof(memcached_host_st) * (ptr->number_of_hosts+1));
+ if (!new_host_list)
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+ memset(&new_host_list[ptr->number_of_hosts], 0, sizeof(memcached_host_st));
+
+ if (!new_host_list)
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+
+ ptr->hosts= new_host_list;
+
+ new_hostname=
+ (char *)malloc(sizeof(char) * (strlen(hostname)+1));
+ if (!new_hostname)
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+
+ memset(new_hostname, 0, strlen(hostname)+1);
+ memcpy(new_hostname, hostname, strlen(hostname));
+ ptr->hosts[ptr->number_of_hosts].hostname= new_hostname;