From 9a00766877b385f6795151204f4d0f2647901656 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Tue, 28 Apr 2020 23:53:25 -0400 Subject: [PATCH] tests: fix minor issues/errors pointed out by Microsoft Studio 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 --- CMakeLists.txt | 1 + iio-config.h.cmakein | 1 + tests/CMakeLists.txt | 1 + tests/gen_code.c | 45 ++++++++++++++++++++++++++++++++------------ tests/iio_attr.c | 26 ++++++++++++------------- tests/iio_common.c | 27 +++++++++++++++++++++++--- tests/iio_common.h | 12 ++++++++++++ tests/iio_genxml.c | 16 ++++++++++------ tests/iio_info.c | 4 ++-- tests/iio_readdev.c | 16 ++++++++-------- tests/iio_writedev.c | 14 +++++++------- 11 files changed, 111 insertions(+), 52 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a3335913..78596521e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/iio-config.h.cmakein b/iio-config.h.cmakein index bb3f3ed80..92742f2ed 100644 --- a/iio-config.h.cmakein +++ b/iio-config.h.cmakein @@ -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 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 27bcf7795..71fca9ba6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/gen_code.c b/tests/gen_code.c index 7e05b9fdc..bd9e67d2b 100644 --- a/tests/gen_code.c +++ b/tests/gen_code.c @@ -22,6 +22,8 @@ #include #include +#include "iio_common.h" + static FILE *fd = NULL; static char *uri; static enum languages { @@ -30,10 +32,27 @@ 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; @@ -41,12 +60,10 @@ bool gen_test_path(const char *gen_file) 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; @@ -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); @@ -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) { @@ -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); diff --git a/tests/iio_attr.c b/tests/iio_attr.c index 090dcc667..d054efee7 100644 --- a/tests/iio_attr.c +++ b/tests/iio_attr.c @@ -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) @@ -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]; @@ -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); } } @@ -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); } @@ -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); } } @@ -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); } @@ -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); } } @@ -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); } @@ -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); } } @@ -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); } diff --git a/tests/iio_common.c b/tests/iio_common.c index 0930f1db3..b5cc9b76e 100644 --- a/tests/iio_common.c +++ b/tests/iio_common.c @@ -23,9 +23,11 @@ #include #include #include +#include #include "iio_common.h" #include "gen_code.h" +#include "iio-config.h" #ifdef _MSC_BUILD #define inline __inline @@ -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; @@ -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; @@ -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) { @@ -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; } diff --git a/tests/iio_common.h b/tests/iio_common.h index 404ecae25..006c901af 100644 --- a/tests/iio_common.h +++ b/tests/iio_common.h @@ -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 */ diff --git a/tests/iio_genxml.c b/tests/iio_genxml.c index a35420495..21691281d 100644 --- a/tests/iio_genxml.c +++ b/tests/iio_genxml.c @@ -26,10 +26,6 @@ #include "iio_common.h" -#ifndef _WIN32 -#define _strdup strdup -#endif - #define MY_NAME "iio_genxml" static const struct option options[] = { @@ -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:", @@ -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; @@ -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 { diff --git a/tests/iio_info.c b/tests/iio_info.c index f42f5f297..003e5651f 100644 --- a/tests/iio_info.c +++ b/tests/iio_info.c @@ -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) diff --git a/tests/iio_readdev.c b/tests/iio_readdev.c index 311a2246d..fad9bfd1d 100644 --- a/tests/iio_readdev.c +++ b/tests/iio_readdev.c @@ -249,7 +249,7 @@ int main(int argc, char **argv) setup_sig_handler(); if (scan_for_context) - ctx = autodetect_context(true, NULL, MY_NAME); + ctx = autodetect_context(true, false, MY_NAME); else if (arg_uri) ctx = iio_create_context_from_uri(arg_uri); else if (arg_ip) @@ -266,7 +266,7 @@ int main(int argc, char **argv) ret = iio_context_set_timeout(ctx, timeout); if (ret < 0) { char err_str[1024]; - iio_strerror(-ret, err_str, sizeof(err_str)); + iio_strerror(-(int)ret, err_str, sizeof(err_str)); fprintf(stderr, "IIO contexts set timeout failed : %s (%zd)\n", err_str, ret); } @@ -304,7 +304,7 @@ int main(int argc, char **argv) "frequency", DEFAULT_FREQ_HZ); if (ret < 0) { char buf[256]; - iio_strerror(-ret, buf, sizeof(buf)); + iio_strerror(-(int)ret, buf, sizeof(buf)); fprintf(stderr, "sample rate not set : %s (%zd)\n", buf, ret); } @@ -313,7 +313,7 @@ int main(int argc, char **argv) ret = iio_device_set_trigger(dev, trigger); if (ret < 0) { char buf[256]; - iio_strerror(-ret, buf, sizeof(buf)); + iio_strerror(-(int)ret, buf, sizeof(buf)); fprintf(stderr, "set triffer failed : %s (%zd)\n", buf, ret); } @@ -383,11 +383,11 @@ int main(int argc, char **argv) #endif while (app_running) { - int ret = iio_buffer_refill(buffer); + ssize_t ret = iio_buffer_refill(buffer); if (ret < 0) { if (app_running) { char buf[256]; - iio_strerror(-ret, buf, sizeof(buf)); + iio_strerror(-(int)ret, buf, sizeof(buf)); fprintf(stderr, "Unable to refill buffer: %s\n", buf); } break; @@ -421,8 +421,8 @@ int main(int argc, char **argv) ret = iio_buffer_foreach_sample(buffer, print_sample, NULL); if (ret < 0) { char buf[256]; - iio_strerror(-ret, buf, sizeof(buf)); - fprintf(stderr, "buffer processing failed : %s (%d)\n", + iio_strerror(-(int)ret, buf, sizeof(buf)); + fprintf(stderr, "buffer processing failed : %s (%zi)\n", buf, ret); } } diff --git a/tests/iio_writedev.c b/tests/iio_writedev.c index dcdd23020..3eb6ce162 100644 --- a/tests/iio_writedev.c +++ b/tests/iio_writedev.c @@ -263,7 +263,7 @@ int main(int argc, char **argv) setup_sig_handler(); if (scan_for_context) - ctx = autodetect_context(true, NULL, MY_NAME); + ctx = autodetect_context(true, false, MY_NAME); else if (arg_uri) ctx = iio_create_context_from_uri(arg_uri); else if (arg_ip) @@ -280,7 +280,7 @@ int main(int argc, char **argv) ret = iio_context_set_timeout(ctx, timeout); if (ret < 0) { char err_str[1024]; - iio_strerror(-ret, err_str, sizeof(err_str)); + iio_strerror(-(int)ret, err_str, sizeof(err_str)); fprintf(stderr, "IIO contexts set timeout failed : %s (%zd)\n", err_str, ret); } @@ -318,7 +318,7 @@ int main(int argc, char **argv) "frequency", DEFAULT_FREQ_HZ); if (ret < 0) { char buf[256]; - iio_strerror(-ret, buf, sizeof(buf)); + iio_strerror(-(int)ret, buf, sizeof(buf)); fprintf(stderr, "sample rate not set : %s (%zd)\n", buf, ret); } @@ -327,7 +327,7 @@ int main(int argc, char **argv) ret = iio_device_set_trigger(dev, trigger); if (ret < 0) { char buf[256]; - iio_strerror(-ret, buf, sizeof(buf)); + iio_strerror(-(int)ret, buf, sizeof(buf)); fprintf(stderr, "set triffer failed : %s (%zd)\n", buf, ret); } @@ -422,16 +422,16 @@ int main(int argc, char **argv) ret = iio_buffer_foreach_sample(buffer, read_sample, NULL); if (ret < 0) { char buf[256]; - iio_strerror(-ret, buf, sizeof(buf)); + iio_strerror(-(int)ret, buf, sizeof(buf)); fprintf(stderr, "buffer processing failed : %s (%zd)\n", buf, ret); } } - int ret = iio_buffer_push(buffer); + ret = iio_buffer_push(buffer); if (ret < 0) { char buf[256]; - iio_strerror(-ret, buf, sizeof(buf)); + iio_strerror(-(int)ret, buf, sizeof(buf)); fprintf(stderr, "Unable to push buffer: %s\n", buf); break; }