[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v4] cutils: Provide strchrnul
From: |
Markus Armbruster |
Subject: |
Re: [Qemu-devel] [PATCH v4] cutils: Provide strchrnul |
Date: |
Tue, 12 Jun 2018 09:07:42 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) |
Keno Fischer <address@hidden> writes:
>> Suggest return strchrnul("Hello World", 'W') != 6, to avoid worries
>> about a sufficiently smart compilers optimizing out a call that would
>> otherwise fail to link, say because headers don't match libraries.
>
> I'm happy to do that, but then again, a sufficiently smart compiler might
> constant fold this call entirely, so to be completely safe maybe we need
>
> extern char *haystack;
> extern char needle;
> int main(void) { return strchrnul(haystack, needle) != 6; }
>
> Though frankly if you're in a position for this to be a problem, you've
> got bigger problems. Happy to change this though.
Yes, please. You even get to pick your favorite number for the right
hand side of the comparison ;)
>> Should this be named HAVE_STRCHRNUL? It's how it would be named with
>> Autoconf...
>
> Ok, I will rename this.
>
>>> +const char *qemu_strchrnul(const char *s, int c)
>>> +{
>>> + const char *e = strchr(s, c);
>>> + if (!e) {
>>> + e = s + strlen(s);
>>> + }
>>> + return e;
>>
>> Stupidest solution that could possibly work. Okay :)
>
> Well, it's the pattern that was used everywhere in place of this function,
> so certainly from a commit factoring this seemed like the most sensible
> thing to do.
>
>> How did you find the spots to convert to strchrnul()?
>
> I audited uses of `strchr` and checked for whether they were really doing
> `strchrnul` (plus the one use case in code that used to only ever be compiled
> on Linux).
Sounds good. Thanks!