From d1dbf0f2e06a06198980b4eea5b2884b9dc5a9aa Mon Sep 17 00:00:00 2001 From: Marco Randazzo Date: Thu, 21 Apr 2022 20:44:20 +0200 Subject: [PATCH] added check to prevent `yarp read` stealing ports already in use --- doc/release/master/safe_yarpread.md | 9 +++++++++ .../src/yarp/companion/impl/Companion.cmdRead.cpp | 10 ++++++++++ .../src/yarp/companion/impl/Companion.cmdWrite.cpp | 12 ++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 doc/release/master/safe_yarpread.md diff --git a/doc/release/master/safe_yarpread.md b/doc/release/master/safe_yarpread.md new file mode 100644 index 00000000000..03ada4fd4ef --- /dev/null +++ b/doc/release/master/safe_yarpread.md @@ -0,0 +1,9 @@ +safe-yarpread {#master} +--------------------------------- + +New Features +------------ + +### lib_yarp_companion + +* commands 'yarp read' and 'yarp write' now checks that target ports do not already exist. diff --git a/src/libYARP_companion/src/yarp/companion/impl/Companion.cmdRead.cpp b/src/libYARP_companion/src/yarp/companion/impl/Companion.cmdRead.cpp index ea3e5df208a..8a08b5d8d22 100644 --- a/src/libYARP_companion/src/yarp/companion/impl/Companion.cmdRead.cpp +++ b/src/libYARP_companion/src/yarp/companion/impl/Companion.cmdRead.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include @@ -77,5 +78,14 @@ int Companion::cmdRead(int argc, char *argv[]) argc--; argv++; } + + //the following check prevents opening as local port a port which is already registered (and active) on the yarp nameserver + bool e = NetworkBase::exists(name, true); + if (e) + { + yCError(COMPANION) << "Port"<< name << "already exists! Do you mean yarp read ..."<< name <<"?"; + return 1; + } + return read(name, src, showEnvelope, trim); } diff --git a/src/libYARP_companion/src/yarp/companion/impl/Companion.cmdWrite.cpp b/src/libYARP_companion/src/yarp/companion/impl/Companion.cmdWrite.cpp index 4f4c3c409ca..7d159f4655a 100644 --- a/src/libYARP_companion/src/yarp/companion/impl/Companion.cmdWrite.cpp +++ b/src/libYARP_companion/src/yarp/companion/impl/Companion.cmdWrite.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #ifdef YARP_HAS_Libedit #include @@ -24,6 +26,7 @@ using yarp::companion::impl::Companion; using yarp::os::Bottle; using yarp::os::Port; using yarp::os::SystemClock; +using yarp::os::NetworkBase; int Companion::write(const char *name, int ntargets, char *targets[]) { Port port; @@ -154,5 +157,14 @@ int Companion::cmdWrite(int argc, char *argv[]) } const char *src = argv[0]; + + //the following check prevents opening as local port a port which is already registered (and active) on the yarp nameserver + bool e = NetworkBase::exists(src, true); + if (e) + { + yCError(COMPANION) << "Port" << src << "already exists! Aborting..."; + return 1; + } + return write(src, argc-1, argv+1); }