From: "Blue Swirl" <blauwirbel@gmail.com>
To: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [5532] Replace uses of strndup (a GNU extension) with Qemu pstrdup
Date: Sat, 25 Oct 2008 17:31:39 +0300 [thread overview]
Message-ID: <f43fc5580810250731r7f619619t75ae42605ed045b6@mail.gmail.com> (raw)
In-Reply-To: <fb249edb0810250715t7005389fj43e3d39439afe9b5@mail.gmail.com>
On 10/25/08, andrzej zaborowski <balrogg@gmail.com> wrote:
> 2008/10/25 Blue Swirl <blauwirbel@gmail.com>:
> > On 10/25/08, andrzej zaborowski <balrogg@gmail.com> wrote:
> >> 2008/10/25 Blue Swirl <blauwirbel@gmail.com>:
> >> > On 10/25/08, andrzej zaborowski <balrogg@gmail.com> wrote:
> >> >> 2008/10/25 Blue Swirl <blauwirbel@gmail.com>:
> >> >>
> >> >> > 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.
> >> >
> >> > That is actually safer if we always want the strings to be NUL terminated.
> >>
> >>
> >> strndup also always NUL terminates the string so it's just as safe,
> >> the length is just different.
> >>
> >>
> >> >
> >> > But the allocation length is wrong, it should be MIN(buf_size, strlen(str) + 1).
> >>
> >>
> >> By my reading of the manual, it should rather be MIN(buf_size, strlen(str)) + 1.
> >
> > But then the length could be incorrect: buf_size + 1.
>
>
> That's what it should be in case lmp_name is 248 chars long. I think
> the confusion is because you called the parameter buf_size while
> strndup calls it n (the number of characters.. not buffer size).
If strlen(lmp_name) >= 248, only first 247 should be copied and the
final character should be NUL, because sizeof(read_local_name_rp.name)
== buf_size == 248.
> Note that you can use memcpy instead of pstrcpy because the length is
> known so there's no need for the overhead.
Right.
next prev parent reply other threads:[~2008-10-25 14:31 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-25 11:23 [Qemu-devel] [5532] Replace uses of strndup (a GNU extension) with Qemu pstrdup Blue Swirl
2008-10-25 12:03 ` andrzej zaborowski
2008-10-25 12:18 ` Blue Swirl
2008-10-25 13:07 ` andrzej zaborowski
2008-10-25 13:49 ` Blue Swirl
2008-10-25 14:15 ` andrzej zaborowski
2008-10-25 14:31 ` Blue Swirl [this message]
2008-10-25 14:48 ` andrzej zaborowski
2008-10-26 9:00 ` Blue Swirl
2008-10-26 10:24 ` andrzej zaborowski
2008-10-25 23:58 ` Warner Losh
2008-11-01 18:19 ` andrzej zaborowski
2008-11-01 18:31 ` Blue Swirl
2008-11-01 18:47 ` andrzej zaborowski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f43fc5580810250731r7f619619t75ae42605ed045b6@mail.gmail.com \
--to=blauwirbel@gmail.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).