Fixed bug in 'url_to_hostname' function

master
Aadhavan Srinivasan 2 years ago
parent 7c17fcadcf
commit d50e4c7036

@ -20,7 +20,8 @@ char* url_to_proto(char* url) {
char* url_to_hostname(char* url) { char* url_to_hostname(char* url) {
/* This function relies on a hack-y assumption: that, given a string /* 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 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); size_t length = strlen(url);
char* protocol = url_to_proto(url); char* protocol = url_to_proto(url);
@ -41,6 +42,11 @@ char* url_to_hostname(char* url) {
sentinel = i; sentinel = i;
} }
} }
if (sentinel == 0) { /* If there are no more slashes */
return temp_substring;
}
strncpy(to_return,temp_substring,sentinel); strncpy(to_return,temp_substring,sentinel);
free(temp_substring); free(temp_substring);
return to_return; return to_return;

Loading…
Cancel
Save