public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/siddhesh/gai-cleanup2] gaih_inet: Generic allocation tracking
@ 2022-03-01 2:40 Siddhesh Poyarekar
0 siblings, 0 replies; only message in thread
From: Siddhesh Poyarekar @ 2022-03-01 2:40 UTC (permalink / raw)
To: glibc-cvs
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=1ae1143f50f5ed704d314c79b3fffb2edbe4419b
commit 1ae1143f50f5ed704d314c79b3fffb2edbe4419b
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
Date: Mon Feb 28 13:43:19 2022 +0530
gaih_inet: Generic allocation tracking
Add tracking of allocations in gaih_inet so that all blocks allocated
during lookup are freed at the end.
Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
Diff:
---
sysdeps/posix/getaddrinfo.c | 77 ++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 70 insertions(+), 7 deletions(-)
diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
index 28e9f9bd06..d3abc59ba9 100644
--- a/sysdeps/posix/getaddrinfo.c
+++ b/sysdeps/posix/getaddrinfo.c
@@ -116,6 +116,50 @@ struct gaih_typeproto
char name[8];
};
+struct gaih_lookup_result
+{
+ struct gaih_addrtuple *at; /* The first result tuple. */
+ struct scratch_buffer *allocs; /* Pointers to allocated tuples. */
+ size_t nallocs; /* Count of such pointers. */
+};
+
+/* Initialize RES with BUF, an initialized scratch buffer. */
+
+static void
+gaih_lookup_result_init (struct gaih_lookup_result *res,
+ struct scratch_buffer *buf)
+{
+ res->allocs = buf;
+}
+
+/* Record BUF in RES as an allocated block. */
+
+static bool
+gaih_lookup_result_push_alloc (struct gaih_lookup_result *res, void *buf)
+{
+ size_t nallocs = res->nallocs + 1;
+ if (nallocs * sizeof (void *) > res->allocs->length
+ && !scratch_buffer_grow_preserve (res->allocs))
+ return false;
+
+ ((void **) res->allocs->data)[nallocs - 1] = buf;
+ res->nallocs = nallocs;
+ return true;
+}
+
+/* Free up resources held in RES. */
+
+static void
+gaih_lookup_result_free (struct gaih_lookup_result *res)
+{
+ res->at = NULL;
+ size_t nallocs = res->nallocs;
+ while (nallocs-- > 0)
+ free (((void **) res->allocs->data)[nallocs]);
+
+ res->nallocs = 0;
+}
+
/* Values for `protoflag'. */
#define GAI_PROTO_NOSERVICE 1
#define GAI_PROTO_PROTOANY 2
@@ -416,9 +460,9 @@ get_servtuples (const struct gaih_service *service, const struct addrinfo *req,
static int
get_numeric_res (const char *name, const struct addrinfo *req,
- struct gaih_addrtuple **pat)
+ struct gaih_lookup_result *res)
{
- *pat = NULL;
+ res->at = NULL;
uint32_t addr[4] = {0};
if (__inet_aton_exact (name, (struct in_addr *) addr) != 0)
@@ -454,7 +498,13 @@ get_numeric_res (const char *name, const struct addrinfo *req,
at->name = canonbuf;
}
- *pat = at;
+ if (!gaih_lookup_result_push_alloc (res, at))
+ {
+ free (at);
+ return -EAI_MEMORY;
+ }
+
+ res->at = at;
return 0;
}
@@ -505,7 +555,13 @@ get_numeric_res (const char *name, const struct addrinfo *req,
at->name = canonbuf;
}
- *pat = at;
+ if (!gaih_lookup_result_push_alloc (res, at))
+ {
+ free (at);
+ return -EAI_MEMORY;
+ }
+
+ res->at = at;
return 0;
}
@@ -540,6 +596,9 @@ gaih_inet (const char *name, const struct gaih_service *service,
struct gaih_addrtuple *addrmem = NULL;
char *canonbuf = NULL;
int result = 0;
+ struct gaih_lookup_result res = {0};
+ struct scratch_buffer resbuf;
+ scratch_buffer_init (&resbuf);
if (name != NULL)
{
@@ -553,11 +612,13 @@ gaih_inet (const char *name, const struct gaih_service *service,
malloc_name = true;
}
- if ((result = get_numeric_res (name, req, &at)) != 0)
+ gaih_lookup_result_init (&res, &resbuf);
+
+ if ((result = get_numeric_res (name, req, &res)) != 0)
goto free_and_return;
- else if (at != NULL)
+ else if (res.at != NULL)
{
- addrmem = at;
+ at = res.at;
canon = canonbuf = at->name;
goto process_list;
}
@@ -1123,6 +1184,8 @@ gaih_inet (const char *name, const struct gaih_service *service,
free ((char *) name);
free (addrmem);
free (canonbuf);
+ gaih_lookup_result_free (&res);
+ scratch_buffer_free (&resbuf);
return result;
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-03-01 2:40 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-01 2:40 [glibc/siddhesh/gai-cleanup2] gaih_inet: Generic allocation tracking Siddhesh Poyarekar
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).