From: Patrick Steinhardt <ps@pks.im>
To: CAPig+cQ49Hdc_8=mRhhJDTny_Kqo6Wg6Nr98rsBN_YXmBrQ6kA@mail.gmail.com
Cc: git@vger.kernel.org, gitster@pobox.com,
phillip.wood123@gmail.com, sunshine@sunshineco.com,
"Sören Krecker" <soekkle@freenet.de>
Subject: Re: [PATCH v3 1/4] add-patch: Fix type conversion warnings from msvc
Date: Mon, 27 Jan 2025 08:26:08 +0100 [thread overview]
Message-ID: <Z5c1EIXi7nsB2kJe@pks.im> (raw)
In-Reply-To: <20250126125638.3089-2-soekkle@freenet.de>
Note: the word after the subject's subsystem should start with a
lower-case letter.
On Sun, Jan 26, 2025 at 01:56:35PM +0100, Sören Krecker wrote:
> Fix some compiler warnings from msvc in add-patch.c for value truncation
> form 64 bit to 32 bit integers. Change unsigned long to size_t for
> correct variable size on linux and windows.
> Add macro str_to_size_t for converting a string to size_t.
There shouldn't be a need for this macro, we already have `strtoumax()`.
And in case the platform doesn't provide it we know to provide our own
implementation.
> Test if convertion fails with over or underflow.
s/convertion/conversion/
> diff --git a/add-patch.c b/add-patch.c
> index 95c67d8c80..4fb6ae2c4b 100644
> --- a/add-patch.c
> +++ b/add-patch.c
> @@ -322,11 +322,12 @@ static void setup_child_process(struct add_p_state *s,
> }
>
> static int parse_range(const char **p,
> - unsigned long *offset, unsigned long *count)
> + size_t *offset, size_t *count)
> {
> char *pend;
> -
> - *offset = strtoul(*p, &pend, 10);
> + *offset = str_to_size_t(*p, &pend, 10);
> + if (errno == ERANGE)
> + return error(_("Number is too large for this field"));
Error messages should start with a lower-case letter.
> if (pend == *p)
> return -1;
> if (*pend != ',') {
> @@ -334,7 +335,9 @@ static int parse_range(const char **p,
> *p = pend;
> return 0;
> }
> - *count = strtoul(pend + 1, (char **)p, 10);
> + *count = str_to_size_t(pend + 1, (char **)p, 10);
> + if (errno == ERANGE)
> + return error(_("Number is too large for this field"));
Here, too.
> @@ -1066,11 +1071,13 @@ static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
>
> /* last hunk simply gets the rest */
> if (header->old_offset != remaining.old_offset)
> - BUG("miscounted old_offset: %lu != %lu",
> - header->old_offset, remaining.old_offset);
> + BUG("miscounted old_offset: %"PRIuMAX" != %"PRIuMAX,
> + (uintmax_t)header->old_offset,
> + (uintmax_t)remaining.old_offset);
> if (header->new_offset != remaining.new_offset)
> - BUG("miscounted new_offset: %lu != %lu",
> - header->new_offset, remaining.new_offset);
> + BUG("miscounted new_offset: %"PRIuMAX" != %"PRIuMAX,
> + (uintmax_t)header->new_offset,
> + (uintmax_t)remaining.new_offset);
> header->old_count = remaining.old_count;
> header->new_count = remaining.new_count;
> hunk->end = end;
I feel like most of the changes are adapting formatting directives like
this. Might be worthwhile to separate into a standalone commit. That'd
also allow the commit message to read less like a list of bullet points
and provide more context, explaining the actual change.
> diff --git a/gettext.h b/gettext.h
> index 484cafa562..d36f5a7ade 100644
> --- a/gettext.h
> +++ b/gettext.h
> @@ -53,7 +53,7 @@ static inline FORMAT_PRESERVING(1) const char *_(const char *msgid)
> }
>
> static inline FORMAT_PRESERVING(1) FORMAT_PRESERVING(2)
> -const char *Q_(const char *msgid, const char *plu, unsigned long n)
> +const char *Q_(const char *msgid, const char *plu, size_t n)
> {
> if (!git_gettext_enabled)
> return n == 1 ? msgid : plu;
This change feels completely unrelated to all the other changes. It
would probably warrant a new commit.
> diff --git a/git-compat-util.h b/git-compat-util.h
> index e283c46c6f..bb9a6c2bc4 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -292,6 +292,13 @@ static inline int _have_unix_sockets(void)
> #include <sys/sysctl.h>
> #endif
>
> +#if SIZE_MAX == ULONG_MAX
> +#define str_to_size_t strtoul
> +#else
> +#define str_to_size_t strtoull
> +#endif
Hm. A couple of comments:
- The function name doesn't match the schema of function names we
already have. I would rather have expected it to be called something
like `strtouz()` or something like that.
- We tend to avoid using `strtoul()` and friends directly, as they are
really hard to get right. See the implementation of `strtoul_ui()`
for all the checks we do there.
- The way the macro is implemented feels quite fragile.
So I'd propose to adapt the approach a bit and introduce a new function
`strtoumax_ui()`:
static inline int strtoumax_ui(char *const *s, int base, unsigned
uintmax_t max, int *result);
The implementation would mostly follow what we have in `strotul_ui()`.
The `max` parameter here could be used to control the maximum that the
caller expects -- if the parsed integer exceeds it, it would return an
error and set `ERANGE`. If we had such a helper, we can then also
reimplement `strtoul_ui()` on top of that function with a simple call to
`strtoumax_ui(s, base, UINT_MAX, result)`.
This would overall be a lot more flexible than what we currently have.
Patrick
next prev parent reply other threads:[~2025-01-27 7:26 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-26 12:56 [PATCH v3 0/4] Fix type conversion Warings from msvc Sören Krecker
2025-01-26 12:56 ` [PATCH v3 1/4] add-patch: Fix type conversion warnings " Sören Krecker
2025-01-27 7:26 ` Patrick Steinhardt [this message]
2025-01-27 16:10 ` Junio C Hamano
2025-01-29 16:51 ` Phillip Wood
2025-01-29 19:52 ` Junio C Hamano
2025-01-30 10:47 ` Phillip Wood
2025-01-30 19:24 ` Junio C Hamano
2025-01-27 7:26 ` [PATCH v3 0/4] Fix type conversion Warings " Patrick Steinhardt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z5c1EIXi7nsB2kJe@pks.im \
--to=ps@pks.im \
--cc=CAPig+cQ49Hdc_8=mRhhJDTny_Kqo6Wg6Nr98rsBN_YXmBrQ6kA@mail.gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=phillip.wood123@gmail.com \
--cc=soekkle@freenet.de \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.