From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 9AD79385DC31 for ; Wed, 16 Mar 2022 23:42:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9AD79385DC31 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-478-kyXcQeZPMeKDVr1JNOej8g-1; Wed, 16 Mar 2022 19:42:41 -0400 X-MC-Unique: kyXcQeZPMeKDVr1JNOej8g-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DECDA185A794; Wed, 16 Mar 2022 23:42:40 +0000 (UTC) Received: from greed.delorie.com (ovpn-112-4.rdu2.redhat.com [10.10.112.4]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C9A971402400; Wed, 16 Mar 2022 23:42:40 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.15.2/8.15.2) with ESMTP id 22GNgdQ71741979; Wed, 16 Mar 2022 19:42:39 -0400 From: DJ Delorie To: Siddhesh Poyarekar Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v2 03/12] getaddrinfo: Fix leak with AI_ALL [BZ #28852] In-Reply-To: <20220314094835.1159523-4-siddhesh@sourceware.org> (message from Siddhesh Poyarekar via Libc-alpha on Mon, 14 Mar 2022 15:18:26 +0530) Date: Wed, 16 Mar 2022 19:42:39 -0400 Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Mar 2022 23:42:43 -0000 Siddhesh Poyarekar via Libc-alpha writes: > Use realloc in convert_hostent_to_gaih_addrtuple and fix up pointers in > the result list so that a single block is maintained for > hostbyname3_r/hostbyname2_r and freed in gaih_inet. This result is > never merged with any other results, since the hosts database does not > permit merging. It took me a while to realize that you're basically converting the data structure from a linked list to an array, and ensuring that it's always handled as an array. That means the comment preceeding convert_hostent_to_gaih_addrtuple() is no longer accurate and needs updating. Ideally, the users could be optimized to treat it as an array instead of a list, but it may need to remain a list-like type for compatibility. LGTM with that comment change. Reviewed-by: DJ Delorie > - while (*result) > - result = &(*result)->next; > - Don't skip to end of "list". Ok. > @@ -212,10 +209,30 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req, > if (count == 0 || h->h_length > sizeof (((struct gaih_addrtuple) {}).addr)) > return true; > > - struct gaih_addrtuple *array = calloc (count, sizeof (*array)); > + struct gaih_addrtuple *array = *result; > + size_t old = 0; > + > + while (array != NULL) > + { > + old++; > + array = array->next; > + } > + > + array = realloc (*result, (old + count) * sizeof (*array)); > + Count existing members of *array* and resize base array. Ok. The rest are initialized later, in the unchanged part of the code. > if (array == NULL) > return false; > > + *result = array; > + > + /* Update the next pointers on reallocation. */ > + for (size_t i = 0; i < old; i++) > + array[i].next = array + i + 1; > + > + array += old; > + > + memset (array, 0, count * sizeof (*array)); > + Ok. > for (size_t i = 0; i < count; ++i) > { > if (family == AF_INET && req->ai_family == AF_INET6) > @@ -235,7 +252,6 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req, > array[0].name = h->h_name; > array[count - 1].next = NULL; > > - *result = array; > return true; > } Ok.