Commit 10e76f9a authored by Eric Cano's avatar Eric Cano
Browse files

Fixed improper use of readlink which does not add the trainling \0.

Adapted the unit test which did not simulate this behavior.
parent 467445bd
......@@ -215,8 +215,10 @@ SCSI::DeviceInfo SCSI::DeviceVector::getDeviceInfo(const char * path) {
{
char rl[PATH_MAX];
std::string lp = ret.sysfs_entry + "/generic";
if (-1 == m_sysWrapper.readlink(lp.c_str(), rl, sizeof (rl)))
ssize_t rlSize;
if (-1 == (rlSize = m_sysWrapper.readlink(lp.c_str(), rl, sizeof (rl) - 1)))
throw exceptions::Errnum("Could not read link " + lp);
rl[rlSize] = '\0';
std::string gl(rl);
size_t pos = gl.find_last_of("/");
if (pos == std::string::npos)
......
......@@ -76,7 +76,11 @@ int System::fakeWrapper::readlink(const char* path, char* buf, size_t len) {
return -1;
}
const std::string & link = m_links[std::string(path)];
strncpy(buf, link.c_str(), len);
/* Copy the link without the training \0 as it is the behavior
of the real readlink */
size_t lenToCopy = link.size();
if (lenToCopy > len) lenToCopy = len;
link.copy(buf, lenToCopy);
return len > link.size() ? link.size() : len;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment