From 902172d4c9b94fe9b16b7fc2d0be4558fd2559d0 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Thu, 10 Dec 2015 18:17:44 -0800 Subject: [PATCH] cppcheck --- rcl/src/rcl/common.c | 2 +- rcl/src/rcl/time_win32.c | 77 --------------------------------- rcl/test/rcl/test_allocator.cpp | 6 +-- rcl/test/test_memory_tools.cpp | 57 ++++++++++++++---------- 4 files changed, 37 insertions(+), 105 deletions(-) diff --git a/rcl/src/rcl/common.c b/rcl/src/rcl/common.c index 563921c..8a22c43 100644 --- a/rcl/src/rcl/common.c +++ b/rcl/src/rcl/common.c @@ -28,7 +28,7 @@ static char __env_buffer[1024]; rcl_ret_t rcl_impl_getenv(const char * env_name, char ** env_value) { - env_value = NULL; + *env_value = NULL; #if !defined(WIN32) *env_value = getenv(env_name); #else diff --git a/rcl/src/rcl/time_win32.c b/rcl/src/rcl/time_win32.c index b57a552..8e43c26 100644 --- a/rcl/src/rcl/time_win32.c +++ b/rcl/src/rcl/time_win32.c @@ -21,96 +21,20 @@ extern "C" { #endif -// Appropriate check accorind to: -// http://man7.org/linux/man-pages/man2/clock_gettime.2.html -#define HAS_CLOCK_GETTIME (_POSIX_C_SOURCE >= 199309L) - #include "rcl/time.h" -#include -#include -#include - -#if defined(__MACH__) -#include -#include -#endif -#if defined(WIN32) #include #include -#endif #include "./common.h" #include "rcl/error_handling.h" -static void -__timespec_get_now_system(struct timespec * timespec_now) -{ -#if defined(__MACH__) - // On OS X use clock_get_time. - clock_serv_t cclock; - mach_timespec_t mts; - host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); - clock_get_time(cclock, &mts); - mach_port_deallocate(mach_task_self(), cclock); - timespec_now->tv_sec = mts.tv_sec; - timespec_now->tv_nsec = mts.tv_nsec; -#else // if defined(__MACH__) - // Otherwise use clock_gettime. - clock_gettime(CLOCK_REALTIME, timespec_now); -#endif // if defined(__MACH__) -} - -static void -__timespec_get_now_monotonic(struct timespec * timespec_now) -{ -#if defined(__MACH__) - // On OS X use clock_get_time. - clock_serv_t cclock; - mach_timespec_t mts; - host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); - clock_get_time(cclock, &mts); - mach_port_deallocate(mach_task_self(), cclock); - timespec_now->tv_sec = mts.tv_sec; - timespec_now->tv_nsec = mts.tv_nsec; -#else // if defined(__MACH__) - // Otherwise use clock_gettime. -#ifdef CLOCK_MONOTONIC_RAW - clock_gettime(CLOCK_MONOTONIC_RAW, timespec_now); -#else - clock_gettime(CLOCK_MONOTONIC, timespec_now); -#endif // CLOCK_MONOTONIC_RAW -#endif // if defined(__MACH__) -} - #define __WOULD_BE_NEGATIVE(seconds, subseconds) (seconds < 0 || (subseconds < 0 && seconds == 0)) rcl_ret_t rcl_system_time_point_now(rcl_system_time_point_t * now) { RCL_CHECK_ARGUMENT_FOR_NULL(now, RCL_RET_INVALID_ARGUMENT); -#ifndef WIN32 - // Unix implementations -#if HAS_CLOCK_GETTIME || defined(__MACH__) - // If clock_gettime is available or on OS X, use a timespec. - struct timespec timespec_now; - __timespec_get_now_system(×pec_now); - if (__WOULD_BE_NEGATIVE(timespec_now.tv_sec, timespec_now.tv_nsec)) { - RCL_SET_ERROR_MSG("unexpected negative time"); - return RCL_RET_ERROR; - } - now->nanoseconds = RCL_S_TO_NS(timespec_now.tv_sec) + timespec_now.tv_nsec; -#else // if HAS_CLOCK_GETTIME || defined(__MACH__) - // Otherwise we have to fallback to gettimeofday. - struct timeval timeofday; - gettimeofday(&timeofday, NULL); - if (__WOULD_BE_NEGATIVE(timeofday.tv_sec, timeofday.tv_usec)) { - RCL_SET_ERROR_MSG("unexpected negative time"); - return RCL_RET_ERROR; - } - now->nanoseconds = RCL_S_TO_NS(timeofday.tv_sec) + timeofday.tv_usec * 1000; -#endif // if HAS_CLOCK_GETTIME || defined(__MACH__) -#else /* Windows implementation adapted from roscpp_core (3-clause BSD), see: * https://github.com/ros/roscpp_core/blob/0.5.6/rostime/src/time.cpp#L96 * @@ -185,7 +109,6 @@ rcl_system_time_point_now(rcl_system_time_point_t * now) if (ret != RCL_RET_OK) { return ret; // rcl error state should already be set. } -#endif return RCL_RET_OK; } diff --git a/rcl/test/rcl/test_allocator.cpp b/rcl/test/rcl/test_allocator.cpp index 0294267..5477713 100644 --- a/rcl/test/rcl/test_allocator.cpp +++ b/rcl/test/rcl/test_allocator.cpp @@ -28,9 +28,9 @@ public: } void SetUp() { - set_on_unepexcted_malloc_callback([]() {EXPECT_FALSE(true) << "unexpected malloc";}); - set_on_unepexcted_realloc_callback([]() {EXPECT_FALSE(true) << "unexpected realloc";}); - set_on_unepexcted_free_callback([]() {EXPECT_FALSE(true) << "unexpected free";}); + set_on_unepexcted_malloc_callback([]() {EXPECT_FALSE(true) << "UNEXPECTED MALLOC";}); + set_on_unepexcted_realloc_callback([]() {EXPECT_FALSE(true) << "UNEXPECTED REALLOC";}); + set_on_unepexcted_free_callback([]() {EXPECT_FALSE(true) << "UNEXPECTED FREE";}); start_memory_checking(); } diff --git a/rcl/test/test_memory_tools.cpp b/rcl/test/test_memory_tools.cpp index 1714c4f..1c03b03 100644 --- a/rcl/test/test_memory_tools.cpp +++ b/rcl/test/test_memory_tools.cpp @@ -35,12 +35,14 @@ TEST(TestMemoryTools, test_allocation_checking_tools) { }); set_on_unepexcted_free_callback(on_unexpected_free); void * mem = nullptr; + void * remem = nullptr; // First try before enabling, should have no effect. mem = malloc(1024); ASSERT_NE(mem, nullptr); - mem = realloc(mem, 2048); - ASSERT_NE(mem, nullptr); - free(mem); + remem = realloc(mem, 2048); + ASSERT_NE(remem, nullptr); + if (!remem) {free(mem);} + free(remem); EXPECT_EQ(unexpected_mallocs, 0); EXPECT_EQ(unexpected_reallocs, 0); EXPECT_EQ(unexpected_frees, 0); @@ -48,9 +50,10 @@ TEST(TestMemoryTools, test_allocation_checking_tools) { start_memory_checking(); mem = malloc(1024); ASSERT_NE(mem, nullptr); - mem = realloc(mem, 2048); - ASSERT_NE(mem, nullptr); - free(mem); + remem = realloc(mem, 2048); + ASSERT_NE(remem, nullptr); + if (!remem) {free(mem);} + free(remem); EXPECT_EQ(unexpected_mallocs, 0); EXPECT_EQ(unexpected_reallocs, 0); EXPECT_EQ(unexpected_frees, 0); @@ -61,10 +64,11 @@ TEST(TestMemoryTools, test_allocation_checking_tools) { mem = malloc(1024); assert_no_malloc_end(); ASSERT_NE(mem, nullptr); - mem = realloc(mem, 2048); + remem = realloc(mem, 2048); assert_no_realloc_end(); - ASSERT_NE(mem, nullptr); - free(mem); + ASSERT_NE(remem, nullptr); + if (!remem) {free(mem);} + free(remem); assert_no_free_end(); EXPECT_EQ(unexpected_mallocs, 1); EXPECT_EQ(unexpected_reallocs, 1); @@ -74,9 +78,10 @@ TEST(TestMemoryTools, test_allocation_checking_tools) { mem = malloc(1024); assert_no_malloc_end(); ASSERT_NE(mem, nullptr); - mem = realloc(mem, 2048); - ASSERT_NE(mem, nullptr); - free(mem); + remem = realloc(mem, 2048); + ASSERT_NE(remem, nullptr); + if (!remem) {free(mem);} + free(remem); EXPECT_EQ(unexpected_mallocs, 2); EXPECT_EQ(unexpected_reallocs, 1); EXPECT_EQ(unexpected_frees, 1); @@ -84,10 +89,11 @@ TEST(TestMemoryTools, test_allocation_checking_tools) { assert_no_realloc_begin(); mem = malloc(1024); ASSERT_NE(mem, nullptr); - mem = realloc(mem, 2048); + remem = realloc(mem, 2048); assert_no_realloc_end(); - ASSERT_NE(mem, nullptr); - free(mem); + ASSERT_NE(remem, nullptr); + if (!remem) {free(mem);} + free(remem); EXPECT_EQ(unexpected_mallocs, 2); EXPECT_EQ(unexpected_reallocs, 2); EXPECT_EQ(unexpected_frees, 1); @@ -95,9 +101,10 @@ TEST(TestMemoryTools, test_allocation_checking_tools) { assert_no_free_begin(); mem = malloc(1024); ASSERT_NE(mem, nullptr); - mem = realloc(mem, 2048); - ASSERT_NE(mem, nullptr); - free(mem); + remem = realloc(mem, 2048); + ASSERT_NE(remem, nullptr); + if (!remem) {free(mem);} + free(remem); assert_no_free_end(); EXPECT_EQ(unexpected_mallocs, 2); EXPECT_EQ(unexpected_reallocs, 2); @@ -105,9 +112,10 @@ TEST(TestMemoryTools, test_allocation_checking_tools) { // Go again, after disabling asserts, should have no effect. mem = malloc(1024); ASSERT_NE(mem, nullptr); - mem = realloc(mem, 2048); - ASSERT_NE(mem, nullptr); - free(mem); + remem = realloc(mem, 2048); + ASSERT_NE(remem, nullptr); + if (!remem) {free(mem);} + free(remem); EXPECT_EQ(unexpected_mallocs, 2); EXPECT_EQ(unexpected_reallocs, 2); EXPECT_EQ(unexpected_frees, 2); @@ -115,9 +123,10 @@ TEST(TestMemoryTools, test_allocation_checking_tools) { stop_memory_checking(); mem = malloc(1024); ASSERT_NE(mem, nullptr); - mem = realloc(mem, 2048); - ASSERT_NE(mem, nullptr); - free(mem); + remem = realloc(mem, 2048); + ASSERT_NE(remem, nullptr); + if (!remem) {free(mem);} + free(remem); EXPECT_EQ(unexpected_mallocs, 2); EXPECT_EQ(unexpected_reallocs, 2); EXPECT_EQ(unexpected_frees, 2);