Skip to content

Commit 9a59e81

Browse files
author
Felix Exner (fexner)
authored
Add a cmake option to activate address sanitizers (#146)
* Add a cmake option to activate address sanitizers * Fix unique_ptr type for comm buffer to array We keep a pointer to the raw data buffer received from the comm interfaces in our packages. This was held as a unique_ptr<uint8_t> while we were storing a uint8_t type inside. This commit changes the pointer's type to the correct one. * Fix memory leak in tests test_tcp_server had a memory leak by not deleting manually allocated clients.
1 parent a1e514c commit 9a59e81

File tree

7 files changed

+14
-9
lines changed

7 files changed

+14
-9
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
- name: install boost
3333
run: sudo apt-get install -y libboost-dev
3434
- name: configure
35-
run: mkdir build && cd build && cmake .. -DBUILDING_TESTS=1 -DINTEGRATION_TESTS=1
35+
run: mkdir build && cd build && cmake .. -DBUILDING_TESTS=1 -DINTEGRATION_TESTS=1 -DWITH_ASAN=ON
3636
env:
3737
CXXFLAGS: -g -O2 -fprofile-arcs -ftest-coverage
3838
CFLAGS: -g -O2 -fprofile-arcs -ftest-coverage

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
88
set(CMAKE_BUILD_TYPE RelWithDebInfo)
99
endif()
1010

11+
option(WITH_ASAN "Compile with address sanitizer support" OFF)
1112

1213
##
1314
## Check C++11 support / enable global pedantic and Wall
@@ -51,6 +52,10 @@ add_library(urcl SHARED
5152
add_library(ur_client_library::urcl ALIAS urcl)
5253
target_compile_options(urcl PRIVATE -Wall -Wextra -Wno-unused-parameter)
5354
target_compile_options(urcl PUBLIC ${CXX17_FLAG})
55+
if(WITH_ASAN)
56+
target_compile_options(urcl PUBLIC -fsanitize=address)
57+
target_link_options(urcl PUBLIC -fsanitize=address)
58+
endif()
5459
target_include_directories( urcl PUBLIC
5560
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
5661
$<INSTALL_INTERFACE:include>

include/ur_client_library/comm/bin_parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class BinParser
239239
* \param buffer The buffer to copy the remaining bytes to.
240240
* \param buffer_length Reference to write the number of remaining bytes to.
241241
*/
242-
void rawData(std::unique_ptr<uint8_t>& buffer, size_t& buffer_length)
242+
void rawData(std::unique_ptr<uint8_t[]>& buffer, size_t& buffer_length)
243243
{
244244
buffer_length = buf_end_ - buf_pos_;
245245
buffer.reset(new uint8_t[buffer_length]);

include/ur_client_library/primary/primary_package.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class PrimaryPackage : public comm::URPackage<PackageHeader>
8282
virtual std::string toString() const;
8383

8484
protected:
85-
std::unique_ptr<uint8_t> buffer_;
85+
std::unique_ptr<uint8_t[]> buffer_;
8686
size_t buffer_length_;
8787
};
8888

include/ur_client_library/rtde/rtde_package.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class RTDEPackage : public comm::URPackage<PackageHeader>
6969
virtual std::string toString() const;
7070

7171
protected:
72-
std::unique_ptr<uint8_t> buffer_;
72+
std::unique_ptr<uint8_t[]> buffer_;
7373
size_t buffer_length_;
7474
PackageType type_;
7575
};

tests/test_bin_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ TEST(bin_parser, raw_data_parser)
230230
0x62, 0x65, 0x20, 0x70, 0x61, 0x72, 0x73, 0x65, 0x64 };
231231
comm::BinParser bp(buffer, sizeof(buffer));
232232

233-
std::unique_ptr<uint8_t> raw_data;
233+
std::unique_ptr<uint8_t[]> raw_data;
234234
size_t size;
235235
bp.rawData(raw_data, size);
236236

tests/test_tcp_server.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <gtest/gtest.h>
3232
#include <condition_variable>
3333
#include <chrono>
34+
#include <memory>
3435

3536
#include <ur_client_library/comm/tcp_server.h>
3637
#include <ur_client_library/comm/tcp_socket.h>
@@ -225,13 +226,12 @@ TEST_F(TCPServerTest, unlimited_clients_allowed)
225226
server.start();
226227

227228
// Test that a large number of clients can connect to the server
228-
std::vector<Client*> clients;
229-
Client* client;
229+
std::vector<std::unique_ptr<Client>> clients;
230+
std::unique_ptr<Client> client;
230231
for (unsigned int i = 0; i < 100; ++i)
231232
{
232-
client = new Client(port_);
233+
clients.push_back(std::make_unique<Client>(port_));
233234
ASSERT_TRUE(waitForConnectionCallback());
234-
clients.push_back(client);
235235
}
236236
}
237237

0 commit comments

Comments
 (0)