Skip to content

Commit

Permalink
tests: fix minor issues/errors pointed out by Microsoft Studio
Browse files Browse the repository at this point in the history
wingetopt, isn't worth fixing, so compile it with -D_CRT_SECURE_NO_WARNINGS=1

use a safe version fopen on windows.

move strdup to a safe/cross platform verison of strndup
adding some things to the top level CMAKE so it can find strndup

many iio functions return ssize_t, but iio_strerr wants an int, so cast
things appropriately.

when calling functions with bool arguments, don't use NULL, use false.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed May 1, 2020
1 parent 282c10d commit 9a00766
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 52 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ endif()

include(CheckSymbolExists)
check_symbol_exists(strdup "string.h" HAS_STRDUP)
check_symbol_exists(strndup "string.h" HAS_STRNDUP)
check_symbol_exists(strerror_r "string.h" HAS_STRERROR_R)
check_symbol_exists(newlocale "locale.h" HAS_NEWLOCALE)

Expand Down
1 change: 1 addition & 0 deletions iio-config.h.cmakein
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#cmakedefine WITH_LOCAL_CONFIG
#cmakedefine HAS_PIPE2
#cmakedefine HAS_STRDUP
#cmakedefine HAS_STRNDUP
#cmakedefine HAS_STRERROR_R
#cmakedefine HAS_NEWLOCALE
#cmakedefine HAS_PTHREAD_SETNAME_NP
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ endif()
if (MSVC)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../deps/wingetopt/src)
set(GETOPT_C_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../deps/wingetopt/src/getopt.c)
set_source_files_properties(${GETOPT_C_FILE} PROPERTIES COMPILE_FLAGS -D_CRT_SECURE_NO_WARNINGS=1)
endif (MSVC)

if (WIN32)
Expand Down
45 changes: 33 additions & 12 deletions tests/gen_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <errno.h>
#include <iio.h>

#include "iio_common.h"

static FILE *fd = NULL;
static char *uri;
static enum languages {
Expand All @@ -30,23 +32,38 @@ static enum languages {
UNSUPPORTED_LANG,
} lang = UNSUPPORTED_LANG;

static int gen_fopen(FILE** pFile, const char *filename, const char *mode)
{
int ret = 0;


#ifdef _MSC_BUILD
ret = fopen_s(pFile, filename, mode);
#else
*pFile = fopen(filename, mode);
if (!*pFile)
ret = -errno;
#endif

return ret;
}

bool gen_test_path(const char *gen_file)
{
FILE *test;
char *first, *last;
char *last;
int ret;

if (!gen_file)
return false;

if (gen_file[0] == '-')
return false;

last = first = strchr(gen_file, '.');
if (!first)
last = strrchr(gen_file, '.');
if (*last != '.')
return false;

while ((first = strchr(last, '.')))
last = first + 1;
last++;

if (!strcmp(last, "c"))
lang = C_LANG;
Expand All @@ -57,8 +74,8 @@ bool gen_test_path(const char *gen_file)
return false;
}

test = fopen(gen_file, "w");
if (!test)
ret = gen_fopen(&test, gen_file, "w");
if (ret)
return false;
fclose(test);

Expand All @@ -67,15 +84,19 @@ bool gen_test_path(const char *gen_file)

void gen_start(const char *gen_file)
{
int ret;

if (!gen_file)
return;

if (lang == UNSUPPORTED_LANG)
return;

fd = fopen(gen_file, "w");
if (!fd) {
fprintf(stderr, "Error '%s' opening file: %s\n", strerror(errno),gen_file);
ret = gen_fopen(&fd, gen_file, "w");
if (ret) {
char buf[1024];
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "Error '%s' opening file: %s\n", buf, gen_file);
return;
}
if (lang == C_LANG) {
Expand Down Expand Up @@ -142,7 +163,7 @@ void gen_context (const char *uri_in)
if (!fd)
return;

uri = strdup(uri_in);
uri = cmn_strndup(uri_in, NAME_MAX);
if (lang == C_LANG) {
fprintf(fd, "\t/* Create IIO Context */\n"
"\tIIO_ASSERT(ctx = iio_create_context_from_uri(\"%s\"));\n\n", uri);
Expand Down
26 changes: 12 additions & 14 deletions tests/iio_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

#ifdef _WIN32
#define snprintf sprintf_s
#else
#define _strdup strdup
#endif

static bool str_match(const char * haystack, char * needle, bool ignore)
Expand All @@ -55,17 +53,17 @@ static bool str_match(const char * haystack, char * needle, bool ignore)
if (!strcmp(".", needle) || !strcmp("*", needle))
return true;

ncpy = _strdup(needle);
hcpy = _strdup(haystack);
ncpy = cmn_strndup(needle, NAME_MAX);
hcpy = cmn_strndup(haystack, NAME_MAX);

if (!ncpy || !hcpy)
goto eek;

if (ignore) {
for (i = 0; hcpy[i]; i++)
hcpy[i] = tolower(hcpy[i]);
hcpy[i] = (char) (tolower(hcpy[i]) & 0xFF);
for (i = 0; ncpy[i]; i++)
ncpy[i] = tolower(ncpy[i]);
ncpy[i] = (char) (tolower(ncpy[i]) & 0xFF);
}

first = ncpy[0];
Expand Down Expand Up @@ -113,7 +111,7 @@ static void dump_device_attributes(const struct iio_device *dev,
else
printf("'%s'\n", buf);
} else {
iio_strerror(-ret, buf, BUF_SIZE);
iio_strerror(-(int)ret, buf, BUF_SIZE);
printf("ERROR: %s (%li)\n", buf, (long)ret);
}
}
Expand All @@ -124,7 +122,7 @@ static void dump_device_attributes(const struct iio_device *dev,
if (!quiet)
printf("wrote %li bytes to %s\n", (long)ret, attr);
} else {
iio_strerror(-ret, buf, BUF_SIZE);
iio_strerror(-(int)ret, buf, BUF_SIZE);
printf("ERROR: %s (%li) while writing '%s' with '%s'\n",
buf, (long)ret, attr, wbuf);
}
Expand Down Expand Up @@ -153,7 +151,7 @@ static void dump_buffer_attributes(const struct iio_device *dev,
else
printf("'%s'\n", buf);
} else {
iio_strerror(-ret, buf, BUF_SIZE);
iio_strerror(-(int)ret, buf, BUF_SIZE);
printf("ERROR: %s (%li)\n", buf, (long)ret);
}
}
Expand All @@ -165,7 +163,7 @@ static void dump_buffer_attributes(const struct iio_device *dev,
if (!quiet)
printf("wrote %li bytes to %s\n", (long)ret, attr);
} else {
iio_strerror(-ret, buf, BUF_SIZE);
iio_strerror(-(int)ret, buf, BUF_SIZE);
printf("ERROR: %s (%li) while writing '%s' with '%s'\n",
buf, (long)ret, attr, wbuf);
}
Expand Down Expand Up @@ -195,7 +193,7 @@ static void dump_debug_attributes(const struct iio_device *dev,
else
printf("'%s'\n", buf);
} else {
iio_strerror(-ret, buf, BUF_SIZE);
iio_strerror(-(int)ret, buf, BUF_SIZE);
printf("ERROR: %s (%li)\n", buf, (long)ret);
}
}
Expand All @@ -207,7 +205,7 @@ static void dump_debug_attributes(const struct iio_device *dev,
if (!quiet)
printf("wrote %li bytes to %s\n", (long)ret, attr);
} else {
iio_strerror(-ret, buf, BUF_SIZE);
iio_strerror(-(int)ret, buf, BUF_SIZE);
printf("ERROR: %s (%li) while writing '%s' with '%s'\n",
buf, (long)ret, attr, wbuf);
}
Expand Down Expand Up @@ -249,7 +247,7 @@ static void dump_channel_attributes(const struct iio_device *dev,
else
printf("value '%s'\n", buf);
} else {
iio_strerror(-ret, buf, BUF_SIZE);
iio_strerror(-(int)ret, buf, BUF_SIZE);
printf("ERROR: %s (%li)\n", buf, (long)ret);
}
}
Expand All @@ -260,7 +258,7 @@ static void dump_channel_attributes(const struct iio_device *dev,
if (!quiet)
printf("wrote %li bytes to %s\n", (long)ret, attr);
} else {
iio_strerror(-ret, buf, BUF_SIZE);
iio_strerror(-(int)ret, buf, BUF_SIZE);
printf("error %s (%li) while writing '%s' with '%s'\n",
buf, (long)ret, attr, wbuf);
}
Expand Down
27 changes: 24 additions & 3 deletions tests/iio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
#include <stdio.h>
#include <inttypes.h>
#include <getopt.h>
#include <string.h>

#include "iio_common.h"
#include "gen_code.h"
#include "iio-config.h"

#ifdef _MSC_BUILD
#define inline __inline
Expand All @@ -47,6 +49,25 @@ void * xmalloc(size_t n, const char * name)
return p;
}

char *cmn_strndup(const char *str, size_t n)
{
#ifdef HAS_STRNDUP
return strndup(str, n);
#else
size_t len = strnlen(str, n + 1);
char *buf = malloc(len + 1);

if (buf) {
/* len = size of buf, so memcpy is OK */
memcpy(buf, str, len); /* Flawfinder: ignore */
buf[len + 1] = 0;
}
return buf;
#endif
}



struct iio_context * autodetect_context(bool rtn, bool gen_code, const char * name)
{
struct iio_scan_context *scan_ctx;
Expand All @@ -65,7 +86,7 @@ struct iio_context * autodetect_context(bool rtn, bool gen_code, const char * na
ret = iio_scan_context_get_info_list(scan_ctx, &info);
if (ret < 0) {
char *err_str = xmalloc(BUF_SIZE, name);
iio_strerror(-ret, err_str, BUF_SIZE);
iio_strerror(-(int)ret, err_str, BUF_SIZE);
fprintf(stderr, "Scanning for IIO contexts failed: %s\n", err_str);
free (err_str);
goto err_free_ctx;
Expand Down Expand Up @@ -107,7 +128,7 @@ struct iio_context * autodetect_context(bool rtn, bool gen_code, const char * na
unsigned long int sanitize_clamp(const char *name, const char *argv,
uint64_t min, uint64_t max)
{
unsigned long int val;
uint64_t val;
char buf[20];

if (!argv) {
Expand All @@ -126,7 +147,7 @@ unsigned long int sanitize_clamp(const char *name, const char *argv,
val = min;
fprintf(stderr, "Clamped %s to min %" PRIu64 "\n", name, min);
}
return val;
return (unsigned long int) val;
}


Expand Down
12 changes: 12 additions & 0 deletions tests/iio_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,22 @@ enum backend {
};

void * xmalloc(size_t n, const char *name);
char *cmn_strndup(const char *str, size_t n);

struct iio_context * autodetect_context(bool rtn, bool gen_code, const char *name);
unsigned long int sanitize_clamp(const char *name, const char *argv,
uint64_t min, uint64_t max);
void usage(char *name, const struct option *options, const char *options_descriptions[]);

/* https://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html
* {NAME_MAX} : Maximum number of bytes in a filename
* {PATH_MAX} : Maximum number of bytes in a pathname
* {PAGESIZE} : Size in bytes of a page
* Too bad we work on non-POSIX systems
*/
#ifndef NAME_MAX
#define NAME_MAX 256
#endif


#endif /* IIO_TESTS_COMMON_H */
16 changes: 10 additions & 6 deletions tests/iio_genxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@

#include "iio_common.h"

#ifndef _WIN32
#define _strdup strdup
#endif

#define MY_NAME "iio_genxml"

static const struct option options[] = {
Expand All @@ -53,11 +49,13 @@ static const char *options_descriptions[] = {
int main(int argc, char **argv)
{
char *xml;
const char *tmp;
struct iio_context *ctx;
int c, option_index = 0;
const char *arg_uri = NULL;
const char *arg_xml = NULL;
const char *arg_ip = NULL;
size_t xml_len;
enum backend backend = IIO_LOCAL;

while ((c = getopt_long(argc, argv, "+hn:x:u:",
Expand Down Expand Up @@ -111,7 +109,13 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}

xml = _strdup(iio_context_get_xml(ctx));
tmp = iio_context_get_xml(ctx);
if (!tmp) {
iio_context_destroy(ctx);
return EXIT_FAILURE;
}
xml_len = strnlen(tmp, (size_t)-1);
xml = cmn_strndup(tmp, xml_len);
if (!xml) {
iio_context_destroy(ctx);
return EXIT_FAILURE;
Expand All @@ -121,7 +125,7 @@ int main(int argc, char **argv)

iio_context_destroy(ctx);

ctx = iio_create_xml_context_mem(xml, strlen(xml));
ctx = iio_create_xml_context_mem(xml, xml_len);
if (!ctx) {
fprintf(stderr, "Unable to re-generate context\n");
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/iio_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ int main(int argc, char **argv)
printf("\n");

if (do_scan) {
autodetect_context(false, NULL, MY_NAME);
autodetect_context(false, false, MY_NAME);
return EXIT_SUCCESS;
}

if (detect_context)
ctx = autodetect_context(true, NULL, MY_NAME);
ctx = autodetect_context(true, false, MY_NAME);
else if (backend == IIO_XML)
ctx = iio_create_xml_context(arg_xml);
else if (backend == IIO_NETWORK)
Expand Down
Loading

0 comments on commit 9a00766

Please sign in to comment.