Skip to content

Commit

Permalink
[core] Avoid using strlen in ThreadName class
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko committed Sep 7, 2021
1 parent 2e09a16 commit e98146a
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions srtcore/threadname.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,24 @@ class ThreadName
#endif
}

explicit ThreadNameImpl(const char* name)
explicit ThreadNameImpl(const std::string& name)
: reset(false)
{
tid = pthread_self();

if (!get(old_name))
return;

reset = set(name);
reset = set(name.c_str());
if (reset)
return;

// Try with a shorter name. 15 is the upper limit supported by Linux,
// other platforms should support a larger value. So 15 should works
// on all platforms.
size_t max_len = 15;
if (std::strlen(name) > max_len)
reset = set(std::string(name, max_len).c_str());
const size_t max_len = 15;
if (name.size() > max_len)
reset = set(name.substr(0, max_len).c_str());
}

~ThreadNameImpl()
Expand Down Expand Up @@ -171,7 +171,7 @@ class ThreadName

static bool set(const char*) { return false; }

ThreadNameImpl(const char*) {}
ThreadNameImpl(const std::string&) {}

~ThreadNameImpl() // just to make it "non-trivially-destructible" for compatibility with normal version
{
Expand Down Expand Up @@ -209,7 +209,7 @@ class ThreadName
static bool set(const std::string& name) { return ThreadNameImpl::set(name.c_str()); }

explicit ThreadName(const std::string& name)
: impl(name.c_str())
: impl(name)
{
}

Expand Down

0 comments on commit e98146a

Please sign in to comment.