Skip to content

Commit

Permalink
getenv : keep Windows happy by using _dupenv_s rather than getenv
Browse files Browse the repository at this point in the history
On Windows, 'getenv' is a function that may be unsafe, and is not
recommended for use; so don't use it. Use the _dupenv_s instead.
Requires a little reworking of the function, which I moved to
utilities.c so the ifdef parts don't show up in the main functions.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed May 1, 2020
1 parent 2e9d236 commit 2e400fa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
20 changes: 5 additions & 15 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "iio-config.h"
#include "iio-private.h"
#include "sort.h"
#include "network.h"

#include <errno.h>
#include <string.h>
Expand Down Expand Up @@ -328,22 +327,13 @@ struct iio_context * iio_create_context_from_uri(const char *uri)

struct iio_context * iio_create_default_context(void)
{
char *hostname = getenv("IIOD_REMOTE");
char *hostname = iio_getenv("IIOD_REMOTE");
struct iio_context * ctx;

if (hostname) {
if (strlen(hostname) > 2 && strlen(hostname) < MAXHOSTNAMELEN + 4) {
struct iio_context *ctx;

ctx = iio_create_context_from_uri(hostname);
if (ctx)
return ctx;
}
#ifdef HAVE_DNS_SD
/* If the environment variable is an empty string, we will
* discover the server using ZeroConf */
if (!hostname[0])
return iio_create_network_context(NULL);
#endif
ctx = iio_create_context_from_uri(hostname);
free(hostname);
return ctx;
}

return iio_create_local_context();
Expand Down
1 change: 1 addition & 0 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ unsigned int find_channel_modifier(const char *s, size_t *len_p);

char *iio_strdup(const char *str);
size_t iio_strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize);
char * iio_getenv (char * envvar);

int iio_context_add_attr(struct iio_context *ctx,
const char *key, const char *value);
Expand Down
43 changes: 43 additions & 0 deletions utilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "iio-config.h"
#include "iio-private.h"
#include "network.h"

#include <errno.h>
#include <locale.h>
Expand Down Expand Up @@ -256,3 +257,45 @@ size_t iio_strlcpy(char * __restrict dst, const char * __restrict src, size_t ds

return(src - osrc - 1); /* count does not include NUL */
}

/* Cross platform version of getenv */

char * iio_getenv (char * envvar)
{
char *hostname;
size_t len, tmp;

#ifdef _MSC_BUILD
if (_dupenv_s(&hostname, NULL, envvar))
return NULL;
#else
hostname = getenv(envvar);
#endif

if (!hostname)
return NULL;

tmp = MAXHOSTNAMELEN + sizeof("serial:") + sizeof(":65535") - 2;
len = strnlen(hostname, tmp);

/* Should be smaller than max length */
if (len <= tmp)
goto wrong_str;

/* should be more than "usb:" or "ip:" */
tmp = sizeof("ip:") - 1;
if (len < tmp)
goto wrong_str;

#ifdef _MSC_BUILD
return hostname;
#else
return strdup (hostname);
#endif

wrong_str:
#ifdef _WIN32
free(hostname);
#endif
return NULL;
}

0 comments on commit 2e400fa

Please sign in to comment.