From d50e4c70360404d96744438297a19fcc81327644 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Fri, 10 Mar 2023 09:37:51 -0600 Subject: [PATCH] Fixed bug in 'url_to_hostname' function --- url_manipulation.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/url_manipulation.c b/url_manipulation.c index 98841ee..be58483 100644 --- a/url_manipulation.c +++ b/url_manipulation.c @@ -20,7 +20,8 @@ char* url_to_proto(char* url) { char* url_to_hostname(char* url) { /* This function relies on a hack-y assumption: that, given a string that represents a URL (e.g. https://www.example.com/index.html), the length of the host portion - of the address is the distance between the second slash (:/'/') and the third ('/'index.html). */ + of the address is the distance between the second slash (:/'/') and the third ('/'index.html), + or the second slash and the end of the string, if the path isn't present. */ size_t length = strlen(url); char* protocol = url_to_proto(url); @@ -41,6 +42,11 @@ char* url_to_hostname(char* url) { sentinel = i; } } + + + if (sentinel == 0) { /* If there are no more slashes */ + return temp_substring; + } strncpy(to_return,temp_substring,sentinel); free(temp_substring); return to_return;