mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* [PATCH] eliminate alloca(3) use
@ 2022-11-18  7:58 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2022-11-18  7:58 UTC (permalink / raw)
  To: mwrap-perl

alloca(3) is difficult for the compiler and static checkers to
deal with, and we already reserve TSD space for storing path
names.
---
 Mwrap.xs     |  2 +-
 mwrap_core.h | 21 ++++++++++++---------
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/Mwrap.xs b/Mwrap.xs
index b515894..367a4c3 100644
--- a/Mwrap.xs
+++ b/Mwrap.xs
@@ -153,7 +153,7 @@ mwrap_get(loc)
 PREINIT:
 	STRLEN len;
 	const char *str;
-	struct src_loc *k = (void *)kbuf;
+	struct src_loc *k = &tsd.src_loc;
 	uintptr_t p;
 	struct cds_lfht_iter iter;
 	struct cds_lfht_node *cur;
diff --git a/mwrap_core.h b/mwrap_core.h
index 3abc3dd..09b7930 100644
--- a/mwrap_core.h
+++ b/mwrap_core.h
@@ -178,9 +178,12 @@ struct alloc_hdr {
 };
 
 /* $PATHNAME:$LINENO */
-static MWRAP_TSD char kbuf[
-	sizeof(struct src_loc) + PATH_MAX + sizeof(":") + UINT2STR_MAX
-];
+static MWRAP_TSD union {
+	char kbuf[
+		sizeof(struct src_loc) + PATH_MAX + sizeof(":") + UINT2STR_MAX
+	];
+	struct src_loc src_loc;
+} tsd;
 
 static struct alloc_hdr *ptr2hdr(void *p)
 {
@@ -290,7 +293,7 @@ static struct src_loc *assign_line(size_t size, const char *file, unsigned line)
 	len = strlen(file);
 	if (len > PATH_MAX)
 		len = PATH_MAX;
-	k = (void *)kbuf;
+	k = &tsd.src_loc;
 	k->total = size;
 	dst = mempcpy(k->k, file, len);
 	*dst++ = ':';
@@ -311,7 +314,6 @@ static struct src_loc *
 update_stats_rcu_lock(size_t *generation, size_t size, uintptr_t caller)
 {
 	struct src_loc *k, *ret = 0;
-	static const size_t xlen = sizeof(caller);
 	struct cds_lfht *t = CMM_LOAD_SHARED(totals);
 	const COP *cop = NULL;
 
@@ -326,11 +328,11 @@ update_stats_rcu_lock(size_t *generation, size_t size, uintptr_t caller)
 		ret = assign_line(size, OutCopFILE(cop), CopLINE(cop));
 #endif /* MWRAP_PERL */
 	if (!ret) {
-		k = alloca(sizeof(*k) + xlen);
+		k = &tsd.src_loc;
 		k->total = size;
-		memcpy(k->k, &caller, xlen);
+		memcpy(k->k, &caller, sizeof(caller));
 		k->capa = 0;
-		hash_loc(k, xlen);
+		hash_loc(k, sizeof(caller));
 		ret = totals_add_rcu(k);
 	}
 out:
@@ -672,7 +674,8 @@ static void dump_destructor(void)
 			*dump_path) {
 		char *end = strchr(dump_path, ',');
 		if (end) {
-			char *tmp = alloca(end - dump_path + 1);
+			char *tmp = tsd.kbuf;
+			assert((end - dump_path) < sizeof(tsd.kbuf));
 			end = mempcpy(tmp, dump_path, end - dump_path);
 			*end = 0;
 			dump_path = tmp;

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-11-18  7:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-18  7:58 [PATCH] eliminate alloca(3) use Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mwrap-perl.git

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).