Skip to content

Commit

Permalink
Use memcpy rather than strncpy
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardalee committed Dec 28, 2024
1 parent 89bfac9 commit 9162ff6
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions core/reactor_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,27 @@ const char* lf_reactor_full_name(self_base_t* self) {
if (self->full_name != NULL) {
return self->full_name;
}
// First find the length of the name.
size_t len = 0;
len += strlen(self->name);
// First find the length of the full name.
size_t name_len = strlen(self->name);
size_t len = name_len;
self_base_t* parent = self->parent;
while (parent != NULL) {
len++;
len++; // For the dot.
len += strlen(parent->name);
parent = parent->parent;
}
self->full_name = (char*)lf_allocate(len + 1, sizeof(char), &self->allocations);
self->full_name[len] = '\0'; // Null terminate the string.

size_t location = len - strlen(self->name);
strncpy(&self->full_name[location], self->name, strlen(self->name) + 1);
size_t location = len - name_len;
memcpy(&self->full_name[location], self->name, name_len);
parent = self->parent;
while (parent != NULL) {
location--;
self->full_name[location] = '.';
location -= strlen(parent->name);
strncpy(&self->full_name[location], parent->name, strlen(parent->name));
size_t parent_len = strlen(parent->name);
location -= parent_len;
memcpy(&self->full_name[location], parent->name, parent_len);
parent = parent->parent;
}
return self->full_name;
Expand Down

0 comments on commit 9162ff6

Please # to comment.