allocate peer_fds on stack

This commit is contained in:
2026-05-19 21:30:37 +01:00
parent a0a8bd0de4
commit 20bf3b8fba
+12 -11
View File
@@ -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);
}
}
}
}