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 f8b171a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bondcpp/include/bondcpp/bond.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class Bond
void publishingTimerReset();
BONDCPP_PUBLIC
void publishingTimerCancel();

BONDCPP_PUBLIC
double getDeadPublishPeriod() const {return dead_publish_period_.seconds();}
BONDCPP_PUBLIC
Expand Down
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 f8b171a

Please sign in to comment.