From: Ken Hornstein Date: Wed, 5 Mar 2014 23:22:56 +0000 (-0500) Subject: Add extra argument to add_param() so it can use the passed-in pointers X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/7cb4e52b1051d8f5eab4ebc157831bbfb084d22e?hp=1e2eede170a7cc4ac86aa78f688b246c407664f9 Add extra argument to add_param() so it can use the passed-in pointers directly without making copies first. --- diff --git a/h/mhparse.h b/h/mhparse.h index b6754794..9b058e68 100644 --- a/h/mhparse.h +++ b/h/mhparse.h @@ -418,10 +418,14 @@ char *output_params(size_t initialwidth, PM params, int *offsetout, * last - Pointer to tail of linked list * name - Name of parameter * value - Value of parameter + * nocopy - If set, will use the pointer values directly for "name" + * and "value" instead of making their own copy. These + * pointers will be free()'d later by the MIME routines, so + * they should not be used after calling this function! * - * Returned allocated parameter element + * Returns allocated parameter element */ -PM add_param(PM *first, PM *last, const char *name, const char *value); +PM add_param(PM *first, PM *last, char *name, char *value, int nocopy); /* * Retrieve a parameter value from a parameter linked list. Convert to the diff --git a/uip/mhbuildsbr.c b/uip/mhbuildsbr.c index 95bbd414..555e3b5c 100644 --- a/uip/mhbuildsbr.c +++ b/uip/mhbuildsbr.c @@ -1468,7 +1468,7 @@ scan_content (CT ct, size_t maxunencoded) CI ci = &ct->c_ctinfo; add_param(&ci->ci_first_pm, &ci->ci_last_pm, "charset", - contains8bit ? write_charset_8bit() : "us-ascii"); + contains8bit ? write_charset_8bit() : "us-ascii", 0); t->tx_charset = CHARSET_SPECIFIED; } } @@ -1536,7 +1536,7 @@ build_headers (CT ct, int header_encoding) static int level = 0; /* store nesting level */ snprintf (buffer, sizeof(buffer), "%s%d", prefix, level++); - add_param(&ci->ci_first_pm, &ci->ci_last_pm, "boundary", buffer); + add_param(&ci->ci_first_pm, &ci->ci_last_pm, "boundary", buffer, 0); } /* @@ -1907,7 +1907,7 @@ setup_attach_content(CT ct, char *filename) if (pm == NULL) add_param(&ct->c_ctinfo.ci_first_pm, &ct->c_ctinfo.ci_last_pm, - "name", simplename); + "name", simplename, 0); ct->c_descr = getcpy(simplename); ct->c_descr = add("\n", ct->c_descr); @@ -1926,5 +1926,5 @@ setup_attach_content(CT ct, char *filename) ct->c_dispo_type = getcpy("attachment"); } - add_param(&ct->c_dispo_first, &ct->c_dispo_last, "filename", simplename); + add_param(&ct->c_dispo_first, &ct->c_dispo_last, "filename", simplename, 0); } diff --git a/uip/mhfixmsg.c b/uip/mhfixmsg.c index 9f317acd..07433377 100644 --- a/uip/mhfixmsg.c +++ b/uip/mhfixmsg.c @@ -1080,7 +1080,7 @@ copy_ctinfo (CI dest, CI src) { for (s_pm = src->ci_first_pm; s_pm; s_pm = s_pm->pm_next) { d_pm = add_param(&dest->ci_first_pm, &dest->ci_last_pm, s_pm->pm_name, - s_pm->pm_value); + s_pm->pm_value, 0); if (s_pm->pm_charset) d_pm->pm_charset = getcpy(s_pm->pm_charset); if (s_pm->pm_lang) @@ -1314,7 +1314,7 @@ build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) { } add_param(&ct->c_ctinfo.ci_first_pm, &ct->c_ctinfo.ci_last_pm, - "boundary", boundary); + "boundary", boundary, 0); p = (struct part *) mh_xmalloc (sizeof *p); p->mp_next = (struct part *) mh_xmalloc (sizeof *p->mp_next); diff --git a/uip/mhparse.c b/uip/mhparse.c index 0ec76071..943e4233 100644 --- a/uip/mhparse.c +++ b/uip/mhparse.c @@ -838,7 +838,7 @@ magic_skip: if (ct->c_dispo_type && !get_param(ct->c_dispo_first, "filename", '_', 1)) { add_param(&ct->c_dispo_first, &ct->c_dispo_last, "filename", - r1bindex(ci->ci_magic, '/')); + r1bindex(ci->ci_magic, '/'), 0); } } else @@ -3590,9 +3590,7 @@ bad_quote: pp->lang = lang; } } else { - pm = add_param(param_head, param_tail, nameptr, valptr); - free(nameptr); - free(valptr); + pm = add_param(param_head, param_tail, nameptr, valptr, 1); pm->pm_charset = charset; pm->pm_lang = lang; } @@ -3637,11 +3635,9 @@ bad_quote: p[tlen] = '\0'; - pm = add_param(param_head, param_tail, pp->name, p); + pm = add_param(param_head, param_tail, pp->name, p, 1); pm->pm_charset = pp->charset; pm->pm_lang = pp->lang; - free(pp->name); - free(p); pp2 = pp->next; free(pp); pp = pp2; @@ -4058,14 +4054,14 @@ normal_param(PM pm, char *output, size_t len, size_t valuelen, */ PM -add_param(PM *first, PM *last, const char *name, const char *value) +add_param(PM *first, PM *last, char *name, char *value, int nocopy) { PM pm = mh_xmalloc(sizeof(*pm)); memset(pm, 0, sizeof(*pm)); - pm->pm_name = getcpy(name); - pm->pm_value = getcpy(value); + pm->pm_name = nocopy ? name : getcpy(name); + pm->pm_value = nocopy ? value : getcpy(value); if (*first) { (*last)->pm_next = pm;