Re: [PATCH] builtin/mv.c: use correct type to compute size of an array element
- From
Jeff King <peff@peff.net>
- Date
- Jul 7, 2022, 18:42 UTC
- Message-ID
- <YscpDTgwxUko/aZM@coredump.intra.peff.net>
- In-Reply-To
- <xmqq8rp54r4l.fsf@gitster.g>
On Wed, Jul 06, 2022 at 07:02:18PM -0700, Junio C Hamano wrote:
Show 8 quoted lines
> * Should we in general use sizeof(TYPE) in these cases, instead > of the size of the zeroth element, e.g. > > memmove(source + i, source + i + 1, > n * sizeof(source[i])); > > It would have been caught by the above Coccinelle rule (we are > taking the size of *dst).
I'm not sure I understand this. As you noted in a later email, using sizeof(TYPE) is less maintainable if the type of "source" changes. But later you mention using "*source" instead of "source[i]". I don't think there is a particular reason to prefer one over the other from the compiler perspective. I find "*source" more idiomatic (but better still of course is MOVE_ARRAY, which removes the choice entirely).
Show 13 quoted lines
> * Shouldn't we have an array of struct with four members, instead
> of four parallel arrays, e.g.
>
> struct {
> const char *source;
> const char *destination;
> enum update_mode mode;
> const char *submodule_gitfile;
> } *mv_file;
>
> The latter question is important to answer before we accept
> Coccinelle-suggested rewrite to use four MOVE_ARRAY() on these
> four parallel arrays on top of this fix.I think that would make the code a lot cleaner. But it looks like "source" and "destination" come from separate calls to internal_prefix_pathspec(). So you'd have to reconcile that. And there's some extra trickiness that sometimes "destination" comes from "dest_path", which _isn't_ always the same size as "source".
So I suspect the code which uses these arrays would be cleaner with a struct, but the setup may get worse. :)
-Peff