diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5854b64..e6b23df 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -42,8 +42,6 @@ set(CMAKE_PROJECT_NAME_FULL "Cyclone DDS") string(REPLACE " " "" PROJECT_NAME "${CMAKE_PROJECT_NAME_FULL}") project(${PROJECT_NAME} VERSION 0.1.0) -find_package(Abstraction REQUIRED) - # Set some convenience variants of the project-name string(REPLACE " " "-" CMAKE_PROJECT_NAME_DASHED "${CMAKE_PROJECT_NAME_FULL}") string(TOUPPER ${CMAKE_PROJECT_NAME} CMAKE_PROJECT_NAME_CAPS) diff --git a/src/cmake/modules/FindAbstraction.cmake b/src/cmake/modules/FindAbstraction.cmake deleted file mode 100644 index e22ddae..0000000 --- a/src/cmake/modules/FindAbstraction.cmake +++ /dev/null @@ -1,42 +0,0 @@ -# -# Copyright(c) 2006 to 2018 ADLINK Technology Limited and others -# -# This program and the accompanying materials are made available under the -# terms of the Eclipse Public License v. 2.0 which is available at -# http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License -# v. 1.0 which is available at -# http://www.eclipse.org/org/documents/edl-v10.php. -# -# SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause -# - -if (NOT TARGET Abstraction) - add_library(Abstraction INTERFACE) -endif() - -# Link with the platform-specific threads library that find_package provides us -set(CMAKE_THREAD_PREFER_PTHREAD TRUE) -find_package(Threads REQUIRED) -target_link_libraries(Abstraction INTERFACE Threads::Threads) - -if(WIN32) - # Link with WIN32 core-libraries - target_link_libraries(Abstraction INTERFACE wsock32 ws2_32 iphlpapi) - - # Many of the secure versions provided by Microsoft have failure modes - # which are not supported by our abstraction layer, so efforts trying - # to use the _s versions aren't typically the proper solution and C11 - # (which contains most of the secure versions) is 'too new'. So we rely - # on static detection of misuse instead of runtime detection, so all - # these warnings can be disabled on Windows. - add_definitions(-D_CRT_SECURE_NO_WARNINGS) - add_definitions(-D_WINSOCK_DEPRECATED_NO_WARNINGS) #Disabled warnings for deprecated Winsock 2 API calls in general - add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE) #Disabled warnings for deprecated POSIX names -elseif(UNIX AND NOT APPLE) - if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64") - # Shared libs will have this by default. Static libs need it too on x86_64. - set(CMAKE_POSITION_INDEPENDENT_CODE ON) - endif() - find_package(GetTime REQUIRED) - target_link_libraries(Abstraction INTERFACE GetTime) -endif() diff --git a/src/cmake/modules/FindGetTime.cmake b/src/cmake/modules/FindGetTime.cmake deleted file mode 100644 index 566db04..0000000 --- a/src/cmake/modules/FindGetTime.cmake +++ /dev/null @@ -1,28 +0,0 @@ -# -# Copyright(c) 2006 to 2018 ADLINK Technology Limited and others -# -# This program and the accompanying materials are made available under the -# terms of the Eclipse Public License v. 2.0 which is available at -# http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License -# v. 1.0 which is available at -# http://www.eclipse.org/org/documents/edl-v10.php. -# -# SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause -# -if (NOT TARGET GetTime) - add_library(GetTime INTERFACE) -endif() - -include(CheckLibraryExists) - -# First check whether libc has clock_gettime -check_library_exists(c clock_gettime "" HAVE_CLOCK_GETTIME_IN_C) - -if(NOT HAVE_CLOCK_GETTIME_IN_C) - # Before glibc 2.17, clock_gettime was in librt - check_library_exists(rt clock_gettime "time.h" HAVE_CLOCK_GETTIME_IN_RT) - if (HAVE_CLOCK_GETTIME_IN_RT) - target_link_libraries(GetTime INTERFACE rt) - endif() -endif() - diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 2493cf9..fd87477 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -9,8 +9,6 @@ # # SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause # -find_package(Abstraction REQUIRED) - include (GenerateExportHeader) FUNCTION(PREPEND var prefix) diff --git a/src/core/ddsc/include/ddsc/dds.h b/src/core/ddsc/include/ddsc/dds.h index c7b1ac7..ec57ce8 100644 --- a/src/core/ddsc/include/ddsc/dds.h +++ b/src/core/ddsc/include/ddsc/dds.h @@ -537,8 +537,8 @@ dds_get_status_mask( _Out_ uint32_t *mask); _Pre_satisfies_(entity & DDS_ENTITY_KIND_MASK) -DDS_EXPORT _Check_return_ dds_return_t -DDS_DEPRECATED_EXPORT dds_get_enabled_status( +DDS_DEPRECATED_EXPORT _Check_return_ dds_return_t +dds_get_enabled_status( _In_ dds_entity_t entity, _Out_ uint32_t *mask); @@ -568,8 +568,7 @@ dds_set_status_mask( _In_ uint32_t mask); _Pre_satisfies_(entity & DDS_ENTITY_KIND_MASK) -DDS_EXPORT dds_return_t -DDS_DEPRECATED_EXPORT +DDS_DEPRECATED_EXPORT dds_return_t dds_set_enabled_status( _In_ dds_entity_t entity, _In_ uint32_t mask); @@ -1115,8 +1114,7 @@ dds_set_topic_filter( dds_topic_filter_fn filter); _Pre_satisfies_((topic & DDS_ENTITY_KIND_MASK) == DDS_KIND_TOPIC) -DDS_EXPORT void -DDS_DEPRECATED_EXPORT +DDS_DEPRECATED_EXPORT void dds_topic_set_filter( dds_entity_t topic, dds_topic_filter_fn filter); @@ -1134,8 +1132,7 @@ dds_get_topic_filter( dds_entity_t topic); _Pre_satisfies_((topic & DDS_ENTITY_KIND_MASK) == DDS_KIND_TOPIC) -DDS_EXPORT dds_topic_filter_fn -DDS_DEPRECATED_EXPORT +DDS_DEPRECATED_EXPORT dds_topic_filter_fn dds_topic_get_filter( dds_entity_t topic); @@ -3145,8 +3142,7 @@ dds_lookup_instance( const void *data); _Pre_satisfies_(entity & DDS_ENTITY_KIND_MASK) -DDS_EXPORT dds_instance_handle_t -DDS_DEPRECATED_EXPORT +DDS_DEPRECATED_EXPORT dds_instance_handle_t dds_instance_lookup ( dds_entity_t entity, const void *data); diff --git a/src/core/ddsc/src/dds_qos.c b/src/core/ddsc/src/dds_qos.c index 0e169fa..caa65dc 100644 --- a/src/core/ddsc/src/dds_qos.c +++ b/src/core/ddsc/src/dds_qos.c @@ -533,7 +533,6 @@ void dds_qset_partition ) { uint32_t i; - size_t len; if(!qos) { DDS_ERROR("Argument qos may not be NULL\n"); @@ -558,9 +557,7 @@ void dds_qset_partition } for (i = 0; i < n; i++) { - len = strlen (ps[i]) + 1; - qos->partition.strs[i] = dds_alloc (len); - strncpy (qos->partition.strs[i], ps[i], len); + qos->partition.strs[i] = dds_string_dup (ps[i]); } qos->present |= QP_PARTITION; } diff --git a/src/core/ddsc/src/dds_topic.c b/src/core/ddsc/src/dds_topic.c index 10c5c0b..00c2194 100644 --- a/src/core/ddsc/src/dds_topic.c +++ b/src/core/ddsc/src/dds_topic.c @@ -458,6 +458,7 @@ dds_create_topic( nn_plist_t plist; dds_entity_t hdl; uint32_t index; + size_t keysz; if (desc == NULL){ DDS_ERROR("Topic description is NULL"); @@ -478,10 +479,9 @@ dds_create_topic( } typename = desc->m_typename; - key = (char*) dds_alloc (strlen (name) + strlen (typename) + 2); - strcpy (key, name); - strcat (key, "/"); - strcat (key, typename); + keysz = strlen (name) + strlen (typename) + 2; + key = (char*) dds_alloc (keysz); + (void) snprintf(key, keysz, "%s/%s", name, typename); st = dds_alloc (sizeof (*st)); @@ -490,10 +490,8 @@ dds_create_topic( st->c.status_cb = dds_topic_status_cb; st->c.status_cb_entity = NULL; /* set by dds_create_topic_arbitrary */ st->c.name_typename = key; - st->c.name = dds_alloc (strlen (name) + 1); - strcpy (st->c.name, name); - st->c.typename = dds_alloc (strlen (typename) + 1); - strcpy (st->c.typename, typename); + st->c.name = dds_string_dup (name); + st->c.typename = dds_string_dup (typename); st->c.ops = &ddsi_sertopic_ops_default; st->c.serdata_ops = desc->m_nkeys ? &ddsi_serdata_ops_cdr : &ddsi_serdata_ops_cdr_nokey; st->c.serdata_basehash = ddsi_sertopic_compute_serdata_basehash (st->c.serdata_ops); diff --git a/src/core/ddsc/tests/config.c b/src/core/ddsc/tests/config.c index 92e3a4f..2bd3e49 100644 --- a/src/core/ddsc/tests/config.c +++ b/src/core/ddsc/tests/config.c @@ -45,9 +45,10 @@ static void config__check_env( if ( !env_ok ) { os_result r; char *envstr; + size_t len = strlen(env_variable) + strlen("=") + strlen(expected_value) + 1; - envstr = os_malloc(strlen(env_variable) + strlen("=") + strlen(expected_value) + 1); - (void) sprintf(envstr, "%s=%s", env_variable, expected_value); + envstr = os_malloc(len); + (void)snprintf(envstr, len, "%s=%s", env_variable, expected_value); r = os_putenv(envstr); CU_ASSERT_EQUAL_FATAL(r, os_resultSuccess); diff --git a/src/core/ddsc/tests/participant.c b/src/core/ddsc/tests/participant.c index a3ae74f..2a68be0 100644 --- a/src/core/ddsc/tests/participant.c +++ b/src/core/ddsc/tests/participant.c @@ -85,12 +85,12 @@ CU_Test(ddsc_participant, create_with_conf_no_env) { dds_domainid_t valid_domain=3; static char env_uri_str[1000]; - (void) sprintf(env_uri_str, "%s=%s", DDSC_PROJECT_NAME_NOSPACE_CAPS"_URI", CONFIG_ENV_SIMPLE_UDP); + (void) snprintf(env_uri_str, sizeof(env_uri_str), "%s=%s", DDSC_PROJECT_NAME_NOSPACE_CAPS"_URI", CONFIG_ENV_SIMPLE_UDP); os_putenv(env_uri_str); printf("env_uri_str %s\n", env_uri_str); static char env_mp_str[100]; - (void) sprintf(env_mp_str, "%s=%s", "MAX_PARTICIPANTS", CONFIG_ENV_MAX_PARTICIPANTS); + (void) snprintf(env_mp_str, sizeof(env_mp_str), "%s=%s", "MAX_PARTICIPANTS", CONFIG_ENV_MAX_PARTICIPANTS); os_putenv(env_mp_str); const char * env_uri = os_getenv(DDSC_PROJECT_NAME_NOSPACE_CAPS"_URI"); diff --git a/src/core/ddsi/src/ddsi_mcgroup.c b/src/core/ddsi/src/ddsi_mcgroup.c index 999494d..da20312 100644 --- a/src/core/ddsi/src/ddsi_mcgroup.c +++ b/src/core/ddsi/src/ddsi_mcgroup.c @@ -128,16 +128,15 @@ static int unreg_group_membership (struct nn_group_membership *mship, ddsi_tran_ static char *make_joinleave_msg (char *buf, size_t bufsz, ddsi_tran_conn_t conn, int join, const nn_locator_t *srcloc, const nn_locator_t *mcloc, const struct nn_interface *interf, int err) { - char mcstr[DDSI_LOCSTRLEN], srcstr[DDSI_LOCSTRLEN], interfstr[DDSI_LOCSTRLEN]; + char mcstr[DDSI_LOCSTRLEN], interfstr[DDSI_LOCSTRLEN]; + char srcstr[DDSI_LOCSTRLEN] = { '*', '\0' }; int n; #ifdef DDSI_INCLUDE_SSM - if (srcloc) + if (srcloc) { ddsi_locator_to_string_no_port(srcstr, sizeof(srcstr), srcloc); - else - strcpy (srcstr, "*"); + } #else OS_UNUSED_ARG (srcloc); - strcpy (srcstr, "*"); #endif ddsi_locator_to_string_no_port (mcstr, sizeof(mcstr), mcloc); if (interf) diff --git a/src/core/ddsi/src/q_addrset.c b/src/core/ddsi/src/q_addrset.c index 2c03269..a21fba5 100644 --- a/src/core/ddsi/src/q_addrset.c +++ b/src/core/ddsi/src/q_addrset.c @@ -125,6 +125,7 @@ static int add_addresses_to_addrset_1 (struct addrset *as, const char *ip, int p return 0; } +OS_WARNING_MSVC_OFF(4996); int add_addresses_to_addrset (struct addrset *as, const char *addrs, int port_mode, const char *msgtag, int req_mc) { /* port_mode: -1 => take from string, if 0 & unicast, add for a range of participant indices; @@ -177,6 +178,7 @@ int add_addresses_to_addrset (struct addrset *as, const char *addrs, int port_mo os_free (addrs_copy); return retval; } +OS_WARNING_MSVC_ON(4996); int compare_locators (const nn_locator_t *a, const nn_locator_t *b) { diff --git a/src/core/ddsi/src/q_config.c b/src/core/ddsi/src/q_config.c index ca7d13c..365d501 100644 --- a/src/core/ddsi/src/q_config.c +++ b/src/core/ddsi/src/q_config.c @@ -1473,6 +1473,7 @@ static int uf_string(struct cfgst *cfgst, void *parent, struct cfgelem const * c return 1; } +OS_WARNING_MSVC_OFF(4996); static int uf_natint64_unit(struct cfgst *cfgst, int64_t *elem, const char *value, const struct unit *unittab, int64_t def_mult, int64_t max) { int pos; @@ -1502,6 +1503,7 @@ static int uf_natint64_unit(struct cfgst *cfgst, int64_t *elem, const char *valu return cfg_error(cfgst, "%s: invalid value", value); } } +OS_WARNING_MSVC_ON(4996); #ifdef DDSI_INCLUDE_BANDWIDTH_LIMITING static int uf_bandwidth(struct cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem, UNUSED_ARG(int first), const char *value) @@ -1761,6 +1763,7 @@ static void pf_sched_class(struct cfgst *cfgst, void *parent, struct cfgelem con cfg_log(cfgst, "%s%s", str, is_default ? " [def]" : ""); } +OS_WARNING_MSVC_OFF(4996); static int uf_maybe_int32(struct cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem, UNUSED_ARG(int first), const char *value) { struct config_maybe_int32 *elem = cfg_address(cfgst, parent, cfgelem); @@ -1776,6 +1779,7 @@ static int uf_maybe_int32(struct cfgst *cfgst, void *parent, struct cfgelem cons return cfg_error(cfgst, "'%s': neither 'default' nor a decimal integer\n", value); } } +OS_WARNING_MSVC_ON(4996); static int uf_maybe_memsize(struct cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem, UNUSED_ARG(int first), const char *value) { @@ -1902,6 +1906,7 @@ static int uf_int_min_max(struct cfgst *cfgst, void *parent, struct cfgelem cons return 1; } +OS_WARNING_MSVC_OFF(4996); static int uf_domainId(struct cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem, UNUSED_ARG(int first), const char *value) { struct config_maybe_int32 *elem = cfg_address(cfgst, parent, cfgelem); @@ -1917,6 +1922,7 @@ static int uf_domainId(struct cfgst *cfgst, void *parent, struct cfgelem const * return cfg_error(cfgst, "'%s': neither 'any' nor a decimal integer in 0 .. 230\n", value); } } +OS_WARNING_MSVC_ON(4996); static int uf_participantIndex(struct cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem, int first, const char *value) { @@ -2745,6 +2751,7 @@ struct cfgst * config_init struct ut_xmlpState *qx; FILE *fp; + OS_WARNING_MSVC_OFF(4996); if ( (fp = fopen(tok, "r")) == NULL ) { if ( strncmp(tok, "file://", 7) != 0 || (fp = fopen(tok + 7, "r")) == NULL ) { DDS_ERROR("can't open configuration file %s\n", tok); @@ -2753,6 +2760,7 @@ struct cfgst * config_init return NULL; } } + OS_WARNING_MSVC_ON(4996); cb.attr = proc_attr; cb.elem_close = proc_elem_close; diff --git a/src/core/ddsi/src/q_init.c b/src/core/ddsi/src/q_init.c index f32b90a..c7469e2 100644 --- a/src/core/ddsi/src/q_init.c +++ b/src/core/ddsi/src/q_init.c @@ -426,6 +426,7 @@ static int check_thread_properties (void) return ok; } +OS_WARNING_MSVC_OFF(4996); int rtps_config_open (void) { int status; @@ -462,6 +463,7 @@ int rtps_config_open (void) return status; } +OS_WARNING_MSVC_ON(4996); int rtps_config_prep (struct cfgst *cfgst) { diff --git a/src/core/ddsi/src/q_nwif.c b/src/core/ddsi/src/q_nwif.c index f6303b9..d1a4cc3 100644 --- a/src/core/ddsi/src/q_nwif.c +++ b/src/core/ddsi/src/q_nwif.c @@ -481,12 +481,11 @@ int find_own_ip (const char *requested_address) char if_name[sizeof (last_if_name)]; int q = 0; - strncpy (if_name, ifa->name, sizeof (if_name) - 1); - if_name[sizeof (if_name) - 1] = 0; + os_strlcpy(if_name, ifa->name, sizeof(if_name)); if (strcmp (if_name, last_if_name)) DDS_LOG(DDS_LC_CONFIG, "%s%s", sep, if_name); - strcpy (last_if_name, if_name); + os_strlcpy(last_if_name, if_name, sizeof(last_if_name)); /* interface must be up */ if ((ifa->flags & IFF_UP) == 0) { diff --git a/src/core/ddsi/src/q_pcap.c b/src/core/ddsi/src/q_pcap.c index 5ca71c2..7ea5451 100644 --- a/src/core/ddsi/src/q_pcap.c +++ b/src/core/ddsi/src/q_pcap.c @@ -79,6 +79,7 @@ static const ipv4_hdr_t ipv4_hdr_template = { #define IPV4_HDR_SIZE 20 #define UDP_HDR_SIZE 8 +OS_WARNING_MSVC_OFF(4996); FILE *new_pcap_file (const char *name) { FILE *fp; @@ -101,6 +102,7 @@ FILE *new_pcap_file (const char *name) return fp; } +OS_WARNING_MSVC_ON(4996); static void write_data (FILE *fp, const struct msghdr *msghdr, size_t sz) { diff --git a/src/core/ddsi/src/q_radmin.c b/src/core/ddsi/src/q_radmin.c index 94d4683..6afd70c 100644 --- a/src/core/ddsi/src/q_radmin.c +++ b/src/core/ddsi/src/q_radmin.c @@ -2509,6 +2509,7 @@ struct nn_dqueue *nn_dqueue_new (const char *name, uint32_t max_samples, nn_dque { struct nn_dqueue *q; char *thrname; + size_t thrnamesz; if ((q = os_malloc (sizeof (*q))) == NULL) goto fail_q; @@ -2523,9 +2524,10 @@ struct nn_dqueue *nn_dqueue_new (const char *name, uint32_t max_samples, nn_dque os_mutexInit (&q->lock); os_condInit (&q->cond, &q->lock); - if ((thrname = os_malloc (3 + strlen (name) + 1)) == NULL) + thrnamesz = 3 + strlen (name) + 1; + if ((thrname = os_malloc (thrnamesz)) == NULL) goto fail_thrname; - sprintf (thrname, "dq.%s", name); + snprintf (thrname, thrnamesz, "dq.%s", name); if ((q->ts = create_thread (thrname, (uint32_t (*) (void *)) dqueue_thread, q)) == NULL) goto fail_thread; os_free (thrname); diff --git a/src/os/CMakeLists.txt b/src/os/CMakeLists.txt index cf713e7..2eee1ae 100644 --- a/src/os/CMakeLists.txt +++ b/src/os/CMakeLists.txt @@ -9,58 +9,115 @@ # # SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause # -string(TOLOWER ${CMAKE_SYSTEM_NAME} platform) +include(CheckCSourceCompiles) +include(CheckLibraryExists) +include(GenerateExportHeader) -set (posix_platforms darwin linux sunos) -IF(${platform} IN_LIST posix_platforms) - set(platform posix) -ENDIF() +set(sources + src/os_atomics.c + src/os_init.c + src/os_log.c + src/os_ifaddrs.c + src/os_socket.c + src/os_thread.c + src/os_time.c + src/os_errno.c + src/os_iter.c + src/os_strlcpy.c) -# For posix platforms include the files in the posix/ directory. -PREPEND(srcs_platform ${platform} os_platform_errno.c os_platform_heap.c os_platform_init.c os_platform_process.c os_platform_ifaddrs.c os_platform_socket.c os_platform_stdlib.c os_platform_sync.c os_platform_thread.c os_platform_time.c) +string(TOLOWER ${CMAKE_SYSTEM_NAME} system_name) +set(system_sources + os_platform_errno.c + os_platform_heap.c + os_platform_ifaddrs.c + os_platform_socket.c + os_platform_stdlib.c + os_platform_sync.c + os_platform_thread.c + os_platform_time.c + os_platform_init.c + os_platform_process.c) -include (GenerateExportHeader) -PREPEND(srcs_os "${CMAKE_CURRENT_SOURCE_DIR}/src" os_atomics.c os_init.c os_log.c os_ifaddrs.c os_socket.c os_thread.c os_time.c os_errno.c os_iter.c ${srcs_platform}) +foreach(source ${system_sources}) + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/${system_name}/${source}") + list(APPEND sources "src/${system_name}/${source}") + else() + list(APPEND sources "src/posix/${source}") + endif() +endforeach() -add_library(OSAPI ${srcs_os}) -configure_file( - "${CMAKE_CURRENT_LIST_DIR}/cmake/os_project.h.in" - "include/os/os_project.h") +add_library(OSAPI ${sources}) + +set(CMAKE_THREAD_PREFER_PTHREAD TRUE) +find_package(Threads REQUIRED) +target_link_libraries(OSAPI INTERFACE Threads::Threads) + +if(WIN32) + # Link with Win32 core-libraries + target_link_libraries(OSAPI INTERFACE wsock32 ws2_32 iphlpapi) + + # Many of the secure versions provided by Microsoft have failure modes + # which are not supported by our abstraction layer, so efforts trying + # to use the _s versions aren't typically the proper solution and C11 + # (which contains most of the secure versions) is 'too new'. So we rely + # on static detection of misuse instead of runtime detection, so all + # these warnings can be disabled on Windows. + target_compile_definitions(OSAPI PRIVATE _CRT_SECURE_NO_WARNINGS) + # Disable warnings for deprecated Winsock API calls in general. + target_compile_definitions(OSAPI PRIVATE _WINSOCK_DEPRECATED_NO_WARNINGS) + # Disable warnings for deprecated POSIX names. + target_compile_definitions(OSAPI PRIVATE -D_CRT_NONSTDC_NO_DEPRECATE) +elseif(UNIX AND NOT APPLE) + if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64") + # Shared libs will have this by default. Static libs need it too on x86_64. + set_property(TARGET OSAPI PROPERTY POSITION_INDEPENDENT_CODE TRUE) + endif() + + check_library_exists(c clock_gettime "" HAVE_CLOCK_GETTIME) + if(NOT HAVE_CLOCK_GETTIME) + # Before glibc 2.17, clock_gettime was in librt. + check_library_exists(rt clock_gettime "time.h" HAVE_CLOCK_GETTIME) + if(HAVE_CLOCK_GETTIME) + target_link_libraries(OSAPI INTERFACE rt) + endif() + endif() + + if(NOT HAVE_CLOCK_GETTIME) + message(FATAL_ERROR "clock_gettime is not available") + endif() +endif() + +if(${CMAKE_C_COMPILER_ID} STREQUAL "SunPro") + target_link_libraries(OSAPI INTERFACE -lsocket -lnsl) + target_compile_definitions(OSAPI PRIVATE -KPIC) +endif() + +configure_file("cmake/os_project.h.in" "include/os/os_project.h") +target_sources(OSAPI PRIVATE "include/os/os_project.h") generate_export_header(OSAPI EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/exports/os/osapi_export.h") -target_link_libraries(OSAPI INTERFACE Abstraction) -target_include_directories(OSAPI PUBLIC "$" "$" - "$" "$") -target_sources(OSAPI PRIVATE "include/os/os_project.h") -target_include_directories(OSAPI - PUBLIC - "$") + +target_include_directories( + OSAPI PUBLIC + "$" + "$" + "$" + "$" + "$") if(BUILD_TESTING) add_subdirectory(tests) endif() -if(${CMAKE_C_COMPILER_ID} STREQUAL "SunPro") - target_link_libraries(OSAPI INTERFACE -lsocket -lnsl) - add_definitions(-KPIC) -endif() - install( - FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_public.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_decl_attributes.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_decl_attributes_sal.h" - DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/ddsc/os" - COMPONENT dev) - -# Currently, only windows and posix platforms are supported. -IF(WIN32 AND NOT UNIX) - install( - FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/os/windows/os_platform_public.h" - DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/ddsc/os" - COMPONENT dev) -ELSE() - install( - FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/os/posix/os_platform_public.h" - DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/ddsc/os" - COMPONENT dev) -ENDIF() + FILES + "${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_public.h" + "${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_decl_attributes.h" + "${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_decl_attributes_sal.h" + "${CMAKE_CURRENT_SOURCE_DIR}/include/os/${system_name}/os_platform_public.h" + DESTINATION + "${CMAKE_INSTALL_INCLUDEDIR}/ddsc/os" + COMPONENT + dev) diff --git a/src/os/include/os/os.h b/src/os/include/os/os.h index 70d8313..8358fef 100644 --- a/src/os/include/os/os.h +++ b/src/os/include/os/os.h @@ -54,6 +54,7 @@ #include "os_errno.h" #include "os_iter.h" #include "os_log.h" +#include "os_strlcpy.h" #define OSPL_VERSION_STR "aap" #define OSPL_HOST_STR "noot" diff --git a/src/os/include/os/os_strlcpy.h b/src/os/include/os/os_strlcpy.h new file mode 100644 index 0000000..4700ef5 --- /dev/null +++ b/src/os/include/os/os_strlcpy.h @@ -0,0 +1,72 @@ +/* + * Copyright(c) 2006 to 2018 ADLINK Technology Limited and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License + * v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ +#ifndef OS_STRLCPY_H +#define OS_STRLCPY_H + +#include "os/os_defs.h" + +#if defined (__cplusplus) +extern "C" { +#endif + +/** + * @brief Copy string. + * + * Copy string to buffer with specified size. The string is truncated if there + * is not enough space. The resulting string is guaranteed to be null + * terminated if there is space. + * + * @param[out] dest Destination buffer. + * @param[in] src Null terminated string to copy to dest. + * @param[in] size Number of bytes available in dest. + * + * @returns Number of characters copied to dest (excluding the null byte), or + * the number of characters that would have been copied if dest is not + * sufficiently large enough. + */ +_Success_(return < size) +OSAPI_EXPORT +size_t +os_strlcpy( + _Out_writes_z_(size) char * __restrict dest, + _In_z_ const char * __restrict src, + _In_ size_t size); + +/** + * @brief Concatenate strings. + * + * Append the string specified by src to the string specified by dest. The + * terminating null byte at the end of dest is overwritten. The resulting + * string is truncated if there is not enough space. The resulting string + * guaranteed to be null terminated if there is space. + * + * @param[inout] dest Destination buffer. + * @param[in] src Null terminated string to append to dest. + * @param[in] size Number of bytes available in dest. + * + * @returns Number of characters copied to dest (excluding the null byte), or + * the number of characters that would have been copied if dest is not + * sufficiently large enough. + */ +_Success_(return < size) +OSAPI_EXPORT +size_t +os_strlcat( + _Inout_updates_z_(size) char * __restrict dest, + _In_z_ const char * __restrict src, + _In_ size_t size); + +#if defined (__cplusplus) +} +#endif + +#endif /* OS_STRLCPY_H */ diff --git a/src/os/src/os_strlcpy.c b/src/os/src/os_strlcpy.c new file mode 100644 index 0000000..39cadf3 --- /dev/null +++ b/src/os/src/os_strlcpy.c @@ -0,0 +1,80 @@ +/* + * Copyright(c) 2006 to 2018 ADLINK Technology Limited and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License + * v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ +#include +#include + +#include "os/os.h" + +_Success_(return < size) +size_t +os_strlcpy( + _Out_writes_z_(size) char * __restrict dest, + _In_z_ const char * __restrict src, + _In_ size_t size) +{ + size_t srclen = 0; + + assert(dest != NULL); + assert(src != NULL); + + /* strlcpy must return the number of bytes that (would) have been written, + i.e. the length of src. */ + srclen = strlen(src); + if (size > 0) { + size_t len = srclen; + if (size <= srclen) { + len = size - 1; + } + memcpy(dest, src, len); + dest[len] = '\0'; + } + + return srclen; +} + +/* NOTE: os_strlcat does not forward to strlcat too avoid a bug in the macOS + implementation where it does not return the right result if dest + contains more characters than the size specified if size is either + 0 or 1. */ +_Success_(return < size) +size_t +os_strlcat( + _Inout_updates_z_(size) char * __restrict dest, + _In_z_ const char * __restrict src, + _In_ size_t size) +{ + size_t destlen, srclen; + + assert(dest != NULL); + assert(src != NULL); + + /* strlcat must return the number of bytes that (would) have been written, + i.e. the length of dest plus the length of src. */ + destlen = strlen(dest); + srclen = strlen(src); + if (SIZE_MAX == destlen) { + srclen = 0; + } else if ((SIZE_MAX - destlen) <= srclen) { + srclen = (SIZE_MAX - destlen) - 1; + } + if (size > 0 && --size > destlen) { + size_t len = srclen; + size -= destlen; + if (size <= srclen) { + len = size; + } + memcpy(dest + destlen, src, len); + dest[destlen + len] = '\0'; + } + + return destlen + srclen; +} diff --git a/src/os/src/snippets/code/os_gethostname.c b/src/os/src/snippets/code/os_gethostname.c index c905cee..5a49654 100644 --- a/src/os/src/snippets/code/os_gethostname.c +++ b/src/os/src/snippets/code/os_gethostname.c @@ -11,6 +11,8 @@ */ #include +#include "os/os.h" + os_result os_gethostname( char *hostname, @@ -23,7 +25,7 @@ os_gethostname( if ((strlen(hostnamebuf)+1) > buffersize) { result = os_resultFail; } else { - strncpy (hostname, hostnamebuf, buffersize); + os_strlcpy (hostname, hostnamebuf, buffersize); result = os_resultSuccess; } } else { diff --git a/src/os/tests/CMakeLists.txt b/src/os/tests/CMakeLists.txt index 93868df..8ac99bd 100644 --- a/src/os/tests/CMakeLists.txt +++ b/src/os/tests/CMakeLists.txt @@ -25,8 +25,9 @@ set(sources "thread.c" "thread_cleanup.c" "strcasecmp.c" - "log.c") + "log.c" + "strlcpy.c") -add_cunit_executable(abstraction ${sources}) -target_link_libraries(abstraction OSAPI) +add_cunit_executable(cunit_abstraction ${sources}) +target_link_libraries(cunit_abstraction OSAPI) diff --git a/src/os/tests/log.c b/src/os/tests/log.c index 8d3b6ee..fce6947 100644 --- a/src/os/tests/log.c +++ b/src/os/tests/log.c @@ -81,6 +81,7 @@ FILE *fmemopen(void *buf, size_t size, const char *mode) if (err) { errno = err; } else { + OS_WARNING_MSVC_OFF(4996); if ((fd = _open_osfhandle((intptr_t)hdl, _O_APPEND)) == -1) { /* errno set by _open_osfhandle. */ CloseHandle(hdl); @@ -90,6 +91,7 @@ FILE *fmemopen(void *buf, size_t size, const char *mode) } else { return fh; } + OS_WARNING_MSVC_ON(4996); } return NULL; diff --git a/src/os/tests/rwlock.c b/src/os/tests/rwlock.c index 7b8f5ea..01b12ad 100644 --- a/src/os/tests/rwlock.c +++ b/src/os/tests/rwlock.c @@ -355,7 +355,7 @@ CU_Test(os_rwlock, read, false) printf ("concurrent_tryread_access = %d\n", sd.concurrent_tryread_access); printf ("concurrent_trywrite_access = %d\n", sd.concurrent_trywrite_access); - sprintf (buffer, "Corrupt counter = %d, Loop counter is %d", + snprintf (buffer, sizeof(buffer), "Corrupt counter = %d, Loop counter is %d", sd.read_corrupt_count + sd.write_corrupt_count + sd.tryread_corrupt_count + sd.trywrite_corrupt_count, sd.concurrent_read_access + sd.concurrent_write_access + sd.concurrent_tryread_access + sd.concurrent_trywrite_access); @@ -419,7 +419,7 @@ CU_Test(os_rwlock, read, false) par[i].read_access, par[i].concurrent_read_access, i); } - sprintf (buffer, "Corrupt read counter = %d, Read loop counter is %d", sd.read_corrupt_count, sd.concurrent_read_access); + snprintf (buffer, sizeof(buffer), "Corrupt read counter = %d, Read loop counter is %d", sd.read_corrupt_count, sd.concurrent_read_access); CU_ASSERT (sd.read_corrupt_count == 0 && sd.concurrent_read_access > 0); /* Test read on rwlock with PRIVATE scope and Success result & not locked */ @@ -443,7 +443,7 @@ CU_Test(os_rwlock, write, false) /* Test critical section WRITE access with locking and PRIVATE scope */ printf ("Starting os_rwlock_write_001\n"); - sprintf (buffer, "Corrupt write counter = %d, Write loop counter is %d", sd.write_corrupt_count, sd.concurrent_write_access); + snprintf (buffer, sizeof(buffer), "Corrupt write counter = %d, Write loop counter is %d", sd.write_corrupt_count, sd.concurrent_write_access); CU_ASSERT (sd.write_corrupt_count == 0 && sd.concurrent_write_access > 0); /* Test write on rwlock with PRIVATE scope and Success result */ @@ -464,7 +464,7 @@ CU_Test(rwlock, tryread, false) /* Test critical section READ access with trylocking and PRIVATE scope */ printf ("Starting os_rwlock_tryread_001\n"); - sprintf (buffer, "Corrupt tryread counter = %d, Tryread loop counter is %d, Busy counter = %d", sd.tryread_corrupt_count, sd.concurrent_tryread_access, sd.tryread_busy_count); + snprintf (buffer, sizeof(buffer), "Corrupt tryread counter = %d, Tryread loop counter is %d, Busy counter = %d", sd.tryread_corrupt_count, sd.concurrent_tryread_access, sd.tryread_busy_count); CU_ASSERT (sd.tryread_corrupt_count == 0 && sd.concurrent_tryread_access > 0); /* Test try read on rwlock with PRIVATE scope and Success result & not locked */ @@ -494,7 +494,7 @@ CU_Test(os_rwlock, trywrite, false) /* Test critical section WRITE access with trylocking and PRIVATE scope */ printf ("Starting os_rwlock_trywrite_001\n"); - sprintf (buffer, "Corrupt trywrite counter = %d, Trywrite loop counter is %d, Busy counter = %d", sd.trywrite_corrupt_count, sd.concurrent_trywrite_access, sd.trywrite_busy_count); + snprintf (buffer, sizeof(buffer), "Corrupt trywrite counter = %d, Trywrite loop counter is %d, Busy counter = %d", sd.trywrite_corrupt_count, sd.concurrent_trywrite_access, sd.trywrite_busy_count); CU_ASSERT (sd.trywrite_corrupt_count == 0 && sd.concurrent_trywrite_access > 0); /* Test try write on rwlock with PRIVATE scope and Success result */ diff --git a/src/os/tests/strlcpy.c b/src/os/tests/strlcpy.c new file mode 100644 index 0000000..a2c8257 --- /dev/null +++ b/src/os/tests/strlcpy.c @@ -0,0 +1,80 @@ +/* + * Copyright(c) 2006 to 2018 ADLINK Technology Limited and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License + * v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ +#include +#include + +#include "os/os.h" +#include "CUnit/Theory.h" + +CU_TheoryDataPoints(os_strlcpy, dest_size) = { + CU_DataPoints(char *, "foo", "foo", "foo", "foo", "foo", "", "", ""), + CU_DataPoints(size_t, 0, 1, 3, 4, 5, 0, 1, 2) +}; + +CU_Theory((char *src, size_t size), os_strlcpy, dest_size) +{ + char dest[] = "................"; + size_t len, srclen; + + srclen = strlen(src); + len = os_strlcpy(dest, src, size); + CU_ASSERT_EQUAL(len, srclen); + if (size > 0) { + if ((size - 1) < len) { + len = size - 1; + } + CU_ASSERT_EQUAL(dest[len], '\0'); + CU_ASSERT_EQUAL(dest[len+1], '.'); + CU_ASSERT((strncmp(dest, src, len) == 0)); + } else { + CU_ASSERT_EQUAL(dest[0], '.'); + } +} + +CU_TheoryDataPoints(os_strlcat, dest_size) = { + CU_DataPoints(char *, "", "", "", "", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "", "", "foo", "foo", "foo"), + CU_DataPoints(char *, "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "", "", "", "", ""), + CU_DataPoints(size_t, 0, 1, 3, 4, 0, 1, 3, 4, 5, 6, 7, 0, 1, 3, 4, 5) +}; + +CU_Theory((char *seed, char *src, size_t size), os_strlcat, dest_size) +{ + char dest[] = "................"; + size_t len, seedlen, srclen; + seedlen = strlen(seed); + srclen = strlen(src); + memcpy(dest, seed, seedlen); + dest[seedlen] = '\0'; + + len = os_strlcat(dest, src, size); + CU_ASSERT_EQUAL(len, (seedlen + srclen)); + if (size > 0) { + char foobar[sizeof(dest)]; + + if ((size - 1) <= seedlen) { + len = seedlen; + } else if ((size - 1) <= len) { + len = size - 1; + } + + CU_ASSERT_EQUAL(dest[len], '\0'); + + if (seedlen < (size - 1)) { + CU_ASSERT_EQUAL(dest[len+1], '.'); + } + + (void)snprintf(foobar, len+1, "%s%s", seed, src); + CU_ASSERT((strncmp(dest, foobar, len) == 0)); + } else { + CU_ASSERT((strcmp(dest, seed) == 0)); + } +} diff --git a/src/tools/ddsls/CMakeLists.txt b/src/tools/ddsls/CMakeLists.txt index 3890fd0..3c519e3 100644 --- a/src/tools/ddsls/CMakeLists.txt +++ b/src/tools/ddsls/CMakeLists.txt @@ -9,10 +9,11 @@ # # SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause # -find_package(Abstraction REQUIRED) - add_executable(ddsls ddsls.c) target_link_libraries(ddsls ddsc OSAPI) +if(WIN32) + target_compile_definitions(ddsls PRIVATE _CRT_SECURE_NO_WARNINGS) +endif() install( TARGETS ddsls diff --git a/src/tools/pubsub/CMakeLists.txt b/src/tools/pubsub/CMakeLists.txt index 13f6588..230364e 100644 --- a/src/tools/pubsub/CMakeLists.txt +++ b/src/tools/pubsub/CMakeLists.txt @@ -9,14 +9,10 @@ # # SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause # -find_package(Abstraction REQUIRED) - add_executable(pubsub pubsub.c common.c testtype.c porting.c) target_link_libraries(pubsub ddsc OSAPI) - -# TODO: improve test inclusion. -if((BUILD_TESTING) AND ((NOT DEFINED MSVC_VERSION) OR (MSVC_VERSION GREATER "1800"))) - add_subdirectory(tests) +if(WIN32) + target_compile_definitions(pubsub PRIVATE _CRT_SECURE_NO_WARNINGS) endif() install( diff --git a/src/tools/pubsub/common.c b/src/tools/pubsub/common.c old mode 100755 new mode 100644 diff --git a/src/tools/pubsub/common.h b/src/tools/pubsub/common.h old mode 100755 new mode 100644 diff --git a/src/tools/pubsub/porting.c b/src/tools/pubsub/porting.c old mode 100755 new mode 100644 diff --git a/src/tools/pubsub/porting.h b/src/tools/pubsub/porting.h old mode 100755 new mode 100644 diff --git a/src/tools/pubsub/pubsub.c b/src/tools/pubsub/pubsub.c old mode 100755 new mode 100644 diff --git a/src/tools/pubsub/tests/CMakeLists.txt b/src/tools/pubsub/tests/CMakeLists.txt deleted file mode 100644 index 4437526..0000000 --- a/src/tools/pubsub/tests/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -# -# Copyright(c) 2006 to 2018 ADLINK Technology Limited and others -# -# This program and the accompanying materials are made available under the -# terms of the Eclipse Public License v. 2.0 which is available at -# http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License -# v. 1.0 which is available at -# http://www.eclipse.org/org/documents/edl-v10.php. -# -# SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause -# -include(CUnit) - -add_cunit_executable(CUnit_tools_pubsub ../common.c ../testtype.c ../porting.c basic.c) -target_link_libraries(CUnit_tools_pubsub util CycloneDDS::ddsc) \ No newline at end of file diff --git a/src/tools/pubsub/tests/basic.c b/src/tools/pubsub/tests/basic.c deleted file mode 100644 index 9e8d440..0000000 --- a/src/tools/pubsub/tests/basic.c +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright(c) 2006 to 2018 ADLINK Technology Limited and others - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License - * v. 1.0 which is available at - * http://www.eclipse.org/org/documents/edl-v10.php. - * - * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause - */ -#include "CUnit/Test.h" - -#define MAIN test_main -#include "../pubsub.c" - -CU_Test(tools_pubsub, main) { - char *argv[] = {"pubsub", "-T", "pubsubTestTopic", "-K", "KS", "-w1:1", "-D", "1", "-q", "t:d=t,r=r", "pubsub_partition"}; - int argc = sizeof(argv) / sizeof(char*); - - int result = MAIN(argc, argv); - if (result != 0) - printf("exitcode was %d\n", result); - CU_ASSERT_EQUAL_FATAL(result, 0); -} diff --git a/src/tools/pubsub/testtype.c b/src/tools/pubsub/testtype.c old mode 100755 new mode 100644 diff --git a/src/tools/pubsub/testtype.h b/src/tools/pubsub/testtype.h old mode 100755 new mode 100644 diff --git a/src/tools/pubsub/tglib.h b/src/tools/pubsub/tglib.h old mode 100755 new mode 100644 diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index e1e37a7..e9dd89e 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -18,8 +18,18 @@ PREPEND(srcs_util "${CMAKE_CURRENT_SOURCE_DIR}/src" ut_avl.c ut_crc.c ut_expand_ add_library(util ${srcs_util}) generate_export_header(util EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/exports/util/ut_export.h") target_link_libraries(util PUBLIC OSAPI) -target_include_directories(util PUBLIC "$" "$" - "$" "$") + +if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64") + # Shared libs will have this by default. Static libs need it too on x86_64. + set_property(TARGET util PROPERTY POSITION_INDEPENDENT_CODE TRUE) +endif() + +target_include_directories( + util PUBLIC + "$" + "$" + "$" + "$") if(${CMAKE_C_COMPILER_ID} STREQUAL "SunPro") add_definitions(-KPIC) diff --git a/src/util/src/ut_xmlparser.c b/src/util/src/ut_xmlparser.c index 6f1880d..e59d367 100644 --- a/src/util/src/ut_xmlparser.c +++ b/src/util/src/ut_xmlparser.c @@ -281,6 +281,7 @@ static char *unescape_into_utf8 (char *dst, unsigned cp) return dst; } +OS_WARNING_MSVC_OFF(4996); static int unescape_insitu (char *buffer, size_t *n) { const char *src = buffer; @@ -337,6 +338,7 @@ static int unescape_insitu (char *buffer, size_t *n) *n = (size_t) (dst - buffer); return 0; } +OS_WARNING_MSVC_ON(4996); static void discard_payload (struct ut_xmlpState *st) {