From a368800b26b3e9715e111a20a4d06604f13cc660 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Wed, 2 Jun 2021 09:12:27 -0400 Subject: [PATCH] net.urllib: don't crash on str() if url is missing host (#10313) --- vlib/net/urllib/urllib.v | 4 ++-- vlib/net/urllib/urllib_test.v | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/vlib/net/urllib/urllib.v b/vlib/net/urllib/urllib.v index b0a6b888ad..ddeb3472a7 100644 --- a/vlib/net/urllib/urllib.v +++ b/vlib/net/urllib/urllib.v @@ -374,7 +374,7 @@ pub: } fn (u &Userinfo) empty() bool { - return u.username == '' && u.password == '' + return isnil(u) || (u.username == '' && u.password == '') } // string returns the encoded userinfo information in the standard form @@ -737,7 +737,7 @@ pub fn (u URL) str() string { if u.opaque != '' { buf.write_string(u.opaque) } else { - if u.scheme != '' || u.host != '' || (u.user != 0 && !u.user.empty()) { + if u.scheme != '' || u.host != '' || !u.user.empty() { if u.host != '' || u.path != '' || !u.user.empty() { buf.write_string('//') } diff --git a/vlib/net/urllib/urllib_test.v b/vlib/net/urllib/urllib_test.v index 2d354efda7..0870c81bbd 100644 --- a/vlib/net/urllib/urllib_test.v +++ b/vlib/net/urllib/urllib_test.v @@ -43,3 +43,9 @@ fn test_parse_query() ? { assert q1.data['format'].data == ['"%l: %c %t"'] assert q2.data['format'].data == ['"%l: %c %t"'] } + +fn test_parse_missing_host() ? { + // issue #10311 + url := urllib.parse('http:///') ? + assert url.str() == 'http://///' +}