You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@mesos.apache.org by "Alexander Rojas (JIRA)" <ji...@apache.org> on 2015/02/26 10:51:05 UTC
[jira] [Commented] (MESOS-2412) Potential memleak(s) in
stout/os.hpp
[ https://issues.apache.org/jira/browse/MESOS-2412?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14338166#comment-14338166 ]
Alexander Rojas commented on MESOS-2412:
----------------------------------------
This is the kind of cases why raw pointers should be avoided. In these case, I would even suggest using something like:
{code}
while (true) {
std::vector<char> buffer(size);
if (getpwnam_r(user.get().c_str(), &passwd, &buffer[0], buffer.size(), &result) == 0) {
...
return gid;
} else {
// RHEL7 (and possibly other systems) will return non-zero and
// set one of the following errors for "The given name or uid
// was not found." See 'man getpwnam_r'. We only check for the
// errors explicitly listed, and do not consider the ellipsis.
if (errno == ENOENT ||
errno == ESRCH ||
errno == EBADF ||
errno == EPERM) {
return None();
// HERE WE DO NOT DELETE BUFFER.
}
...
// getpwnam_r set ERANGE so try again with a larger buffer.
size *= 2;
}
{code}
As you see, no need to care of the buffer lifecycle.
> Potential memleak(s) in stout/os.hpp
> ------------------------------------
>
> Key: MESOS-2412
> URL: https://issues.apache.org/jira/browse/MESOS-2412
> Project: Mesos
> Issue Type: Bug
> Components: stout
> Reporter: Joerg Schad
> Labels: coverity
>
> Coverity picked up this potential memleak in os.hpp where we do not delete buffer in the else case. The exact same pattern occurs in getuid(const Option<std::string>& user = None()).
> The corresponding CID 1230371 and 1230371.
> {code}
> inline Result<gid_t> getgid(const Option<std::string>& user = None())
> ...
> while (true) {
> char* buffer = new char[size];
> if (getpwnam_r(user.get().c_str(), &passwd, buffer, size, &result) == 0) {
> ...
> delete[] buffer;
> return gid;
> } else {
> // RHEL7 (and possibly other systems) will return non-zero and
> // set one of the following errors for "The given name or uid
> // was not found." See 'man getpwnam_r'. We only check for the
> // errors explicitly listed, and do not consider the ellipsis.
> if (errno == ENOENT ||
> errno == ESRCH ||
> errno == EBADF ||
> errno == EPERM) {
> return None();
> // HERE WE DO NOT DELETE BUFFER.
> }
> ...
> // getpwnam_r set ERANGE so try again with a larger buffer.
> size *= 2;
> delete[] buffer;
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)