Received: from eggs.gnu.org ([2001:4830:134:3::10]:60337) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YLpVt-0003Xo-AR for qemu-devel@nongnu.org; Thu, 12 Feb 2015 03:52:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YLpVq-0002pD-4P for qemu-devel@nongnu.org; Thu, 12 Feb 2015 03:52:45 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41687) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YLpVp-0002p1-Ru for qemu-devel@nongnu.org; Thu, 12 Feb 2015 03:52:42 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t1C8qeEx029147 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Thu, 12 Feb 2015 03:52:40 -0500 From: Markus Armbruster References: <1423153463-26494-1-git-send-email-armbru@redhat.com> <1423153463-26494-3-git-send-email-armbru@redhat.com> <54DBA23F.6050409@redhat.com> Date: Thu, 12 Feb 2015 09:52:37 +0100 In-Reply-To: <54DBA23F.6050409@redhat.com> (Paolo Bonzini's message of "Wed, 11 Feb 2015 19:41:03 +0100") Message-ID: <87oaozbjfu.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PULL 2/4] coverity: Model GLib string allocation partially List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: qemu-devel@nongnu.org Paolo Bonzini writes: > On 05/02/2015 17:24, Markus Armbruster wrote: >> + >> +char *g_strdup(const char *s) >> +{ >> + char *dup; >> + size_t i; >> + >> + if (!s) { >> + return NULL; >> + } >> + >> + __coverity_string_null_sink__(s); >> + __coverity_string_size_sink__(s); > > What's __coverity_string_size_sink__? It is likely responsible for this > in libcacard: > > Unbounded source buffer (STRING_SIZE) > string_size: Passing string argv[argc - 2] of unknown size to g_strdup, > which expects a string of a particular size Yup, it is. The reference manual explains it "indicates to the STRING_SIZE checker that a function is a string size sink and must be protected from arbitrarily large strings." Of course, treating argv[] strings as unbounded in size is debatable. The OS generally imposes some limit. Linux's limit is rather generous: 32 pages, if I read execve(2) correctly. That aside, why on earth are we copying these strings? The only use of the copies is passing them to getaddrinfo() via connect_to_qemu(). > I guess it's okay to mark this as intentional? I guess it's okay to delete the copying. >> +char *g_strndup(const char *s, size_t n) >> +{ >> + char *dup; >> + size_t i; >> + >> + __coverity_negative_sink__(n); >> + >> + if (!s) { >> + return NULL; >> + } >> + >> + dup = g_malloc(n + 1); for (i = 0; i < n && (dup[i] = s[i]); i++) ; dup[i] = 0; return dup; } > > > This should be g_malloc0 I think. g_malloc0() is more faithful to what the function does, but I decided to use g_malloc() anyway, because I recommend not to rely on the padding behavior[*]. I think g_strndup() is fine when you want to duplicate a substring. No padding then. I figure this is the common use case. When you want to duplicate a string, but limit the size of the duplicate, then it doesn't do the right thing when the string is shorter than the limit. Use g_strdup_printf() instead. What it does is right for something else: allocating a buffer with a specific size and completely initializing it from a string. I can't remember having had a use for that myself. [*] Looks like the designers of g_strndup() couldn't resist the temptation to "improve" upon strndup(). Fine, but call it something else then.