Skip to content

Instantly share code, notes, and snippets.

@ruanchao
Created August 23, 2012 04:49
Show Gist options
  • Save ruanchao/3432550 to your computer and use it in GitHub Desktop.
Save ruanchao/3432550 to your computer and use it in GitHub Desktop.
Safe-memory-functions
/*
* safe_malloc ()
*
* Call calloc () and abort if the specified amount of memory cannot be
* allocated.
*/
void *
safe_malloc(size_t size)
{
void *mem_block = NULL;
if ((mem_block = calloc(1, size)) == NULL) {
fprintf(stderr, "ERROR: safe_malloc(%zu) cannot allocate memory.", size);
exit(EXIT_FAILURE);
}
return (mem_block);
}
/*
* safe_realloc ()
*
* Call realloc () and abort if the specified amount of memory cannot be
* allocated.
*/
void *
safe_realloc(void *old_mem, size_t new_size)
{
if ((old_mem = realloc(old_mem, new_size)) == NULL) {
fprintf(stderr, "ERROR: safe_realloc() cannot allocate"
"%zu blocks of memory.\n", new_size);
exit(EXIT_FAILURE);
}
return (old_mem);
}
/*
* safe_strdup ()
*
* Safe version of strdup avoid buffer overflow, etc.
*
*/
char *
safe_strdup(const char *str)
{
char *copy = NULL;
if (str == NULL) {
fprintf(stderr, "ERROR safe_strdup(): str == NULL");
exit(EXIT_FAILURE);
}
copy = safe_malloc((strlen(str) + 1) * sizeof(char));
(void) strcpy(copy, str);
return (copy);
}
char *
safe_strcat(char *str1, const char *str2)
{
char *rv = NULL;
size_t len = 0;
if (str1 == NULL || str2 == NULL) {
fprintf(stderr, "ERROR safe_strcat_new(): str == NULL");
exit(EXIT_FAILURE);
}
len = strlen(str1) + strlen(str2) + 1;
rv = safe_malloc(len * sizeof(char));
(void) strcpy(rv, str1);
rv = strcat(rv, str2);
return (rv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment