From: Sean Bright Date: Thu, 9 Nov 2023 23:06:22 +0000 (-0500) Subject: uri.c: Simplify ast_uri_make_host_with_port() X-Git-Tag: 21.1.0-rc1~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6a75f22858a31de851cbafffc3fc1535d5913872;p=thirdparty%2Fasterisk.git uri.c: Simplify ast_uri_make_host_with_port() (cherry picked from commit e2e18b366c06f3cdfccf6fcfe03027676f316290) --- diff --git a/main/uri.c b/main/uri.c index 07493699bb..312ab45be2 100644 --- a/main/uri.c +++ b/main/uri.c @@ -299,25 +299,14 @@ struct ast_uri *ast_uri_parse_websocket(const char *uri) char *ast_uri_make_host_with_port(const struct ast_uri *uri) { - int host_size = ast_uri_host(uri) ? - strlen(ast_uri_host(uri)) : 0; - /* if there is a port +1 for the colon */ - int port_size = ast_uri_port(uri) ? - strlen(ast_uri_port(uri)) + 1 : 0; - char *res = ast_malloc(host_size + port_size + 1); + char *res; - if (!res) { + if (ast_asprintf(&res, "%s%s%s", + ast_uri_host(uri) ?: "", + ast_uri_port(uri) ? ":" : "", + ast_uri_port(uri) ?: "") == -1) { return NULL; } - memcpy(res, ast_uri_host(uri), host_size); - - if (ast_uri_port(uri)) { - res[host_size] = ':'; - memcpy(res + host_size + 1, - ast_uri_port(uri), port_size - 1); - } - - res[host_size + port_size] = '\0'; return res; }