From 927185e732df87c140cb0b92937d29ebbcf1172f Mon Sep 17 00:00:00 2001 From: Luke Howard Date: Mon, 14 May 2018 12:14:59 +1000 Subject: [PATCH] IPC: Solaris doors backend: avoid using TLS where possible Optimistically delay using thread-local storage for the door reply buffer until we actually need to allocate from the heap. --- lib/ipc/server.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/ipc/server.c b/lib/ipc/server.c index d045deffc..10c693251 100644 --- a/lib/ipc/server.c +++ b/lib/ipc/server.c @@ -1191,12 +1191,11 @@ door_complete(heim_sipc_call ctx, int returnvalue, heim_idata *reply) struct door_reply reply; char buffer[offsetof(struct door_reply, data) + BUFSIZ]; } replyBuf; - int ret; heim_base_once_f(&once, &door_key, door_key_create); - /* free previously saved reply, if any */ - free(HEIMDAL_getspecific(door_key)); + /* door_return() doesn't return; don't leak cred */ + heim_ipc_free_cred(cs->cred); error_reply: rlen = offsetof(struct door_reply, data); @@ -1205,11 +1204,16 @@ error_reply: /* long replies (> BUFSIZ) are allocated from the heap */ if (rlen > BUFSIZ) { - r = malloc(rlen); + int ret; + + /* door_return() doesn't return, so stash reply buffer in TLS */ + r = realloc(HEIMDAL_getspecific(door_key), rlen); if (r == NULL) { returnvalue = EAGAIN; /* don't leak ENOMEM to caller */ goto error_reply; } + + HEIMDAL_setspecific(door_key, r, ret); } else { r = &replyBuf.reply; } @@ -1222,10 +1226,6 @@ error_reply: r->length = 0; } - /* door_return() doesn't return; don't leak cred */ - heim_ipc_free_cred(cs->cred); - HEIMDAL_setspecific(door_key, r == &replyBuf.reply ? NULL : r, ret); - door_return((char *)r, rlen, NULL, 0); }