[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-ppc] [Qemu-devel] [libfdt][PATCH v2] implement strnlen for sys
From: |
David Gibson |
Subject: |
Re: [Qemu-ppc] [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it |
Date: |
Tue, 24 Oct 2017 18:31:23 +0200 |
User-agent: |
Mutt/1.9.1 (2017-09-22) |
On Tue, Oct 24, 2017 at 12:16:47AM -0400, Programmingkid wrote:
>
> > On Oct 22, 2017, at 1:33 AM, David Gibson <address@hidden> wrote:
> >
> > On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
> >> On 10/20/2017 10:55 AM, John Arbuckle wrote:
> >>> +static inline size_t strnlen(const char *string, size_t max_count)
> >>> +{
> >>> + size_t count;
> >>> + for (count = 0; count < max_count; count++) {
> >>> + if (string[count] == '\0') {
> >>> + break;
> >>> + }
> >>> + }
> >>> + return count;
> >>
> >> Not to nitpick, but
> >>
> >> const char *p = memchr(string, 0, max_count);
> >> return p ? max_count : p - string;
> >
> > Richard's right, that's definitely a better implementation.
>
> I was just wondering, what if we rewrote the code to use strlen()
> instead of strnlen(). Would that be an acceptable solution?
Only if you can do so safely - i.e. without accessing memory beyond
what we're supposed to. I don't think you'll be able to do that
without effectively re-implementing strnlen(), there's a reason I used
it in the first place, after all.
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
signature.asc
Description: PGP signature
Re: [Qemu-ppc] [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it, Peter Maydell, 2017/10/22