Skip to content

Commit

Permalink
added check to prevent yarp read stealing ports already in use
Browse files Browse the repository at this point in the history
  • Loading branch information
randaz81 committed Apr 28, 2022
1 parent 6830d8a commit d1dbf0f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions doc/release/master/safe_yarpread.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <yarp/companion/impl/BottleReader.h>
#include <yarp/os/ContactStyle.h>
#include <yarp/os/Network.h>
#include <yarp/os/LogStream.h>

#include <cstring>

Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <yarp/os/Port.h>
#include <yarp/os/SystemClock.h>
#include <yarp/os/impl/Terminal.h>
#include <yarp/os/Network.h>
#include <yarp/os/LogStream.h>

#ifdef YARP_HAS_Libedit
#include <yarp/conf/dirs.h>
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

0 comments on commit d1dbf0f

Please sign in to comment.