commit 20bf3b8fba247b300b1cc92093c735f0cf184d56
parent a0a8bd0de4d55c3e0a8f419ef72023d0082c1bba
Author: Daniel Barlow <dan@telent.net>
Date: Tue, 19 May 2026 21:30:37 +0100
allocate peer_fds on stack
Diffstat:
| M | luad.c | | | 23 | ++++++++++++----------- |
1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/luad.c b/luad.c
@@ -50,13 +50,12 @@ int open_socket(char * pathname) {
return fd;
}
-int * fds_from_msg(struct msghdr *msg) {
+int * fds_from_msg(struct msghdr *msg, int * peer_fds) {
// * there may be multiple SCM_RIGHTS messages
// * we expect three fds
// * if we get some number other than three, we should
// close them and return NULL
- int *peer_fds = (int *) calloc(3, sizeof(int));
for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(msg);
cmsg != NULL;
cmsg = CMSG_NXTHDR(msg, cmsg)) {
@@ -218,16 +217,18 @@ int main(int argc, char **argv)
if(pollfds[0].revents) {
if(find_peer(0)) {
int len = recvmsg(socket, &msg , 0);
- int *peer_fds = fds_from_msg(&msg);
- char *argline = malloc(len);
- if(! argline) {
- die("malloc failed");
- }
- memmove(argline, buf, len);
+ int peer_fds[3];
+ if(fds_from_msg(&msg, peer_fds)) {
+ char *argline = malloc(len);
+ if(! argline) {
+ die("malloc failed");
+ }
+ memmove(argline, buf, len);
- pid_t child_pid = fork_lua(L, argline, len, peer_fds);
- if(child_pid >= 0) {
- register_peer(child_pid, &peer_addr, msg.msg_namelen);
+ pid_t child_pid = fork_lua(L, argline, len, peer_fds);
+ if(child_pid >= 0) {
+ register_peer(child_pid, &peer_addr, msg.msg_namelen);
+ }
}
}
}