Re: [PATCH 1/2] [GSOC] ref-filter: add %(raw) atom
- From
- Phillip Wood <phillip.wood123@gmail.com>
- Date
- May 29, 2021, 17:23 UTC
- Message-ID
- <e7db3342-7022-98db-e0d2-a880f874565e@gmail.com>
- In-Reply-To
- <60b25ca7a01c2_265088208af@natae.notmuch>
On 29/05/2021 16:24, Felipe Contreras wrote:
Show 21 quoted lines
> Phillip Wood wrote: >> On 27/05/2021 17:36, Felipe Contreras wrote: >>> ZheNing Hu via GitGitGadget wrote: >>> [...] >>>> +static int memcasecmp(const void *vs1, const void *vs2, size_t n) >>> >>> Why void *? We can delcare as char *. >> >> If you look at how this function is used you'll see >> int (*cmp_fn)(const void *, const void *, size_t); >> cmp_fn = s->sort_flags & REF_SORTING_ICASE >> ? memcasecmp : memcmp; > > Yeah, but why? > > We know we are comparing two char *. Presumably the reason is that > memcmp and memcasecmp use void *, but that could be remedied with: > > cmp_fn = (int (*)(const char *, const char *, size_t))memcmp; > > That way the same cmp_fn could be used for the two cases.
But that is still undefined behavior - the ugly cast just silences any compiler warning without making the code safe. It calls memcmp using a pointer of a different type. The type of cmp_fn and the two functions assigned to it must match.
Best Wishes
Phillip
Show 11 quoted lines
> Either way I don't care particularly much. It also could be possible to > use void * and do the casting in tolower(). > >>> (and I personally prefer lower to upper) >> >> We should be using tolower() as that is what POSIX specifies for >> strcasecmp() [1] which we are trying to emulate and there are cases[2] where >> (tolower(c1) == tolower(c2)) != (toupper(c1) == toupper(c2)) > > That's true. >