Skip to content

Commit

Permalink
NULL-terminate the received ZeroMQ message in the C client
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaeon committed Sep 4, 2013
1 parent a90c7ff commit 4aa91db
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/vmpoller-cclient/vmpoller-cclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
Expand Down Expand Up @@ -59,17 +60,18 @@ main(int argc, char *argv[])
*property, /* The property we want as defined in the vSphere Web Services SDK */
*url, /* Datastore URL, only applicable to datastores object type */
*cmd, /* The command to be processed, e.g. 'poll' or 'discover' */
*vcenter, /* The vCenter server we send the request message to */
*result; /* A pointer to hold the result from our request */
*vcenter; /* The vCenter server we send the request message to */

char *result; /* A pointer to hold the result from our request */


/* The ZeroMQ Broker/Proxy endpoint we connect to */
const char *endpoint = DEFAULT_ENDPOINT;

int rc = EX_OK; /* Return code */
int timeout = DEFAULT_TIMEOUT; /* Timeout in msec */
int retries = DEFAULT_RETRIES; /* Number of retries */
int linger = 0; /* Set the ZeroMQ socket option ZMQ_LINGER to 0 */
int msg_len = 0; /* Length of the received message */

char msg_buf[1024]; /* Message buffer to hold the final message we send out */
char ch;
Expand Down Expand Up @@ -187,8 +189,20 @@ main(int argc, char *argv[])

/* Do we have a reply? */
if (items[0].revents & ZMQ_POLLIN) {
if (zmq_msg_recv(&msg_in, zsocket, 0) != -1) {
if ((msg_len = zmq_msg_recv(&msg_in, zsocket, 0)) != -1) {
/*
* Allocate a buffer to hold our resulting message
* The resulting message needs to be NULL-terminated as well
*/
if ((result = malloc(msg_len + 1)) == NULL) {
fprintf(stderr, "Cannot allocate memory\n");
zmq_msg_close(&msg_in);
zmq_ctx_destroy(zcontext);
return (EX_OSERR);
}

result = zmq_msg_data(&msg_in);
result[msg_len] = '\0';
break;
}
} else {
Expand Down

0 comments on commit 4aa91db

Please sign in to comment.