Skip to content
Snippets Groups Projects

Fix buffer size specification in function calls

Merged felixkhals requested to merge bugfix/buffer_length into master
5 files
+ 89
50
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -96,41 +96,61 @@ static void errorExit(char* msg) {
// TODO: Change int return to size_t!
static int getProcessAgentRank(uint32_t jobid, uint32_t vpid, size_t size) {
struct sockaddr_un strAddr;
socklen_t lenAddr;
int fdSock;
if ((fdSock=socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
errorExit("socket");
struct addrinfo hints;
struct addrinfo *result, *rp;
const char* host_ip = "localhost";
const char * agent_port = getenv("DPM_AGENT_PORT");
if (NULL == agent_port) {
errorExit("Could not find DPM_AGENT_PORT env");
}
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; //opting for reliable sequenced socket type
hints.ai_flags = 0;
hints.ai_protocol = IPPROTO_TCP;
int error_code = getaddrinfo(host_ip, agent_port, &hints, &result);
if (0 != error_code) {
errorExit("Could not get addrinfo!");
}
int socket_fd = 0;
for (rp = result; rp != NULL; rp = rp->ai_next) {
socket_fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (socket_fd == -1) {
continue;
}
strAddr.sun_family=AF_LOCAL; /* Unix domain */
const char * socket_path = getenv("DPM_AGENT_SOCKET"); // check for success
if (NULL == socket_path) {
errorExit("Could not find DPM_AGENT_SOCKET env");
if (0 == connect(socket_fd, rp->ai_addr, rp->ai_addrlen)) {
break; //successfully connected
} else {
close(socket_fd); //close socket when we cannot connect
}
}
strcpy(strAddr.sun_path, socket_path);
lenAddr=sizeof(strAddr.sun_family)+strlen(strAddr.sun_path);
if (connect(fdSock, (struct sockaddr*)&strAddr, lenAddr) !=0 ) {
errorExit("connect");
freeaddrinfo(result); //free result
// exit if the result-linked-list has been traversed until the end without a successful
// connection
if (NULL == rp) {
errorExit("Could not connect to host. Host not available or host/port invalid.");
}
pid_t pid = getpid();
char info_to_send[BUFFLEN];
snprintf(info_to_send, BUFFLEN+1,
snprintf(info_to_send, BUFFLEN,
"%d,%u,%u,%zu",
pid, vpid, jobid, size);
if (send(fdSock, info_to_send, BUFFLEN+1, 0) < 0) {
if (send(socket_fd, info_to_send, BUFFLEN, 0) < 0) {
errorExit("send");
}
char rank_to_recv[BUFFLEN];
recv(fdSock, rank_to_recv, BUFFLEN+1, 0);
recv(socket_fd, rank_to_recv, BUFFLEN, 0);
int received_rank = (int)strtol(rank_to_recv, NULL, 0);
printf("Received from server: %d\n", received_rank);
close(fdSock);
close(socket_fd);
return received_rank;
}
Loading