Commit 9ecf15ee authored by Pekka Pessi's avatar Pekka Pessi

Code cleanup and memory leak fix in soa/sdp.

Fixed memory in iparsing sdp in soa.
Cleaned up handling of rejected media (related to bug report #1419078).

darcs-hash:20060203162345-65a35-0c2180b2462c5a8130317e317245edbbdf400728.gz
parent 870977d2
......@@ -12,18 +12,18 @@ INCLUDES = -I$(srcdir)/../su
noinst_LTLIBRARIES = libsdp.la
noinst_PROGRAMS = sdp_torture sdp_test
noinst_PROGRAMS = torture_sdp test_sdp
dist_noinst_SCRIPTS = run-tests
dist_noinst_SCRIPTS = run_test_sdp
TESTS = sdp_torture run-tests
TESTS = torture_sdp run_test_sdp
BUILT_SOURCES = sdp_tag_ref.c
# ----------------------------------------------------------------------
# Rules for building the targets
include_sofia_HEADERS = sdp.h sdp_tag.h
include_sofia_HEADERS = sdp.h sdp_tag.h
libsdp_la_SOURCES = sdp.c sdp_parse.c sdp_print.c \
sdp_tag.c sdp_tag_ref.c
......
......@@ -2,10 +2,10 @@
#
# Run the sdp tests
#
# usage: run-tests [test_program] [test-directory]
# usage: run_test_sdp [test_program] [test-directory]
#
sdp_test="${1:-./sdp_test}"
test_sdp="${1:-./test_sdp}"
tests="${2:-tests}"
if test -r $tests/message-1.sdp ; then
......@@ -13,10 +13,10 @@ if test -r $tests/message-1.sdp ; then
for n in 1 2 3 4 5 6 7 8 9 10;
do
echo -n "$n: "
"$sdp_test" < "$tests/message-$n.sdp" && echo OK
"$test_sdp" < "$tests/message-$n.sdp" && echo OK
done
else
echo "sdp run-tests: no tests found, skipping."
echo "sdp run-tests: no tests found, skipping."
fi
......@@ -52,7 +52,6 @@ struct sdp_parser_s {
sdp_session_t pru_session[1];
} pr_output;
char *pr_message;
su_home_t *pr_orig_home;
sdp_mode_t pr_session_mode;
......@@ -113,23 +112,27 @@ static void parsing_error(sdp_parser_t *p, char const *fmt, ...);
sdp_parser_t *
sdp_parse(su_home_t *home, char const msg[], int msgsize, int flags)
{
sdp_parser_t *p = su_home_clone(home, sizeof(*p));
sdp_parser_t *p;
char *b;
if (msgsize == -1 && msg)
msgsize = strlen(msg);
if (msgsize < 0 || msg == NULL) {
parsing_error(p, "invalid input message");
p = su_home_clone(home, sizeof(*p));
if (p)
parsing_error(p, "invalid input message");
else
p = (sdp_parser_t*)&no_mem_error;
return p;
}
b = su_alloc(p->pr_home, msgsize + 1);
p = su_home_clone(home, sizeof(*p) + msgsize + 1);
if (p && b) {
p->pr_orig_home = home;
strncpy(b, msg, msgsize);
if (p) {
b = strncpy((void *)(p + 1), msg, msgsize);
b[msgsize] = 0;
p->pr_message = b;
p->pr_strict = (flags & sdp_f_strict) != 0;
p->pr_anynet = (flags & sdp_f_anynet) != 0;
......@@ -205,7 +208,7 @@ char const *sdp_parsing_error(sdp_parser_t *p)
void sdp_parser_free(sdp_parser_t *p)
{
if (p && p != &no_mem_error)
su_free(p->pr_orig_home, p);
su_home_unref(p->pr_home);
}
/* ========================================================================= */
......
......@@ -583,6 +583,7 @@ static void print_media(sdp_printer_t *p,
else
sdp_printf(p, "m=%s %u/%u %s",
media, m->m_port, m->m_number_of_ports, proto);
if (m->m_rtpmaps) {
for (rm = m->m_rtpmaps; rm; rm = rm->rm_next) {
if (rm->rm_any)
......@@ -597,8 +598,11 @@ static void print_media(sdp_printer_t *p,
sdp_printf(p, " %s", l->l_text);
}
else {
sdp_printf(p, " 13");
sdp_printf(p, " 9"); /* SDP syntax requires at least one format.
9 is used by nobody, right?. */
}
sdp_printf(p, CRLF);
if (m->m_information)
......
......@@ -24,7 +24,7 @@
/**@internal
*
* @CFILE sdp_test.c
* @CFILE test_sdp.c
*
* Simple SDP tester
*
......@@ -97,7 +97,7 @@ int main(int argc, char *argv[])
int line, pos;
if (diff(buffer, sdp_message(printer), &line, &pos)) {
fprintf(stdout, "sdp_test:%d: messages differ:\n", line);
fprintf(stdout, "test_sdp:%d: messages differ:\n", line);
fputs(buffer, stdout);
fprintf(stdout, ">>>new message:\n");
fputs(sdp_message(printer), stdout);
......@@ -107,14 +107,14 @@ int main(int argc, char *argv[])
}
}
else {
fprintf(stderr, "sdp_test: %s\n", sdp_printing_error(printer));
fprintf(stderr, "test_sdp: %s\n", sdp_printing_error(printer));
}
sdp_printer_free(printer);
su_home_check(home);
}
else {
fprintf(stderr, "sdp_test: %s\n", sdp_parsing_error(p));
fprintf(stderr, "test_sdp: %s\n", sdp_parsing_error(p));
sdp_parser_free(p);
exit(1);
}
......@@ -123,10 +123,10 @@ int main(int argc, char *argv[])
}
else {
if (ferror(f)) {
perror("sdp_test");
perror("test_sdp");
}
else {
fprintf(stderr, "sdp_test: maximum length of sdp messages is %u bytes\n",
fprintf(stderr, "test_sdp: maximum length of sdp messages is %u bytes\n",
(unsigned)sizeof(buffer));
}
exit(1);
......
......@@ -26,7 +26,7 @@
*
* @CFILE sdp_torture.c
*
* Testing sdp module.
* Torture testing sdp module.
*
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
* @author Kai Vehmanen <kai.vehmanen@nokia.com>
......@@ -48,11 +48,13 @@
#include <su_tag_io.h>
#include <sdp_tag.h>
#define TSTFLAGS flags
#define TSTFLAGS tstflags
#include <tstdef.h>
char const *name = "sdp_torture.c";
int tstflags;
char const *name = "torture_sdp.c";
FILE *null;
......@@ -73,7 +75,7 @@ static char const e1_msg[] =
"a=rtpmap:0 PCMU/8000\n"
"m=video 0 *";
static int test_error(int flags)
static int test_error(void)
{
su_home_t *home = su_home_create();
sdp_parser_t *parser;
......@@ -115,7 +117,7 @@ static char const s0_msg[] =
"m=* 0 RTP/AVP *\n"
;
static int test_session(int flags)
static int test_session(void)
{
su_home_t *home = su_home_create(), *home2 = su_home_create();
sdp_session_t *sdp_src, *sdp_target;
......@@ -183,7 +185,7 @@ static int test_session(int flags)
if (printer != NULL) {
char const *msg = sdp_message(printer);
if (flags & tst_verbatim) {
if (tstflags & tst_verbatim) {
printf("sdp_torture.c: parsed SDP message:\"%s\".\n", msg);
}
......@@ -194,7 +196,7 @@ static int test_session(int flags)
TEST_1(dup = tl_adup(home2, lst));
if (flags & tst_verbatim)
if (tstflags & tst_verbatim)
tl_print(stdout, "dup:\n", dup);
else
tl_print(null, "dup:\n", dup);
......@@ -205,7 +207,7 @@ static int test_session(int flags)
printer = sdp_print(home2, sdp, buffer, sizeof(buffer), 0);
if (printer != NULL) {
char const *msg = sdp_message(printer);
if (flags & tst_verbatim) {
if (tstflags & tst_verbatim) {
printf("sdp_torture.c: "
"SDP message passed through tag list:\n\"%s\".\n", msg);
}
......@@ -254,7 +256,7 @@ static char const s2_msg[] =
"a=fmtp:97 mode-set=\"0\"\r\n"
"a=maxptime:500\r\n";
static int test_session2(int flags)
static int test_session2(void)
{
su_home_t *home = su_home_create();
sdp_session_t const *sdp = NULL;
......@@ -453,7 +455,7 @@ static char const s3_msg[] =
;
static int test_sanity(int flags)
static int test_sanity(void)
{
su_home_t *home = su_home_create();
sdp_parser_t *parser;
......@@ -475,7 +477,7 @@ static sdp_list_t const l0[1] = {{ sizeof(l0), NULL, "foo" }};
static sdp_list_t const l1[1] = {{ sizeof(l1), (sdp_list_t *)l0, "bar" }};
/** Test list things */
int test_list(int flags)
int test_list(void)
{
su_home_t *home = su_home_create();
sdp_list_t *l;
......@@ -520,7 +522,7 @@ sdp_rtpmap_t const rm1[1] =
}};
/** Test rtpmap-related things */
int test_rtpmap(int flags)
int test_rtpmap(void)
{
su_home_t *home = su_home_create();
sdp_rtpmap_t *rm;
......@@ -557,7 +559,7 @@ static sdp_attribute_t const a0[1] =
static sdp_attribute_t const a1[1] =
{{ sizeof(a1), (sdp_attribute_t *)a0, "bar", "1" }};
static int test_attribute(int flags)
static int test_attribute(void)
{
su_home_t *home = su_home_create();
sdp_attribute_t *a, *a_new, *list, *replaced;
......@@ -615,7 +617,7 @@ static int test_attribute(int flags)
END();
}
static int test_connection(int flags)
static int test_connection(void)
{
BEGIN();
END();
......@@ -649,7 +651,7 @@ static sdp_media_t const m0[1] =
"udp",
}};
static int test_media(int flags)
static int test_media(void)
{
su_home_t *home = su_home_create();
sdp_media_t *media;
......@@ -689,13 +691,13 @@ static int test_media(int flags)
END();
}
static int test_origin(int flags)
static int test_origin(void)
{
BEGIN();
END();
}
static int test_bandwidth(int flags)
static int test_bandwidth(void)
{
BEGIN();
END();
......@@ -716,7 +718,7 @@ static char const t_msg[] =
"z=2882844526 -1h 2898848070 0\n"
;
static int test_time(int flags)
static int test_time(void)
{
sdp_parser_t *parser;
sdp_session_t *sdp;
......@@ -749,7 +751,7 @@ static int test_time(int flags)
END();
}
static int test_key(int flags)
static int test_key(void)
{
BEGIN();
END();
......@@ -758,7 +760,7 @@ static int test_key(int flags)
#include <time.h>
#include <stdlib.h>
static int test_build(int flags)
static int test_build(void)
{
sdp_session_t *sdp, *dup;
sdp_origin_t *o;
......@@ -842,7 +844,7 @@ static int test_build(int flags)
TEST_1(printer = sdp_print(home, dup, NULL, 0, 0));
TEST_1(data = sdp_message(printer));
if (flags & tst_verbatim)
if (tstflags & tst_verbatim)
printf("sdp_torture.c: built SDP message:\"%s\".\n", data);
sdp_printer_free(printer);
......@@ -855,45 +857,34 @@ void usage(void)
fprintf(stderr, "usage: %s [-v]\n", name);
}
char *lastpart(char *path)
{
if (strchr(path, '/'))
return strrchr(path, '/') + 1;
else
return path;
}
int main(int argc, char *argv[])
{
int flags = 0;
int retval = 0;
int i;
name = lastpart(argv[0]); /* Set our name */
for (i = 1; argv[i]; i++) {
if (strcmp(argv[i], "-v") == 0)
flags |= tst_verbatim;
tstflags |= tst_verbatim;
else
usage();
}
null = fopen("/dev/null", "ab");
retval |= test_error(flags); fflush(stdout);
retval |= test_session(flags); fflush(stdout);
retval |= test_session2(flags); fflush(stdout);
retval |= test_sanity(flags); fflush(stdout);
retval |= test_list(flags); fflush(stdout);
retval |= test_rtpmap(flags); fflush(stdout);
retval |= test_origin(flags); fflush(stdout);
retval |= test_connection(flags); fflush(stdout);
retval |= test_bandwidth(flags); fflush(stdout);
retval |= test_time(flags); fflush(stdout);
retval |= test_key(flags); fflush(stdout);
retval |= test_attribute(flags); fflush(stdout);
retval |= test_media(flags); fflush(stdout);
retval |= test_build(flags); fflush(stdout);
retval |= test_error(); fflush(stdout);
retval |= test_session(); fflush(stdout);
retval |= test_session2(); fflush(stdout);
retval |= test_sanity(); fflush(stdout);
retval |= test_list(); fflush(stdout);
retval |= test_rtpmap(); fflush(stdout);
retval |= test_origin(); fflush(stdout);
retval |= test_connection(); fflush(stdout);
retval |= test_bandwidth(); fflush(stdout);
retval |= test_time(); fflush(stdout);
retval |= test_key(); fflush(stdout);
retval |= test_attribute(); fflush(stdout);
retval |= test_media(); fflush(stdout);
retval |= test_build(); fflush(stdout);
return retval;
}
......@@ -317,15 +317,15 @@ int soa_base_init(char const *name,
{
if (parent) {
#define DUP(d, dup, s) if ((s) && !((d) = dup(ss->ss_home, (s)))) return -1
su_home_t *h = ss->ss_home;
su_home_t *home = ss->ss_home;
if (soa_description_dup(h, ss->ss_caps, parent->ss_caps) < 0)
if (soa_description_dup(home, ss->ss_caps, parent->ss_caps) < 0)
return -1;
if (soa_description_dup(h, ss->ss_user, parent->ss_user) < 0)
if (soa_description_dup(home, ss->ss_user, parent->ss_user) < 0)
return -1;
if (soa_description_dup(h, ss->ss_local, parent->ss_local) < 0)
if (soa_description_dup(home, ss->ss_local, parent->ss_local) < 0)
return -1;
if (soa_description_dup(h, ss->ss_remote, parent->ss_remote) < 0)
if (soa_description_dup(home, ss->ss_remote, parent->ss_remote) < 0)
return -1;
DUP(ss->ss_address, su_strdup, parent->ss_address);
......@@ -1486,7 +1486,7 @@ int soa_set_sdp(soa_session_t *ss,
char const *sdp_str, int str_len)
{
struct soa_description *ssd;
int flags, new_version;
int flags, new_version, retval;
sdp_parser_t *parser = NULL;
sdp_session_t sdp[1];
......@@ -1545,14 +1545,23 @@ int soa_set_sdp(soa_session_t *ss,
switch (what) {
case soa_capability_sdp_kind:
return ss->ss_actions->soa_set_capability_sdp(ss, sdp, sdp_str, str_len);
retval = ss->ss_actions->soa_set_capability_sdp(ss, sdp, sdp_str, str_len);
break;
case soa_user_sdp_kind:
return ss->ss_actions->soa_set_user_sdp(ss, sdp, sdp_str, str_len);
retval = ss->ss_actions->soa_set_user_sdp(ss, sdp, sdp_str, str_len);
break;
case soa_remote_sdp_kind:
return ss->ss_actions->soa_set_remote_sdp(ss, 1, sdp, sdp_str, str_len);
retval = ss->ss_actions->soa_set_remote_sdp(ss, 1, sdp, sdp_str, str_len);
break;
default:
return -1;
retval = soa_set_status(ss, 500, "Internal Error");
break;
}
if (parser)
sdp_parser_free(parser);
return retval;
}
......@@ -1612,10 +1621,8 @@ int soa_description_dup(su_home_t *home,
struct soa_description const *ssd0)
{
if (ssd0->ssd_sdp) {
int len = ssd0->ssd_str ? strlen(ssd0->ssd_str) + 1 : 0;
ssd->ssd_sdp = sdp_session_dup(home, ssd0->ssd_sdp);
ssd->ssd_printer = sdp_print(home, ssd->ssd_sdp, NULL, len, 0);
ssd->ssd_printer = sdp_print(home, ssd->ssd_sdp, NULL, 0, 0);
ssd->ssd_str = (char *)sdp_message(ssd->ssd_printer);
if (ssd0->ssd_str != ssd0->ssd_unparsed)
ssd->ssd_unparsed = su_strdup(home, ssd0->ssd_unparsed);
......
......@@ -207,8 +207,6 @@ sdp_media_t *soa_sdp_make_rejected_media(su_home_t *home,
int include_all_codecs)
{
sdp_media_t rejected[1] = {{ sizeof (rejected) }};
sdp_list_t format[1] = {{ sizeof (format), NULL, "x" }};
sdp_rtpmap_t rtpmap[1] = {{ sizeof (rtpmap) }};
rejected->m_type = m->m_type;
rejected->m_type_name = m->m_type_name;
......@@ -218,15 +216,6 @@ sdp_media_t *soa_sdp_make_rejected_media(su_home_t *home,
if (include_all_codecs) {
rejected->m_rtpmaps = m->m_rtpmaps;
rejected->m_format = format;
}
else if (m->m_proto == sdp_proto_rtp) {
rtpmap->rm_predef = 1; rtpmap->rm_pt = 9;
rtpmap->rm_encoding = "*"; rtpmap->rm_rate = 8000;
rejected->m_rtpmaps = rtpmap;
}
else {
rejected->m_format = format;
}
rejected->m_rejected = 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment