Add bacnet_strdup function to replace POSIX strdup and update bacfile to use it (#1251)

This commit is contained in:
Steve Karg
2026-03-04 07:14:25 -06:00
committed by GitHub
parent 70914cf4d8
commit b17a7c6084
4 changed files with 26 additions and 19 deletions
+15
View File
@@ -2136,3 +2136,18 @@ int bacnet_snprintf(
return write_length;
}
/**
* @brief duplicate a string (replacement for POSIX strdup)
* @param s - string to duplicate
* @return a pointer to a new string on success, or a null pointer
*/
char *bacnet_strdup(const char *s)
{
size_t size = strlen(s) + 1;
char *p = malloc(size);
if (p != NULL) {
memcpy(p, s, size);
}
return p;
}
+2
View File
@@ -238,6 +238,8 @@ char *bacnet_trim(char *str, const char *trimmedchars);
BACNET_STACK_EXPORT
char *bacnet_stptok(const char *s, char *tok, size_t toklen, const char *brk);
BACNET_STACK_EXPORT
char *bacnet_strdup(const char *s);
#ifdef __cplusplus
}
+6 -18
View File
@@ -116,21 +116,6 @@ void BACfile_Writable_Property_List(
}
}
/**
* @brief duplicate a string (replacement for POSIX strdup)
* @param s - string to duplicate
* @return a pointer to a new string on success, or a null pointer
*/
static char *bacfile_strdup(const char *s)
{
size_t size = strlen(s) + 1;
char *p = malloc(size);
if (p != NULL) {
memcpy(p, s, size);
}
return p;
}
/**
* @brief For a given object instance-number, returns the pathname
* @param object_instance - object-instance number of the object
@@ -161,7 +146,7 @@ void bacfile_pathname_set(uint32_t object_instance, const char *pathname)
pObject = Keylist_Data(Object_List, object_instance);
if (pObject) {
free(pObject->Pathname);
pObject->Pathname = bacfile_strdup(pathname);
pObject->Pathname = bacnet_strdup(pathname);
}
}
@@ -245,7 +230,7 @@ bool bacfile_object_name_set(uint32_t object_instance, const char *new_name)
if (pObject) {
status = true;
free(pObject->Object_Name);
pObject->Object_Name = bacfile_strdup(new_name);
pObject->Object_Name = bacnet_strdup(new_name);
}
return status;
@@ -698,7 +683,7 @@ void bacfile_file_type_set(uint32_t object_instance, const char *mime_type)
pObject = Keylist_Data(Object_List, object_instance);
if (pObject) {
free(pObject->File_Type);
pObject->File_Type = bacfile_strdup(mime_type);
pObject->File_Type = bacnet_strdup(mime_type);
}
}
@@ -1314,6 +1299,9 @@ bool bacfile_delete(uint32_t object_instance)
pObject = Keylist_Data_Delete(Object_List, object_instance);
if (pObject) {
free(pObject->Pathname);
free(pObject->File_Type);
free(pObject->Object_Name);
free(pObject);
status = true;
}