client: eliminate destructive parameter parsing
The function mgcp_response_parse_params() that is used to parse the
SDP parameters edits the content of the r->body.
- Create a local copy of r->body and work on this copy to keep
the original r-body in its original state.
Change-Id: Ia475036f7f3802b1638e0511a5e9162fea1592eb
diff --git a/src/libosmo-mgcp-client/mgcp_client.c b/src/libosmo-mgcp-client/mgcp_client.c
index a394f3b..017911d 100644
--- a/src/libosmo-mgcp-client/mgcp_client.c
+++ b/src/libosmo-mgcp-client/mgcp_client.c
@@ -244,24 +244,25 @@
int rc;
OSMO_ASSERT(r->body);
char *data = mgcp_find_section_end(r->body);
+ char *data_ptr;
- /* Warning: This function performs a destructive parsing on r->body.
- * Since this function is called at the very end of the persing
- * process, destructive parsing is acceptable. */
+ /* Since this functions performs a destructive parsing, we create a
+ * local copy of the body data */
+ data = talloc_zero_size(NULL, strlen(r->body)+1);
+ OSMO_ASSERT(data);
+ data_ptr = data;
+ osmo_strlcpy(data, r->body, strlen(r->body));
+ /* Find beginning of the parameter (SDP) section */
+ data_ptr = mgcp_find_section_end(data);
if (!data) {
LOGP(DLMGCP, LOGL_ERROR,
"MGCP response: cannot find start of parameters\n");
- return -EINVAL;
+ rc = -EINVAL;
+ goto exit;
}
- /* Advance to after the \n\n, replace the second \n with \0. That's
- * where the parameters start. */
- data ++;
- *data = '\0';
- data ++;
-
- for_each_non_empty_line(line, data) {
+ for_each_non_empty_line(line, data_ptr) {
if (!mgcp_line_is_valid(line))
return -EINVAL;
@@ -269,19 +270,23 @@
case 'm':
rc = mgcp_parse_audio_port(r, line);
if (rc)
- return rc;
+ goto exit;
break;
case 'c':
rc = mgcp_parse_audio_ip(r, line);
if (rc)
- return rc;
+ goto exit;
break;
default:
/* skip unhandled parameters */
break;
}
}
- return 0;
+
+ rc = 0;
+exit:
+ talloc_free(data);
+ return rc;
}
/* Parse a line like "X: something" */