Re: [PATCH v2 1/8] fast-import: tighten path unquoting
- From
Thalia Archibald <thalia@archibald.dev>
- Date
- Apr 10, 2024, 08:44 UTC
- Message-ID
- <784B5318-F155-4D93-8085-62ACA63432E5@archibald.dev>
- In-Reply-To
- <CAPx1GvfgC46n_5fk3dHxg7dn393UVMi0CtHKqQ_GAaqnV_ECCQ@mail.gmail.com>
On Apr 10, 2024, at 01:18, Chris Torek <chris.torek@gmail.com> wrote:
Show 19 quoted lines
> On Tue, Apr 9, 2024 at 11:30 PM Patrick Steinhardt <ps@pks.im> wrote: >>> + if (include_spaces) >>> + *endp = p + strlen(p); >>> + else >>> + *endp = strchr(p, ' '); >>> + strbuf_add(sb, p, *endp - p); >> >> strchr(3P) may return a NULL pointer in case there is no space, which >> would make us segfault here when dereferencing `*endp`. We should >> probably add a testcase that would hit this edge case. > > Note that you can do: > > *endp = p + strcspn(p, " "); > > (though `strcspn` is a fundamentally harder operation since it > takes a string argument). Everything depends on whether you > want to test for an explicit "there was no space at all" case of > course; performance considerations are secondary.
I thought strchr returned a pointer to the terminating NUL byte if the needle was not found. Turns out it does return NULL in that case, as you say. strchrnul does what I want here and I’ve replaced it with that.
I’ve added a test covering this case.
Thalia