Skip to content

Commit e109685

Browse files
jbachorikclaude
andcommitted
Fix remaining CppCheck warnings and style violations
- Replace obsolete alloca() with std::string and std::vector for safer memory management - Add explicit keyword to single-argument constructors to prevent implicit conversions - Improve code safety and consistency across C++ codebase 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 1a85c0d commit e109685

7 files changed

Lines changed: 13 additions & 13 deletions

File tree

ddprof-lib/src/main/cpp/dictionary.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Dictionary {
6363

6464
public:
6565
Dictionary() : Dictionary(0) {}
66-
Dictionary(int id) : _id(id) {
66+
explicit Dictionary(int id) : _id(id) {
6767
_table = (DictTable *)calloc(1, sizeof(DictTable));
6868
Counters::set(DICTIONARY_PAGES, 1, id);
6969
Counters::set(DICTIONARY_BYTES, sizeof(DictTable), id);

ddprof-lib/src/main/cpp/flightRecorder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,8 +1612,8 @@ void FlightRecorder::recordLog(LogLevel level, const char *message,
16121612

16131613
if (len > MAX_STRING_LENGTH)
16141614
len = MAX_STRING_LENGTH;
1615-
// cppcheck-suppress obsoleteFunctions
1616-
Buffer *buf = (Buffer *)alloca(len + 40);
1615+
std::vector<char> buf_storage(len + 40);
1616+
Buffer *buf = reinterpret_cast<Buffer *>(buf_storage.data());
16171617
buf->reset();
16181618

16191619
int start = buf->skip(5);

ddprof-lib/src/main/cpp/jfrMetadata.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class Element {
117117
std::vector<Attribute> _attributes;
118118
std::vector<const Element *> _children;
119119

120-
Element(const char *name) : _name(getId(name)), _attributes(), _children() {}
120+
explicit Element(const char *name) : _name(getId(name)), _attributes(), _children() {}
121121

122122
Element &attribute(const char *key, const char *value) {
123123
_attributes.push_back(Attribute(getId(key), getId(value)));
@@ -153,7 +153,7 @@ class Element {
153153

154154
class NoField : public Element {
155155
public:
156-
NoField(const char *name) : Element(name) {}
156+
explicit NoField(const char *name) : Element(name) {}
157157

158158
virtual const bool is_nofield() { return true; }
159159
};

ddprof-lib/src/main/cpp/reservoirSampler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ReservoirSampler {
3232
std::vector<T> _reservoir;
3333

3434
public:
35-
ReservoirSampler(const int size) :
35+
explicit ReservoirSampler(const int size) :
3636
_size(size),
3737
_generator([]() {
3838
std::random_device rd;

ddprof-lib/src/main/cpp/symbols_linux.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,10 +710,10 @@ UnloadProtection::UnloadProtection(const CodeCache *cc) {
710710
// dlopen() can reopen previously loaded libraries even if the underlying file has been deleted
711711
const char* stripped_name = cc->name();
712712
size_t name_len = strlen(stripped_name);
713+
std::string name_buf;
713714
if (name_len > 10 && strcmp(stripped_name + name_len - 10, " (deleted)") == 0) {
714-
char* buf = (char*) alloca(name_len - 9);
715-
*stpncpy(buf, stripped_name, name_len - 10) = 0;
716-
stripped_name = buf;
715+
name_buf.assign(stripped_name, name_len - 10);
716+
stripped_name = name_buf.c_str();
717717
}
718718

719719
// Protect library from unloading while parsing in-memory ELF program headers.

ddprof-lib/src/main/cpp/symbols_linux.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SymbolDesc {
1111
const char *_desc;
1212

1313
public:
14-
SymbolDesc(const char *s) {
14+
explicit SymbolDesc(const char *s) {
1515
_addr = s;
1616
_desc = strchr(_addr, ' ');
1717
}
@@ -32,7 +32,7 @@ class MemoryMapDesc {
3232
const char *_file;
3333

3434
public:
35-
MemoryMapDesc(const char *s) {
35+
explicit MemoryMapDesc(const char *s) {
3636
_addr = s;
3737
_end = strchr(_addr, '-') + 1;
3838
_perm = strchr(_end, ' ') + 1;

ddprof-lib/src/main/cpp/unwindStats.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class ExclusiveLock {
160160
private:
161161
SpinLock &_lock;
162162
public:
163-
ExclusiveLock(SpinLock &lock) : _lock(lock) {
163+
explicit ExclusiveLock(SpinLock &lock) : _lock(lock) {
164164
_lock.lock();
165165
}
166166
~ExclusiveLock() {
@@ -175,7 +175,7 @@ class SharedLock {
175175
private:
176176
SpinLock &_lock;
177177
public:
178-
SharedLock(SpinLock &lock) : _lock(lock) {
178+
explicit SharedLock(SpinLock &lock) : _lock(lock) {
179179
_lock.lockShared();
180180
}
181181
~SharedLock() {

0 commit comments

Comments
 (0)