Received: from eggs.gnu.org ([2001:4830:134:3::10]:39590) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XUd9N-0004K8-6T for qemu-devel@nongnu.org; Thu, 18 Sep 2014 10:57:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XUd9F-0000we-AI for qemu-devel@nongnu.org; Thu, 18 Sep 2014 10:57:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52969) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XUd9F-0000vw-2r for qemu-devel@nongnu.org; Thu, 18 Sep 2014 10:57:29 -0400 From: Markus Armbruster References: <1410248706-9769-1-git-send-email-mrezanin@redhat.com> <20140917092655.GD10699@stefanha-thinkpad.redhat.com> <878ulhyo6n.fsf@blackfin.pond.sub.org> Date: Thu, 18 Sep 2014 16:57:19 +0200 In-Reply-To: <878ulhyo6n.fsf@blackfin.pond.sub.org> (Markus Armbruster's message of "Thu, 18 Sep 2014 16:54:08 +0200") Message-ID: <87bnqdx9gw.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH] Prevent segmentation fault in case of relative resolve of uri List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi Cc: mrezanin@redhat.com, qemu-devel@nongnu.org Markus Armbruster writes: > Stefan Hajnoczi writes: > >> On Tue, Sep 09, 2014 at 09:45:06AM +0200, mrezanin@redhat.com wrote: >>> From: Miroslav Rezanina >>> >>> It was possible to call strcmp with NULL argument, that can cause >>> segmentation fault. Properly checking parameters to prevent this >>> situation. >>> >>> Signed-off-by: Miroslav Rezanina >>> --- >>> util/uri.c | 3 ++- >>> 1 file changed, 2 insertions(+), 1 deletion(-) >>> >>> diff --git a/util/uri.c b/util/uri.c >>> index e348c17..16c01d0 100644 >>> --- a/util/uri.c >>> +++ b/util/uri.c >>> @@ -1985,7 +1985,8 @@ uri_resolve_relative (const char *uri, const char * base) >>> val = g_strdup (uri); >>> goto done; >>> } >>> - if (!strcmp(bas->path, ref->path)) { >>> + if (bas->path != NULL && ref->path != NULL && >>> + !strcmp(bas->path, ref->path)) { >>> val = g_strdup(""); >>> goto done; >>> } >> >> This patch adds more conditions, what's needed is to audit and clean up this >> function. There is dead code and things could be simplified instead: >> >> if (!strcmp(bas->path, ref->path)) { >> val = g_strdup(""); >> goto done; >> } >> if (bas->path == NULL) { >> val = g_strdup(ref->path); >> goto done; >> } >> if (ref->path == NULL) { >> ref->path = (char *) "/"; >> remove_path = 1; >> } >> <---- bas->path cannot be NULL because we took goto done >> <---- ref->path cannot be NULL because we assigned "/" >> >> /* >> * At this point (at last!) we can compare the two paths >> * >> * First we take care of the special case where either of the >> * two path components may be missing (bug 316224) >> */ >> if (bas->path == NULL) { <--- dead code! >> if (ref->path != NULL) { >> uptr = ref->path; >> if (*uptr == '/') >> uptr++; >> /* exception characters from uri_to_string */ >> val = uri_string_escape(uptr, "/;&=+$,"); >> } >> goto done; >> } >> bptr = bas->path; >> if (ref->path == NULL) { <--- dead code! >> >> Also, perhaps the strcmp() you touched should really be moved below the NULL >> checks. > > If you simplify this function, please get rid of the silly conditionals > around g_strdup(). See commits 80e0090 c14e984 c64f50d for examples. Likewise: g_free(). See commits 9e28840 64641d8 f7047c2 fb7da62 fec0da9 ec15993.