From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Ktt53-00054o-4K for qemu-devel@nongnu.org; Sat, 25 Oct 2008 20:02:05 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Ktt50-000544-Sz for qemu-devel@nongnu.org; Sat, 25 Oct 2008 20:02:04 -0400 Received: from [199.232.76.173] (port=48953 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ktt50-00053y-KV for qemu-devel@nongnu.org; Sat, 25 Oct 2008 20:02:02 -0400 Received: from bsdimp.com ([199.45.160.85]:56497 helo=harmony.bsdimp.com) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1Ktt4z-0003ry-T7 for qemu-devel@nongnu.org; Sat, 25 Oct 2008 20:02:02 -0400 Date: Sat, 25 Oct 2008 17:58:38 -0600 (MDT) Message-Id: <20081025.175838.74717763.imp@bsdimp.com> Subject: Re: [Qemu-devel] [5532] Replace uses of strndup (a GNU extension) with Qemu pstrdup From: Warner Losh In-Reply-To: References: Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org, balrogg@gmail.com From: "andrzej zaborowski" Subject: Re: [Qemu-devel] [5532] Replace uses of strndup (a GNU extension) with Qemu pstrdup Date: Sat, 25 Oct 2008 14:03:40 +0200 > 2008/10/25 Blue Swirl : > > Revision: 5532 > > http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532 > > Author: blueswir1 > > Date: 2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008) > > > > Log Message: > > ----------- > > Replace uses of strndup (a GNU extension) with Qemu pstrdup > > > > Modified Paths: > > -------------- > > trunk/cutils.c > > trunk/hw/bt-hci.c > > trunk/qemu-common.h > > > > Modified: trunk/cutils.c > > =================================================================== > > --- trunk/cutils.c 2008-10-25 11:21:28 UTC (rev 5531) > > +++ trunk/cutils.c 2008-10-25 11:23:27 UTC (rev 5532) > > @@ -50,6 +50,18 @@ > > return buf; > > } > > > > +/* strdup with a limit */ > > +char *pstrdup(const char *str, size_t buf_size) > > +{ > > + size_t len; > > + char *buf; > > + > > + len = MIN(buf_size, strlen(str)); > > + buf = qemu_malloc(len); > > + pstrcpy(buf, len, str); > > + return buf; > > +} > > I think here also pstrcpy will only copy up to buf_size - 1 characters > while strndup would copy buf_size chars. Is this really safe? If str is very long, this can still run off into the weeds... Warner