Skip to content

Commit

Permalink
safe_strdup() mustn't rely on non-C-standard strdup() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
skiselkov committed Jul 13, 2023
1 parent b529a7f commit ab8cf2f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/acfutils/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include <dirent.h> /* to bring in DIR, opendir, readdir & friends */
#include <unistd.h>
#endif
#include <time.h>

#if IBM
#include <windows.h>
Expand All @@ -54,6 +53,7 @@
#include "math_core.h"
#include "sysmacros.h"
#include "safe_alloc.h"
#include "time.h"
#include "types.h"

#ifdef __cplusplus
Expand Down
12 changes: 7 additions & 5 deletions src/acfutils/safe_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ safe_realloc(void *oldptr, size_t size)
static inline char *
safe_strdup(const char *str2)
{
char *str = strdup(str2);
if (str2 != NULL) {
VERIFY_MSG(str != NULL, "Cannot allocate %lu bytes: "
"out of memory", (long unsigned)strlen(str2) + 1);
}
char *str;
int l;
ASSERT(str2 != NULL);
l = strlen(str2);
str = (char *)safe_malloc(l + 1);
memcpy(str, str2, l);
str[l] = '\0';
return (str);
}

Expand Down

0 comments on commit ab8cf2f

Please sign in to comment.