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
+3 -1
View File
@@ -12,7 +12,7 @@ The git repositories are hosted at the following sites:
* https://bacnet.sourceforge.net/ * https://bacnet.sourceforge.net/
* https://github.com/bacnet-stack/bacnet-stack/ * https://github.com/bacnet-stack/bacnet-stack/
## [Unreleased] - 2026-02-23 ## [Unreleased] - 2026-03-04
### Security ### Security
@@ -152,6 +152,8 @@ The git repositories are hosted at the following sites:
### Changed ### Changed
* Changed bacfile_strdup to bacnet_strdup function to replace POSIX strdup
and update bacfile to use bacnet_strdup. (#1251)
* Changed PositiveInteger present-value datatype to * Changed PositiveInteger present-value datatype to
BACNET_UNSIGNED_INTEGER. (#1246) BACNET_UNSIGNED_INTEGER. (#1246)
* Changed BACFILE define dependencies to reflect bacfile-posix.c dependence * Changed BACFILE define dependencies to reflect bacfile-posix.c dependence
+15
View File
@@ -2136,3 +2136,18 @@ int bacnet_snprintf(
return write_length; 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 BACNET_STACK_EXPORT
char *bacnet_stptok(const char *s, char *tok, size_t toklen, const char *brk); 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 #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 * @brief For a given object instance-number, returns the pathname
* @param object_instance - object-instance number of the object * @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); pObject = Keylist_Data(Object_List, object_instance);
if (pObject) { if (pObject) {
free(pObject->Pathname); 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) { if (pObject) {
status = true; status = true;
free(pObject->Object_Name); free(pObject->Object_Name);
pObject->Object_Name = bacfile_strdup(new_name); pObject->Object_Name = bacnet_strdup(new_name);
} }
return status; 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); pObject = Keylist_Data(Object_List, object_instance);
if (pObject) { if (pObject) {
free(pObject->File_Type); 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); pObject = Keylist_Data_Delete(Object_List, object_instance);
if (pObject) { if (pObject) {
free(pObject->Pathname);
free(pObject->File_Type);
free(pObject->Object_Name);
free(pObject); free(pObject);
status = true; status = true;
} }