Skip to content

Commit 2e400fa

Browse files
committed
getenv : keep Windows happy by using _dupenv_s rather than getenv
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]>
1 parent 2e9d236 commit 2e400fa

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

context.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "iio-config.h"
2121
#include "iio-private.h"
2222
#include "sort.h"
23-
#include "network.h"
2423

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

329328
struct iio_context * iio_create_default_context(void)
330329
{
331-
char *hostname = getenv("IIOD_REMOTE");
330+
char *hostname = iio_getenv("IIOD_REMOTE");
331+
struct iio_context * ctx;
332332

333333
if (hostname) {
334-
if (strlen(hostname) > 2 && strlen(hostname) < MAXHOSTNAMELEN + 4) {
335-
struct iio_context *ctx;
336-
337-
ctx = iio_create_context_from_uri(hostname);
338-
if (ctx)
339-
return ctx;
340-
}
341-
#ifdef HAVE_DNS_SD
342-
/* If the environment variable is an empty string, we will
343-
* discover the server using ZeroConf */
344-
if (!hostname[0])
345-
return iio_create_network_context(NULL);
346-
#endif
334+
ctx = iio_create_context_from_uri(hostname);
335+
free(hostname);
336+
return ctx;
347337
}
348338

349339
return iio_create_local_context();

iio-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ unsigned int find_channel_modifier(const char *s, size_t *len_p);
314314

315315
char *iio_strdup(const char *str);
316316
size_t iio_strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize);
317+
char * iio_getenv (char * envvar);
317318

318319
int iio_context_add_attr(struct iio_context *ctx,
319320
const char *key, const char *value);

utilities.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "iio-config.h"
2323
#include "iio-private.h"
24+
#include "network.h"
2425

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

257258
return(src - osrc - 1); /* count does not include NUL */
258259
}
260+
261+
/* Cross platform version of getenv */
262+
263+
char * iio_getenv (char * envvar)
264+
{
265+
char *hostname;
266+
size_t len, tmp;
267+
268+
#ifdef _MSC_BUILD
269+
if (_dupenv_s(&hostname, NULL, envvar))
270+
return NULL;
271+
#else
272+
hostname = getenv(envvar);
273+
#endif
274+
275+
if (!hostname)
276+
return NULL;
277+
278+
tmp = MAXHOSTNAMELEN + sizeof("serial:") + sizeof(":65535") - 2;
279+
len = strnlen(hostname, tmp);
280+
281+
/* Should be smaller than max length */
282+
if (len <= tmp)
283+
goto wrong_str;
284+
285+
/* should be more than "usb:" or "ip:" */
286+
tmp = sizeof("ip:") - 1;
287+
if (len < tmp)
288+
goto wrong_str;
289+
290+
#ifdef _MSC_BUILD
291+
return hostname;
292+
#else
293+
return strdup (hostname);
294+
#endif
295+
296+
wrong_str:
297+
#ifdef _WIN32
298+
free(hostname);
299+
#endif
300+
return NULL;
301+
}

0 commit comments

Comments
 (0)