FS-8650 #resolve [Add switch_must_malloc/switch_must_realloc/switch_must_strdup and use it in switch_xml.c]

This commit is contained in:
Anthony Minessale 2015-12-11 16:57:30 -06:00
parent af5281e91d
commit 350a739da8
2 changed files with 95 additions and 166 deletions

View File

@ -227,6 +227,29 @@ struct switch_core_session;
struct switch_core_runtime; struct switch_core_runtime;
struct switch_core_port_allocator; struct switch_core_port_allocator;
static inline void *switch_must_malloc(size_t _b)
{
void *m = malloc(_b);
switch_assert(m);
return m;
}
static inline void *switch_must_realloc(void *_b, size_t _z)
{
void *m = realloc(_b, _z);
switch_assert(m);
return m;
}
static inline char *switch_must_strdup(const char *_s)
{
char *s = strdup(_s);
switch_assert(s);
return s;
}
/*! /*!
\defgroup core1 Core Library \defgroup core1 Core Library
\ingroup FREESWITCH \ingroup FREESWITCH

View File

@ -595,23 +595,10 @@ static char *switch_xml_decode(char *s, char **ent, char t)
l = (d = (unsigned long) (s - r)) + c + (unsigned long) strlen(e); /* new length */ l = (d = (unsigned long) (s - r)) + c + (unsigned long) strlen(e); /* new length */
if (l) { if (l) {
if (r == m) { if (r == m) {
char *tmp = (char *) malloc(l); char *tmp = (char *) switch_must_malloc(l);
if (tmp) { r = strcpy(tmp, r);
r = strcpy(tmp, r);
} else {
if (r)
free(r);
return NULL;
}
} else { } else {
char *tmp = (char *) realloc(r, l); r = (char *) switch_must_realloc(r, l);
if (tmp) {
r = tmp;
} else {
if (r)
free(r);
return NULL;
}
} }
} }
e = strchr((s = r + d), ';'); /* fix up pointers */ e = strchr((s = r + d), ';'); /* fix up pointers */
@ -683,19 +670,11 @@ static void switch_xml_char_content(switch_xml_root_t root, char *s, switch_size
xml->txt = s; /* initial character content */ xml->txt = s; /* initial character content */
else { /* allocate our own memory and make a copy */ else { /* allocate our own memory and make a copy */
if ((xml->flags & SWITCH_XML_TXTM)) { /* allocate some space */ if ((xml->flags & SWITCH_XML_TXTM)) { /* allocate some space */
char *tmp = (char *) realloc(xml->txt, (l = strlen(xml->txt)) + len); xml->txt = (char *) switch_must_realloc(xml->txt, (l = strlen(xml->txt)) + len);
if (tmp) {
xml->txt = tmp;
} else {
return;
}
} else { } else {
char *tmp = (char *) malloc((l = strlen(xml->txt)) + len); char *tmp = (char *) switch_must_malloc((l = strlen(xml->txt)) + len);
if (tmp) {
xml->txt = strcpy(tmp, xml->txt); xml->txt = strcpy(tmp, xml->txt);
} else {
return;
}
} }
strcpy(xml->txt + l, s); /* add new char content */ strcpy(xml->txt + l, s); /* add new char content */
if (s != m) if (s != m)
@ -759,38 +738,29 @@ static void switch_xml_proc_inst(switch_xml_root_t root, char *s, switch_size_t
} }
if (!root->pi || !root->pi[0]) { if (!root->pi || !root->pi[0]) {
root->pi = (char ***) malloc(sizeof(char **)); root->pi = (char ***) switch_must_malloc(sizeof(char **));
if (!root->pi)
return;
*(root->pi) = NULL; /* first pi */ *(root->pi) = NULL; /* first pi */
} }
while (root->pi[i] && strcmp(target, root->pi[i][0])) while (root->pi[i] && strcmp(target, root->pi[i][0]))
i++; /* find target */ i++; /* find target */
if (!root->pi[i]) { /* new target */ if (!root->pi[i]) { /* new target */
char ***ssstmp = (char ***) realloc(root->pi, sizeof(char **) * (i + 2)); char ***ssstmp = (char ***) switch_must_realloc(root->pi, sizeof(char **) * (i + 2));
if (!ssstmp)
return;
root->pi = ssstmp; root->pi = ssstmp;
if (!root->pi) if (!root->pi)
return; return;
root->pi[i] = (char **) malloc(sizeof(char *) * 3); root->pi[i] = (char **) switch_must_malloc(sizeof(char *) * 3);
if (!root->pi[i])
return;
root->pi[i][0] = target; root->pi[i][0] = target;
root->pi[i][1] = (char *) (root->pi[i + 1] = NULL); /* terminate pi list */ root->pi[i][1] = (char *) (root->pi[i + 1] = NULL); /* terminate pi list */
root->pi[i][2] = strdup(""); /* empty document position list */ root->pi[i][2] = switch_must_strdup(""); /* empty document position list */
} }
while (root->pi[i][j]) while (root->pi[i][j])
j++; /* find end of instruction list for this target */ j++; /* find end of instruction list for this target */
sstmp = (char **) realloc(root->pi[i], sizeof(char *) * (j + 3)); sstmp = (char **) switch_must_realloc(root->pi[i], sizeof(char *) * (j + 3));
if (!sstmp)
return;
root->pi[i] = sstmp; root->pi[i] = sstmp;
stmp = (char *) realloc(root->pi[i][j + 1], j + 1); stmp = (char *) switch_must_realloc(root->pi[i][j + 1], j + 1);
if (!stmp)
return;
root->pi[i][j + 2] = stmp; root->pi[i][j + 2] = stmp;
strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<"); strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<");
root->pi[i][j + 1] = NULL; /* null terminate pi list for this target */ root->pi[i][j + 1] = NULL; /* null terminate pi list for this target */
@ -804,7 +774,7 @@ static short switch_xml_internal_dtd(switch_xml_root_t root, char *s, switch_siz
int i, j; int i, j;
char **sstmp; char **sstmp;
pe = (char **) memcpy(malloc(sizeof(SWITCH_XML_NIL)), SWITCH_XML_NIL, sizeof(SWITCH_XML_NIL)); pe = (char **) memcpy(switch_must_malloc(sizeof(SWITCH_XML_NIL)), SWITCH_XML_NIL, sizeof(SWITCH_XML_NIL));
for (s[len] = '\0'; s;) { for (s[len] = '\0'; s;) {
while (*s && *s != '<' && *s != '%') while (*s && *s != '<' && *s != '%')
@ -824,11 +794,7 @@ static short switch_xml_internal_dtd(switch_xml_root_t root, char *s, switch_siz
} }
for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++); for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++);
sstmp = (char **) realloc(ent, (i + 3) * sizeof(char *)); /* space for next ent */ sstmp = (char **) switch_must_realloc(ent, (i + 3) * sizeof(char *)); /* space for next ent */
if (!sstmp) {
switch_xml_err(root, v, "Allocation Error!");
break;
}
ent = sstmp; ent = sstmp;
if (*c == '%') if (*c == '%')
pe = ent; pe = ent;
@ -900,20 +866,15 @@ static short switch_xml_internal_dtd(switch_xml_root_t root, char *s, switch_siz
} }
if (!root->attr[i]) { /* new tag name */ if (!root->attr[i]) { /* new tag name */
root->attr = (!i) ? (char ***) malloc(2 * sizeof(char **)) root->attr = (!i) ? (char ***) switch_must_malloc(2 * sizeof(char **))
: (char ***) realloc(root->attr, (i + 2) * sizeof(char **)); : (char ***) switch_must_realloc(root->attr, (i + 2) * sizeof(char **));
root->attr[i] = (char **) malloc(2 * sizeof(char *)); root->attr[i] = (char **) switch_must_malloc(2 * sizeof(char *));
root->attr[i][0] = t; /* set tag name */ root->attr[i][0] = t; /* set tag name */
root->attr[i][1] = (char *) (root->attr[i + 1] = NULL); root->attr[i][1] = (char *) (root->attr[i + 1] = NULL);
} }
for (j = 1; root->attr[i][j]; j += 3); /* find end of list */ for (j = 1; root->attr[i][j]; j += 3); /* find end of list */
sstmp = (char **) realloc(root->attr[i], (j + 4) * sizeof(char *)); sstmp = (char **) switch_must_realloc(root->attr[i], (j + 4) * sizeof(char *));
if (!sstmp) {
switch_xml_err(root, t, "Allocation Error!");
break;
}
root->attr[i] = sstmp; root->attr[i] = sstmp;
root->attr[i][j + 3] = NULL; /* null terminate list */ root->attr[i][j + 3] = NULL; /* null terminate list */
@ -948,7 +909,7 @@ static char *switch_xml_str2utf8(char **s, switch_size_t *len)
if (be == -1) if (be == -1)
return NULL; /* not UTF-16 */ return NULL; /* not UTF-16 */
u = (char *) malloc(max); u = (char *) switch_must_malloc(max);
for (sl = 2; sl < *len - 1; sl += 2) { for (sl = 2; sl < *len - 1; sl += 2) {
c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) /* UTF-16BE */ c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) /* UTF-16BE */
: (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); /* UTF-16LE */ : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); /* UTF-16LE */
@ -960,9 +921,7 @@ static char *switch_xml_str2utf8(char **s, switch_size_t *len)
while (l + 6 > max) { while (l + 6 > max) {
char *tmp; char *tmp;
tmp = (char *) realloc(u, max += SWITCH_XML_BUFSIZE); tmp = (char *) switch_must_realloc(u, max += SWITCH_XML_BUFSIZE);
if (!tmp)
return NULL;
u = tmp; u = tmp;
} }
if (c < 0x80) if (c < 0x80)
@ -976,7 +935,7 @@ static char *switch_xml_str2utf8(char **s, switch_size_t *len)
u[l++] = (char) (0x80 | ((c >> (6 * --b)) & 0x3F)); /* payload */ u[l++] = (char) (0x80 | ((c >> (6 * --b)) & 0x3F)); /* payload */
} }
} }
return *s = (char *) realloc(u, *len = l); return *s = (char *) switch_must_realloc(u, *len = l);
} }
/* frees a tag attribute list */ /* frees a tag attribute list */
@ -1006,7 +965,7 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_parse_str_dynamic(char *s, switch_bool_t
char *data; char *data;
switch_assert(s); switch_assert(s);
data = dup ? strdup(s) : s; data = dup ? switch_must_strdup(s) : s;
if ((root = (switch_xml_root_t) switch_xml_parse_str(data, strlen(data)))) { if ((root = (switch_xml_root_t) switch_xml_parse_str(data, strlen(data)))) {
root->dynamic = 1; /* Make sure we free the memory is switch_xml_free() */ root->dynamic = 1; /* Make sure we free the memory is switch_xml_free() */
@ -1056,10 +1015,10 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_parse_str(char *s, switch_size_t len)
for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++); for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++);
for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { /* new attrib */ for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { /* new attrib */
attr = (l) ? (char **) realloc(attr, (l + 4) * sizeof(char *)) attr = (l) ? (char **) switch_must_realloc(attr, (l + 4) * sizeof(char *))
: (char **) malloc(4 * sizeof(char *)); /* allocate space */ : (char **) switch_must_malloc(4 * sizeof(char *)); /* allocate space */
attr[l + 3] = (l) ? (char *) realloc(attr[l + 1], (l / 2) + 2) attr[l + 3] = (l) ? (char *) switch_must_realloc(attr[l + 1], (l / 2) + 2)
: (char *) malloc(2); /* mem for list of maloced vals */ : (char *) switch_must_malloc(2); /* mem for list of maloced vals */
strcpy(attr[l + 3] + (l / 2), " "); /* value is not malloced */ strcpy(attr[l + 3] + (l / 2), " "); /* value is not malloced */
attr[l + 2] = NULL; /* null terminate list */ attr[l + 2] = NULL; /* null terminate list */
attr[l + 1] = (char *) ""; /* temporary attribute value */ attr[l + 1] = (char *) ""; /* temporary attribute value */
@ -1176,17 +1135,12 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_parse_fp(FILE * fp)
switch_size_t l, len = 0; switch_size_t l, len = 0;
char *s; char *s;
if (!(s = (char *) malloc(SWITCH_XML_BUFSIZE))) s = (char *) switch_must_malloc(SWITCH_XML_BUFSIZE);
return NULL;
do { do {
len += (l = fread((s + len), 1, SWITCH_XML_BUFSIZE, fp)); len += (l = fread((s + len), 1, SWITCH_XML_BUFSIZE, fp));
if (l == SWITCH_XML_BUFSIZE) { if (l == SWITCH_XML_BUFSIZE) {
char *tmp = s; s = (char *) switch_must_realloc(s, len + SWITCH_XML_BUFSIZE);
s = (char *) realloc(s, len + SWITCH_XML_BUFSIZE);
if (!s) {
free(tmp);
return NULL;
}
} }
} while (s && l == SWITCH_XML_BUFSIZE); } while (s && l == SWITCH_XML_BUFSIZE);
@ -1215,9 +1169,8 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_parse_fd(int fd)
return NULL; return NULL;
} }
m = malloc(st.st_size); m = switch_must_malloc(st.st_size);
if (!m)
return NULL;
if (!(0<(l = read(fd, m, st.st_size))) if (!(0<(l = read(fd, m, st.st_size)))
|| !(root = (switch_xml_root_t) switch_xml_parse_str((char *) m, l))) { || !(root = (switch_xml_root_t) switch_xml_parse_str((char *) m, l))) {
free(m); free(m);
@ -1357,8 +1310,8 @@ static FILE *preprocess_glob(const char *cwd, const char *pattern, FILE *write_f
} }
for (n = 0; n < glob_data.gl_pathc; ++n) { for (n = 0; n < glob_data.gl_pathc; ++n) {
dir_path = strdup(glob_data.gl_pathv[n]); dir_path = switch_must_strdup(glob_data.gl_pathv[n]);
switch_assert(dir_path);
if ((e = strrchr(dir_path, *SWITCH_PATH_SEPARATOR))) { if ((e = strrchr(dir_path, *SWITCH_PATH_SEPARATOR))) {
*e = '\0'; *e = '\0';
} }
@ -1411,12 +1364,12 @@ static int preprocess(const char *cwd, const char *file, FILE *write_fd, int rle
} }
eblen = len * 2; eblen = len * 2;
ebuf = malloc(eblen); ebuf = switch_must_malloc(eblen);
memset(ebuf, 0, eblen); memset(ebuf, 0, eblen);
while (!(bp = expand_vars(buf, ebuf, eblen, &cur, &err))) { while (!(bp = expand_vars(buf, ebuf, eblen, &cur, &err))) {
eblen *= 2; eblen *= 2;
ebuf = realloc(ebuf, eblen); ebuf = switch_must_realloc(ebuf, eblen);
memset(ebuf, 0, eblen); memset(ebuf, 0, eblen);
} }
@ -1606,8 +1559,8 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_parse_file_simple(const char *file)
if ((fd = open(file, O_RDONLY, 0)) > -1) { if ((fd = open(file, O_RDONLY, 0)) > -1) {
fstat(fd, &st); fstat(fd, &st);
if (!st.st_size) goto error; if (!st.st_size) goto error;
m = malloc(st.st_size); m = switch_must_malloc(st.st_size);
switch_assert(m);
if (!(0<(l = read(fd, m, st.st_size)))) goto error; if (!(0<(l = read(fd, m, st.st_size)))) goto error;
if (!(root = (switch_xml_root_t) switch_xml_parse_str((char *) m, l))) goto error; if (!(root = (switch_xml_root_t) switch_xml_parse_str((char *) m, l))) goto error;
root->dynamic = 1; root->dynamic = 1;
@ -2062,7 +2015,7 @@ static void switch_xml_user_cache(const char *key, const char *user_name, const
switch_safe_free(expires_lookup); switch_safe_free(expires_lookup);
} }
if (expires) { if (expires) {
char *expires_val = malloc(1024); char *expires_val = switch_must_malloc(1024);
if (sprintf(expires_val, "%ld", (long)expires)) { if (sprintf(expires_val, "%ld", (long)expires)) {
switch_core_hash_insert(CACHE_EXPIRES_HASH, mega_key, expires_val); switch_core_hash_insert(CACHE_EXPIRES_HASH, mega_key, expires_val);
} else { } else {
@ -2083,7 +2036,7 @@ SWITCH_DECLARE(switch_status_t) switch_xml_locate_user_merged(const char *key, c
int i, nkeys; int i, nkeys;
if (strchr(key, ':')) { if (strchr(key, ':')) {
kdup = strdup(key); kdup = switch_must_strdup(key);
nkeys = switch_split(kdup, ':', keys); nkeys = switch_split(kdup, ':', keys);
} else { } else {
keys[0] = (char *)key; keys[0] = (char *)key;
@ -2456,10 +2409,7 @@ static char *switch_xml_ampencode(const char *s, switch_size_t len, char **dst,
while (s != e) { while (s != e) {
while (*dlen + 10 > *max) { while (*dlen + 10 > *max) {
char *tmp = (char *) realloc(*dst, *max += SWITCH_XML_BUFSIZE); *dst = (char *) switch_must_realloc(*dst, *max += SWITCH_XML_BUFSIZE);
if (!tmp)
return *dst;
*dst = tmp;
} }
if (immune) { if (immune) {
@ -2574,10 +2524,7 @@ static char *switch_xml_toxml_r(switch_xml_t xml, char **s, switch_size_t *len,
*s = switch_xml_ampencode(txt + start, xml->off - start, s, len, max, 0); *s = switch_xml_ampencode(txt + start, xml->off - start, s, len, max, 0);
while (*len + strlen(xml->name) + 5 + (strlen(XML_INDENT) * (*count)) + 1 > *max) { /* reallocate s */ while (*len + strlen(xml->name) + 5 + (strlen(XML_INDENT) * (*count)) + 1 > *max) { /* reallocate s */
char *tmp = *s; *s = (char *) switch_must_realloc(*s, *max += SWITCH_XML_BUFSIZE);
*s = (char *) realloc(*s, *max += SWITCH_XML_BUFSIZE);
if (!*s)
return tmp;
} }
if (*len && *(*s + (*len) - 1) == '>') { if (*len && *(*s + (*len) - 1) == '>') {
@ -2592,10 +2539,7 @@ static char *switch_xml_toxml_r(switch_xml_t xml, char **s, switch_size_t *len,
if (switch_xml_attr(xml, xml->attr[i]) != xml->attr[i + 1]) if (switch_xml_attr(xml, xml->attr[i]) != xml->attr[i + 1])
continue; continue;
while (*len + strlen(xml->attr[i]) + 7 + (strlen(XML_INDENT) * (*count)) > *max) { /* reallocate s */ while (*len + strlen(xml->attr[i]) + 7 + (strlen(XML_INDENT) * (*count)) > *max) { /* reallocate s */
char *tmp = (char *) realloc(*s, *max += SWITCH_XML_BUFSIZE); *s = (char *) switch_must_realloc(*s, *max += SWITCH_XML_BUFSIZE);
if (!tmp)
return *s;
*s = tmp;
} }
*len += sprintf(*s + *len, " %s=\"", xml->attr[i]); *len += sprintf(*s + *len, " %s=\"", xml->attr[i]);
@ -2608,10 +2552,7 @@ static char *switch_xml_toxml_r(switch_xml_t xml, char **s, switch_size_t *len,
if (!attr[i][j + 1] || switch_xml_attr(xml, attr[i][j]) != attr[i][j + 1]) if (!attr[i][j + 1] || switch_xml_attr(xml, attr[i][j]) != attr[i][j + 1])
continue; /* skip duplicates and non-values */ continue; /* skip duplicates and non-values */
while (*len + strlen(attr[i][j]) + 8 + (strlen(XML_INDENT) * (*count)) > *max) { /* reallocate s */ while (*len + strlen(attr[i][j]) + 8 + (strlen(XML_INDENT) * (*count)) > *max) { /* reallocate s */
char *tmp = (char *) realloc(*s, *max += SWITCH_XML_BUFSIZE); *s = (char *) switch_must_realloc(*s, *max += SWITCH_XML_BUFSIZE);
if (!tmp)
return *s;
*s = tmp;
} }
*len += sprintf(*s + *len, " %s=\"", attr[i][j]); *len += sprintf(*s + *len, " %s=\"", attr[i][j]);
@ -2630,10 +2571,7 @@ static char *switch_xml_toxml_r(switch_xml_t xml, char **s, switch_size_t *len,
} }
while (*len + strlen(xml->name) + 5 + (strlen(XML_INDENT) * (*count)) > *max) { /* reallocate s */ while (*len + strlen(xml->name) + 5 + (strlen(XML_INDENT) * (*count)) > *max) { /* reallocate s */
char *tmp = *s; *s = (char *) switch_must_realloc(*s, *max += SWITCH_XML_BUFSIZE);
*s = (char *) realloc(*s, *max += SWITCH_XML_BUFSIZE);
if (!*s)
return tmp;
} }
if (xml->child || xml->txt) { if (xml->child || xml->txt) {
@ -2664,8 +2602,8 @@ static char *switch_xml_toxml_r(switch_xml_t xml, char **s, switch_size_t *len,
SWITCH_DECLARE(char *) switch_xml_toxml_nolock(switch_xml_t xml, switch_bool_t prn_header) SWITCH_DECLARE(char *) switch_xml_toxml_nolock(switch_xml_t xml, switch_bool_t prn_header)
{ {
char *s = (char *) malloc(SWITCH_XML_BUFSIZE); char *s = (char *) switch_must_malloc(SWITCH_XML_BUFSIZE);
switch_assert(s);
return switch_xml_toxml_buf(xml, s, SWITCH_XML_BUFSIZE, 0, prn_header); return switch_xml_toxml_buf(xml, s, SWITCH_XML_BUFSIZE, 0, prn_header);
} }
@ -2674,8 +2612,7 @@ SWITCH_DECLARE(char *) switch_xml_toxml(switch_xml_t xml, switch_bool_t prn_head
{ {
char *r, *s; char *r, *s;
s = (char *) malloc(SWITCH_XML_BUFSIZE); s = (char *) switch_must_malloc(SWITCH_XML_BUFSIZE);
switch_assert(s);
r = switch_xml_toxml_buf(xml, s, SWITCH_XML_BUFSIZE, 0, prn_header); r = switch_xml_toxml_buf(xml, s, SWITCH_XML_BUFSIZE, 0, prn_header);
@ -2687,10 +2624,10 @@ SWITCH_DECLARE(char *) switch_xml_tohtml(switch_xml_t xml, switch_bool_t prn_hea
char *r, *s, *h; char *r, *s, *h;
switch_size_t rlen = 0; switch_size_t rlen = 0;
switch_size_t len = SWITCH_XML_BUFSIZE; switch_size_t len = SWITCH_XML_BUFSIZE;
s = (char *) malloc(SWITCH_XML_BUFSIZE);
switch_assert(s); s = (char *) switch_must_malloc(SWITCH_XML_BUFSIZE);
h = (char *) malloc(SWITCH_XML_BUFSIZE); h = (char *) switch_must_malloc(SWITCH_XML_BUFSIZE);
switch_assert(h);
r = switch_xml_toxml_buf(xml, s, SWITCH_XML_BUFSIZE, 0, prn_header); r = switch_xml_toxml_buf(xml, s, SWITCH_XML_BUFSIZE, 0, prn_header);
h = switch_xml_ampencode(r, 0, &h, &rlen, &len, 1); h = switch_xml_ampencode(r, 0, &h, &rlen, &len, 1);
switch_safe_free(r); switch_safe_free(r);
@ -2704,7 +2641,7 @@ SWITCH_DECLARE(char *) switch_xml_toxml_buf(switch_xml_t xml, char *buf, switch_
switch_xml_t p = (xml) ? xml->parent : NULL; switch_xml_t p = (xml) ? xml->parent : NULL;
switch_xml_root_t root = (switch_xml_root_t) xml; switch_xml_root_t root = (switch_xml_root_t) xml;
switch_size_t len = 0, max = buflen; switch_size_t len = 0, max = buflen;
char *s, *t, *n, *r; char *s, *t, *n;
int i, j, k; int i, j, k;
uint32_t count = 0; uint32_t count = 0;
@ -2717,10 +2654,7 @@ SWITCH_DECLARE(char *) switch_xml_toxml_buf(switch_xml_t xml, char *buf, switch_
} }
if (!xml || !xml->name) { if (!xml || !xml->name) {
if (!(r = (char *) realloc(s, len + 1))) { return (char *) switch_must_realloc(s, len + 1);
abort();
}
return r;
} }
while (root->xml.parent) { while (root->xml.parent) {
@ -2734,10 +2668,7 @@ SWITCH_DECLARE(char *) switch_xml_toxml_buf(switch_xml_t xml, char *buf, switch_
continue; /* not pre-root */ continue; /* not pre-root */
} }
while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max) { while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max) {
if (!(r = (char *) realloc(s, max += SWITCH_XML_BUFSIZE))) { s = (char *) switch_must_realloc(s, max += SWITCH_XML_BUFSIZE);
abort();
}
s = r;
} }
len += sprintf(s + len, "<?%s%s%s?>", t, *n ? " " : "", n); len += sprintf(s + len, "<?%s%s%s?>", t, *n ? " " : "", n);
} }
@ -2752,20 +2683,13 @@ SWITCH_DECLARE(char *) switch_xml_toxml_buf(switch_xml_t xml, char *buf, switch_
continue; /* not post-root */ continue; /* not post-root */
} }
while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max) { while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max) {
if (!(r = (char *) realloc(s, max += SWITCH_XML_BUFSIZE))) { s = (char *) switch_must_realloc(s, max += SWITCH_XML_BUFSIZE);
abort();
}
s = r;
} }
len += sprintf(s + len, "\n<?%s%s%s?>", t, *n ? " " : "", n); len += sprintf(s + len, "\n<?%s%s%s?>", t, *n ? " " : "", n);
} }
} }
if (!(r = (char *) realloc(s, len + 1))) { return (char *) switch_must_realloc(s, len + 1);
abort();
}
return r;
} }
/* free the memory allocated for the switch_xml structure */ /* free the memory allocated for the switch_xml structure */
@ -2867,14 +2791,13 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_new(const char *name)
static const char *ent[] = { "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;", static const char *ent[] = { "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
"apos;", "&#39;", "amp;", "&#38;", NULL "apos;", "&#39;", "amp;", "&#38;", NULL
}; };
switch_xml_root_t root = (switch_xml_root_t) malloc(sizeof(struct switch_xml_root)); switch_xml_root_t root = (switch_xml_root_t) switch_must_malloc(sizeof(struct switch_xml_root));
if (!root)
return NULL;
memset(root, '\0', sizeof(struct switch_xml_root)); memset(root, '\0', sizeof(struct switch_xml_root));
root->xml.name = (char *) name; root->xml.name = (char *) name;
root->cur = &root->xml; root->cur = &root->xml;
strcpy(root->err, root->xml.txt = (char *) ""); strcpy(root->err, root->xml.txt = (char *) "");
root->ent = (char **) memcpy(malloc(sizeof(ent)), ent, sizeof(ent)); root->ent = (char **) memcpy(switch_must_malloc(sizeof(ent)), ent, sizeof(ent));
root->attr = root->pi = (char ***) (root->xml.attr = SWITCH_XML_NIL); root->attr = root->pi = (char ***) (root->xml.attr = SWITCH_XML_NIL);
return &root->xml; return &root->xml;
} }
@ -2927,8 +2850,8 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_add_child(switch_xml_t xml, const char *
if (!xml) if (!xml)
return NULL; return NULL;
if (!(child = (switch_xml_t) malloc(sizeof(struct switch_xml)))) child = (switch_xml_t) switch_must_malloc(sizeof(struct switch_xml));
return NULL;
memset(child, '\0', sizeof(struct switch_xml)); memset(child, '\0', sizeof(struct switch_xml));
child->name = (char *) name; child->name = (char *) name;
child->attr = SWITCH_XML_NIL; child->attr = SWITCH_XML_NIL;
@ -2965,20 +2888,15 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_set_attr(switch_xml_t xml, const char *n
if (!value) if (!value)
return xml; /* nothing to do */ return xml; /* nothing to do */
if (xml->attr == SWITCH_XML_NIL) { /* first attribute */ if (xml->attr == SWITCH_XML_NIL) { /* first attribute */
xml->attr = (char **) malloc(4 * sizeof(char *)); xml->attr = (char **) switch_must_malloc(4 * sizeof(char *));
if (!xml->attr) xml->attr[1] = switch_must_strdup(""); /* empty list of malloced names/vals */
return NULL;
xml->attr[1] = strdup(""); /* empty list of malloced names/vals */
} else { } else {
char **tmp = (char **) realloc(xml->attr, (l + 4) * sizeof(char *)); xml->attr = (char **) switch_must_realloc(xml->attr, (l + 4) * sizeof(char *));
if (!tmp)
return xml;
xml->attr = tmp;
} }
xml->attr[l] = (char *) name; /* set attribute name */ xml->attr[l] = (char *) name; /* set attribute name */
xml->attr[l + 2] = NULL; /* null terminate attribute list */ xml->attr[l + 2] = NULL; /* null terminate attribute list */
xml->attr[l + 3] = (char *) realloc(xml->attr[l + 1], (c = (int) strlen(xml->attr[l + 1])) + 2); xml->attr[l + 3] = (char *) switch_must_realloc(xml->attr[l + 1], (c = (int) strlen(xml->attr[l + 1])) + 2);
strcpy(xml->attr[l + 3] + c, " "); /* set name/value as not malloced */ strcpy(xml->attr[l + 3] + c, " "); /* set name/value as not malloced */
if (xml->flags & SWITCH_XML_DUP) if (xml->flags & SWITCH_XML_DUP)
xml->attr[l + 3][c] = SWITCH_XML_NAMEM; xml->attr[l + 3][c] = SWITCH_XML_NAMEM;
@ -2996,14 +2914,10 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_set_attr(switch_xml_t xml, const char *n
if (value) if (value)
xml->attr[l + 1] = (char *) value; /* set attribute value */ xml->attr[l + 1] = (char *) value; /* set attribute value */
else { /* remove attribute */ else { /* remove attribute */
char **tmp;
if (xml->attr[c + 1][l / 2] & SWITCH_XML_NAMEM) if (xml->attr[c + 1][l / 2] & SWITCH_XML_NAMEM)
free(xml->attr[l]); free(xml->attr[l]);
memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char *)); memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char *));
tmp = (char **) realloc(xml->attr, (c + 2) * sizeof(char *)); xml->attr = (char **) switch_must_realloc(xml->attr, (c + 2) * sizeof(char *));
if (!tmp)
return xml;
xml->attr = tmp;
memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1, (c / 2) - (l / 2)); /* fix list of which name/vals are malloced */ memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1, (c / 2) - (l / 2)); /* fix list of which name/vals are malloced */
} }
xml->flags &= ~SWITCH_XML_DUP; /* clear strdup() flag */ xml->flags &= ~SWITCH_XML_DUP; /* clear strdup() flag */
@ -3647,14 +3561,7 @@ static int globextend(const char *path, glob_t *pglob, size_t *limit)
} }
newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
pathv = pglob->gl_pathv ? realloc((char *) pglob->gl_pathv, newsize) : malloc(newsize); pathv = pglob->gl_pathv ? switch_must_realloc((char *) pglob->gl_pathv, newsize) : switch_must_malloc(newsize);
if (pathv == NULL) {
if (pglob->gl_pathv) {
free(pglob->gl_pathv);
pglob->gl_pathv = NULL;
}
return (GLOB_NOSPACE);
}
if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
/* first time around -- clear initial gl_offs items */ /* first time around -- clear initial gl_offs items */
@ -3667,10 +3574,9 @@ static int globextend(const char *path, glob_t *pglob, size_t *limit)
for (p = path; *p++;) for (p = path; *p++;)
continue; continue;
len = (size_t) (p - path); len = (size_t) (p - path);
if ((copy = malloc(len)) != NULL) { copy = switch_must_malloc(len);
memcpy(copy, path, len); memcpy(copy, path, len);
pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
}
pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
return (copy == NULL ? GLOB_NOSPACE : 0); return (copy == NULL ? GLOB_NOSPACE : 0);
} }