/* Create duplicate of ha structure */
static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
{
- struct ast_ha *new_ha = malloc(sizeof(struct ast_ha));
- /* Copy from original to new object */
- ast_copy_ha(original, new_ha);
+ struct ast_ha *new_ha;
+
+ if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
+ /* Copy from original to new object */
+ ast_copy_ha(original, new_ha);
+ }
return new_ha;
}
struct ast_ha *ast_append_ha(char *sense, char *stuff, struct ast_ha *path)
{
- struct ast_ha *ha = malloc(sizeof(struct ast_ha));
+ struct ast_ha *ha;
char *nm = "255.255.255.255";
char tmp[256];
struct ast_ha *prev = NULL;
struct ast_ha *ret;
int x, z;
- unsigned int y;
+ unsigned int y;
+
ret = path;
while (path) {
prev = path;
path = path->next;
}
- if (ha) {
+ if ((ha = ast_malloc(sizeof(*ha)))) {
ast_copy_string(tmp, stuff, sizeof(tmp));
nm = strchr(tmp, '/');
if (!nm) {
return -1;
}
}
- lin = malloc(sizeof(struct linear_state));
- if (lin) {
- memset(lin, 0, sizeof(lin));
+ if ((lin = ast_calloc(1, sizeof(*lin)))) {
lin->fd = fd;
lin->allowoverride = allowoverride;
lin->autoclose = autoclose;
int fd;
time_t start;
- s = alloca(strlen(path) + 10);
- fs = alloca(strlen(path) + 20);
-
- if (!fs || !s) {
+ if (!(s = alloca(strlen(path) + 10)) || !(fs = alloca(strlen(path) + 20))) {
ast_log(LOG_WARNING, "Out of memory!\n");
return AST_LOCK_FAILURE;
}
int ast_unlock_path(const char *path)
{
char *s;
- s = alloca(strlen(path) + 10);
- if (!s)
+ if (!(s = alloca(strlen(path) + 10)))
return -1;
snprintf(s, strlen(path) + 9, "%s/%s", path, ".lock");
ast_log(LOG_DEBUG, "Unlocked path '%s'\n", path);
if (fd < 0) {
ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", filename, strerror(errno));
return NULL;
- }
- output=(char *)malloc(count);
- if (output) {
+ }
+ if ((output = ast_malloc(count))) {
res = read(fd, output, count - 1);
if (res == count - 1) {
output[res] = '\0';
free(output);
output = NULL;
}
- } else
- ast_log(LOG_WARNING, "Out of memory!\n");
+ }
close(fd);
return output;
}
work = ast_strdupa(version);
work = ast_strip(ast_strip_quoted(work, "$", "$"));
version_length = strlen(work) + 1;
-
- new = calloc(1, sizeof(*new) + version_length);
- if (!new)
+
+ if (!(new = ast_calloc(1, sizeof(*new) + version_length)))
return;
new->file = file;
{
int res = -1;
struct ast_atexit *ae;
- ast_unregister_atexit(func);
- ae = malloc(sizeof(struct ast_atexit));
+ ast_unregister_atexit(func);
AST_LIST_LOCK(&atexits);
- if (ae) {
- memset(ae, 0, sizeof(struct ast_atexit));
+ if ((ae = ast_calloc(1, sizeof(*ae)))) {
AST_LIST_INSERT_HEAD(&atexits, ae, list);
ae->func = func;
res = 0;
/* ARGUSED */
{
if (replace) {
- char *t = alloca(strlen(s) + 2);
- if (t) {
+ char *t;
+ if ((t = alloca(strlen(s) + 2))) {
sprintf(t, "\r%s", s);
if (complete)
ast_network_puts(t);
}
break;
case 'd': /* date */
- memset(&tm, 0, sizeof(struct tm));
+ memset(&tm, 0, sizeof(tm));
time(&ts);
if (localtime_r(&ts, &tm)) {
strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm);
break;
#endif
case 't': /* time */
- memset(&tm, 0, sizeof(struct tm));
+ memset(&tm, 0, sizeof(tm));
time(&ts);
if (localtime_r(&ts, &tm)) {
strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm);
break;
if (matches + 1 >= match_list_len) {
match_list_len <<= 1;
- match_list = realloc(match_list, match_list_len * sizeof(char *));
+ if (!(match_list = ast_realloc(match_list, match_list_len * sizeof(char *)))) {
+ /* TODO: Handle memory allocation failure */
+ }
}
match_list[matches++] = strdup(retstr);
if (!match_list)
return (char **) NULL;
- if (matches>= match_list_len)
- match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
+ if (matches >= match_list_len) {
+ if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(char *)))) {
+ /* TODO: Handle memory allocation failure */
+ }
+ }
match_list[matches] = (char *) NULL;
if (nummatches > 0) {
char *mbuf;
int mlen = 0, maxmbuf = 2048;
- /* Start with a 2048 byte buffer */
- mbuf = malloc(maxmbuf);
- if (!mbuf)
+ /* Start with a 2048 byte buffer */
+ if (!(mbuf = ast_malloc(maxmbuf)))
return (char *)(CC_ERROR);
snprintf(buf, sizeof(buf),"_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr);
fdprint(ast_consock, buf);
while (!strstr(mbuf, AST_CLI_COMPLETE_EOF) && res != -1) {
if (mlen + 1024 > maxmbuf) {
/* Every step increment buffer 1024 bytes */
- maxmbuf += 1024;
- mbuf = realloc(mbuf, maxmbuf);
- if (!mbuf)
+ maxmbuf += 1024;
+ if (!(mbuf = ast_realloc(mbuf, maxmbuf)))
return (char *)(CC_ERROR);
}
/* Only read 1024 bytes at a time */