From 6dcb9080255b1adb932048a35f81a3a045a35646 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Fri, 16 Dec 2022 15:00:42 +0800 Subject: [PATCH] res_http_media_cache: Do not crash when there is no extension Do not crash when a URL has no path component as in this case the ast_uri_path function will return NULL. Make the code cope with not having a path. The below would crash > media cache create http://google.com /tmp/foo.wav Thread 1 "asterisk" received signal SIGSEGV, Segmentation fault. 0x0000ffff836616cc in strrchr () from /lib/aarch64-linux-gnu/libc.so.6 (gdb) bt #0 0x0000ffff836616cc in strrchr () from /lib/aarch64-linux-gnu/libc.so.6 #1 0x0000ffff43d43a78 in file_extension_from_string (str=, buffer=buffer@entry=0xffffca9973c0 "", capacity=capacity@entry=64) at res_http_media_cache.c:288 #2 0x0000ffff43d43bac in file_extension_from_url_path (bucket_file=bucket_file@entry=0x3bf96568, buffer=buffer@entry=0xffffca9973c0 "", capacity=capacity@entry=64) at res_http_media_cache.c:378 #3 0x0000ffff43d43c74 in bucket_file_set_extension (bucket_file=bucket_file@entry=0x3bf96568) at res_http_media_cache.c:392 #4 0x0000ffff43d43d10 in bucket_file_run_curl (bucket_file=0x3bf96568) at res_http_media_cache.c:555 #5 0x0000ffff43d43f74 in bucket_http_wizard_create (sorcery=, data=, object=) at res_http_media_cache.c:613 #6 0x0000000000487638 in bucket_file_wizard_create (sorcery=, data=, object=) at bucket.c:191 #7 0x0000000000554408 in sorcery_wizard_create (object_wizard=object_wizard@entry=0x3b9f0718, details=details@entry=0xffffca9974a8) at sorcery.c:2027 #8 0x0000000000559698 in ast_sorcery_create (sorcery=, object=object@entry=0x3bf96568) at sorcery.c:2077 #9 0x00000000004893a4 in ast_bucket_file_create (file=file@entry=0x3bf96568) at bucket.c:727 #10 0x00000000004f877c in ast_media_cache_create_or_update (uri=0x3bfa1103 "https://google.com", file_path=0x3bfa1116 "/tmp/foo.wav", metadata=metadata@entry=0x0) at media_cache.c:335 #11 0x00000000004f88ec in media_cache_handle_create_item (e=, cmd=, a=0xffffca9976b8) at media_cache.c:640 ASTERISK-30375 #close Change-Id: I6a9433688cb5d3d4be8758b7642d923bdde6c273 --- res/res_http_media_cache.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/res/res_http_media_cache.c b/res/res_http_media_cache.c index 0ad5ea8047..dfafdc767f 100644 --- a/res/res_http_media_cache.c +++ b/res/res_http_media_cache.c @@ -245,6 +245,7 @@ static char *file_extension_from_content_type(struct ast_bucket_file *bucket_fil static char *file_extension_from_url_path(struct ast_bucket_file *bucket_file, char *buffer, size_t capacity) { + const char *path; struct ast_uri *uri; uri = ast_uri_parse(ast_sorcery_object_get_id(bucket_file)); @@ -254,8 +255,14 @@ static char *file_extension_from_url_path(struct ast_bucket_file *bucket_file, c return NULL; } + path = ast_uri_path(uri); + if (!path) { + ao2_cleanup(uri); + return NULL; + } + /* Just parse it as a string like before, but without the extra cruft */ - buffer = file_extension_from_string(ast_uri_path(uri), buffer, capacity); + buffer = file_extension_from_string(path, buffer, capacity); ao2_cleanup(uri); return buffer; }