Skip to content

Commit

Permalink
Revamp copyString to use memcpy.
Browse files Browse the repository at this point in the history
Also make it allocate less memory in general.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette committed Oct 23, 2023
1 parent 8a0b831 commit e87e66b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions smclib/include/smclib/statemap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ inline char * copyString(const char * s)
{
char * retval = NULL;
if (s != NULL) {
retval = new char[MAX_NAME_LEN + 1];
retval[MAX_NAME_LEN] = '\0';
(void) std::strncpy(retval, s, MAX_NAME_LEN);
size_t copy_len = strlen(s);
if (copy_len > MAX_NAME_LEN) {
copy_len = MAX_NAME_LEN;
}
retval = new char[copy_len + 1];
memcpy(retval, s, copy_len);
retval[copy_len] = '\0';
}

return retval;
Expand Down

0 comments on commit e87e66b

Please sign in to comment.