Skip to content

Commit

Permalink
vport: Fix strncpy warning
Browse files Browse the repository at this point in the history
g++-8.2.0 complains about strncpy with the following warning:

```
drivers/vport.cc: In member function ‘CommandResponse VPort::Init(const bess::pb::VPortArg&)’:
drivers/vport.cc:508:12: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound 16 equals destination size [-Werror=stringop-truncation]
     strncpy(ifname_, name().c_str(), IFNAMSIZ);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/vport.cc:506:12: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound 16 equals destination size [-Werror=stringop-truncation]
     strncpy(ifname_, arg.ifname().c_str(), IFNAMSIZ);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus: all warnings being treated as errors
Error: drivers/vport.o
```

This was not a problem for arg.ifname(), but could have been a problem
for name().

Fix it by:

* Checking the length of name() as well (suggested by Chris Torek
  <chris.torek@gmail.com>)
* Put the terminating byte in any case
  • Loading branch information
ddiproietto authored and chris3torek committed Aug 23, 2018
1 parent 36cdfed commit ee7e574
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions core/drivers/vport.cc
Original file line number Diff line number Diff line change
Expand Up @@ -494,19 +494,19 @@ CommandResponse VPort::Init(const bess::pb::VPortArg &arg) {
netns_fd_ = -1;
container_pid_ = 0;

if (arg.ifname().length() >= IFNAMSIZ) {
const std::string &ifname = !arg.ifname().empty() ? arg.ifname() : name();

if (ifname.length() >= IFNAMSIZ) {
err = CommandFailure(EINVAL,
"Linux interface name should be "
"shorter than %d characters",
IFNAMSIZ);
goto fail;
}

if (arg.ifname().length()) {
strncpy(ifname_, arg.ifname().c_str(), IFNAMSIZ);
} else {
strncpy(ifname_, name().c_str(), IFNAMSIZ);
}
static_assert(sizeof(ifname_) == IFNAMSIZ);
strncpy(ifname_, ifname.c_str(), IFNAMSIZ - 1);
ifname_[IFNAMSIZ - 1] = '\0';

if (arg.cpid_case() == bess::pb::VPortArg::kDocker) {
err = docker_container_pid(arg.docker(), &container_pid_);
Expand Down

0 comments on commit ee7e574

Please sign in to comment.