From 29ca7fd0b14cd60f01eca27b5718db4db0fbe92c Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Wed, 6 Apr 2022 10:25:56 -0400 Subject: [PATCH 1/4] iio_strndup : fix off by one error When debugging some weirdness between the windows code and linux code running mds, I noticed that iio_strndup() was behaving differently. :( That is because the local implmentation for Windows was wrong. Checking against: https://github.com/gcc-mirror/gcc/blob/master/libiberty/strndup.c (Which we can't use directly since it is GPL3, and not compatible with LGPL), and checking against (which is BSD): https://opensource.apple.com/source/Libc/Libc-1158.30.7/string/FreeBSD/strndup.c.auto.html we can see the strlen we should be using is n, not n+1, which is what it was before. :( So, fix it. Signed-off-by: Robin Getz --- utilities.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utilities.c b/utilities.c index 87b7f516e..5ada6ad34 100644 --- a/utilities.c +++ b/utilities.c @@ -247,7 +247,7 @@ char *iio_strndup(const char *str, size_t n) #ifdef HAS_STRNDUP return strndup(str, n); #else - size_t len = strnlen(str, n + 1); + size_t len = strnlen(str, n); char *buf = malloc(len + 1); if (buf) { /* len = size of buf, so memcpy is OK */ From 391f44aec402d140d3acfee6a5a33288b67fb7e3 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Wed, 6 Apr 2022 10:43:16 -0400 Subject: [PATCH 2/4] CMake: add debug options to windows builds gcc and clang always build with '-g', enabling easier debugging, so catch MSVC to the same. This will create seperate program database (.pdb) files, which map identifiers and statements in the project's source code to corresponding identifiers and instructions in compiled apps. These mapping files link the debugger to your source code, which enables debugging. Hopefully this will put a stop to those who want to build in windows in debug mode (which is not possible, see #772) This does not include the pdb files in the installers, so the only way to get them is build locally. Signed-off-by: Robin Getz --- CMakeLists.txt | 6 +++++- tests/CMakeLists.txt | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e65c945e7..86294ddb9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,7 +67,9 @@ if (NOT LOG_LEVEL) endif() if (MSVC) - add_compile_options(/W4 /wd4200 /wd4127 /wd4100) + add_compile_options(/Zi /W4 /wd4200 /wd4127 /wd4100) + # Zi produces a separate PDB file that contains all the symbolic debugging information + # W4 displays level 1, 2, 3, and 4 (informational) warnings that aren't off by default # C4200: nonstandard extension used : zero-sized array in struct (usb.h) # C4127: conditional expression is constant (IIO_ERROR and IIO_DEBUG macros) # C4100: unreferenced parameter; same as -Wno-unused-parameter @@ -474,6 +476,8 @@ target_link_libraries(iio LINK_PRIVATE ${LIBS_TO_LINK}) if (MSVC) set_target_properties(iio PROPERTIES OUTPUT_NAME libiio) + target_link_options(iio PUBLIC /DEBUG) + # The linker puts debugging information into a program database (PDB) file endif() if(NOT SKIP_INSTALL_ALL) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f819a2b10..46ac38e38 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -37,6 +37,16 @@ add_executable(iio_readdev iio_readdev.c ${GETOPT_C_FILE} ${LIBIIO_RC}) add_executable(iio_reg iio_reg.c ${GETOPT_C_FILE} ${LIBIIO_RC}) add_executable(iio_writedev iio_writedev.c ${GETOPT_C_FILE} ${LIBIIO_RC}) +if (MSVC) + target_link_options(iio_tests_helper PUBLIC /DEBUG) + target_link_options(iio_genxml PUBLIC /DEBUG) + target_link_options(iio_info PUBLIC /DEBUG) + target_link_options(iio_attr PUBLIC /DEBUG) + target_link_options(iio_readdev PUBLIC /DEBUG) + target_link_options(iio_reg PUBLIC /DEBUG) + target_link_options(iio_writedev PUBLIC /DEBUG) +endif() + target_link_libraries(iio_genxml iio iio_tests_helper) target_link_libraries(iio_info iio iio_tests_helper) target_link_libraries(iio_attr iio iio_tests_helper) From 47381e2dd067d3fc5cfd79323c59fdcc66e65603 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Wed, 6 Apr 2022 15:50:28 +0100 Subject: [PATCH 3/4] tests: Fix build under MinGW MinGW 32-bit does not have timespec_get(). Strangely, the 64-bit version does have it. But both have clock_gettime(), so only use timespec_get() when building under Visual Studio. Fixes #830. Signed-off-by: Paul Cercueil --- tests/iio_common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/iio_common.c b/tests/iio_common.c index a5b90f2a4..e88c093e8 100644 --- a/tests/iio_common.c +++ b/tests/iio_common.c @@ -451,7 +451,7 @@ uint64_t get_time_us(void) { struct timespec tp; -#ifdef _WIN32 +#ifdef _MSC_BUILD timespec_get(&tp, TIME_UTC); #else clock_gettime(CLOCK_REALTIME, &tp); From b1099c96c9ac16d468995a65b79474ba03de305c Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Wed, 6 Apr 2022 18:18:46 -0400 Subject: [PATCH 4/4] Use FQDN_LEN instead of MAXHOSTNAMELEN for DNS names As defined in nbase.h (or in some system header file on Linux), MAXHOSTNAMELEN is 64. This is the maximum length of a hostname, but not of a DNS name, which may be longer. RFC 1035 spells out that DNS names are formed of labels, each of which must be 63 bytes or less, and that labels are combined to form a DNS name (a.k.a. FQDN) which may not exceed 255 bytes. So add and use that, which fixed a long name issue for me. Signed-off-by: Robin Getz --- dns_sd.c | 2 +- dns_sd.h | 4 +--- dns_sd_bonjour.c | 4 ++-- network-windows.c | 3 --- network.c | 2 +- utilities.c | 2 +- 6 files changed, 6 insertions(+), 11 deletions(-) diff --git a/dns_sd.c b/dns_sd.c index cc807944a..1d26375d5 100644 --- a/dns_sd.c +++ b/dns_sd.c @@ -81,7 +81,7 @@ static int dnssd_fill_context_info(struct iio_context_info *info, char *hostname, char *addr_str, uint16_t port) { struct iio_context *ctx; - char uri[sizeof("ip:") + MAXHOSTNAMELEN + sizeof (":65535") + 1]; + char uri[sizeof("ip:") + FQDN_LEN + sizeof (":65535") + 1]; char description[255], *p; const char *hw_model, *serial; unsigned int i; diff --git a/dns_sd.h b/dns_sd.h index 3fb370b34..7906837e2 100644 --- a/dns_sd.h +++ b/dns_sd.h @@ -15,14 +15,12 @@ #ifdef _WIN32 #include -#ifndef MAXHOSTNAMELEN -#define MAXHOSTNAMELEN (MAX_COMPUTERNAME_LENGTH+1) -#endif /* MAXHOSTNAMELEN */ #else #include #endif #define DNS_SD_ADDRESS_STR_MAX (40) /* IPv6 Max = 4*8 + 7 + 1 for NUL */ +#define FQDN_LEN (255) /* RFC 1035 */ /* MacOS doesn't include ENOMEDIUM (No medium found) like Linux does */ #ifndef ENOMEDIUM diff --git a/dns_sd_bonjour.c b/dns_sd_bonjour.c index 18779836f..2d7ed5e11 100644 --- a/dns_sd_bonjour.c +++ b/dns_sd_bonjour.c @@ -30,8 +30,8 @@ static void __cfnet_browser_cb(CFNetServiceBrowserRef browser, struct dns_sd_discovery_data *dd = info; char address_v4[DNS_SD_ADDRESS_STR_MAX+1] = ""; char address_v6[DNS_SD_ADDRESS_STR_MAX+1] = ""; - char hostname[MAXHOSTNAMELEN]; - char name[MAXHOSTNAMELEN]; + char hostname[FQDN_LEN]; + char name[FQDN_LEN]; bool have_v4 = false; bool have_v6 = false; struct sockaddr_in *sa; diff --git a/network-windows.c b/network-windows.c index 5cfb22e39..b3f689544 100644 --- a/network-windows.c +++ b/network-windows.c @@ -11,9 +11,6 @@ #include #include #define close(s) closesocket(s) -#ifndef MAXHOSTNAMELEN -#define MAXHOSTNAMELEN (MAX_COMPUTERNAME_LENGTH+1) -#endif /* MAXHOSTNAMELEN */ int set_blocking_mode(int s, bool blocking) { diff --git a/network.c b/network.c index cdedf5e9b..03cb09f62 100644 --- a/network.c +++ b/network.c @@ -1218,7 +1218,7 @@ struct iio_context * network_create_context(const char *host) uri_len = strlen(description); if (host && host[0]) - uri_len = strnlen(host, MAXHOSTNAMELEN); + uri_len = strnlen(host, FQDN_LEN); uri_len += sizeof ("ip:"); uri = malloc(uri_len); diff --git a/utilities.c b/utilities.c index 5ada6ad34..71787395d 100644 --- a/utilities.c +++ b/utilities.c @@ -321,7 +321,7 @@ char * iio_getenv (char * envvar) if (!hostname) return NULL; - tmp = MAXHOSTNAMELEN + sizeof("serial:") + sizeof(":65535") - 2; + tmp = FQDN_LEN + sizeof("serial:") + sizeof(":65535") - 2; len = strnlen(hostname, tmp); /* Should be smaller than max length */