aboutsummaryrefslogtreecommitdiffstats
path: root/parse-options.c
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2025-07-09 11:45:24 +0200
committerJunio C Hamano <gitster@pobox.com>2025-07-09 08:39:26 -0700
commitc898bbc5e4b582c28379bc64b7f9c9ec96106993 (patch)
treeddff138bb55adc7225edbf3088667818ac4d33e5 /parse-options.c
parent0d3e045b34f38d23e6160ce8aae363f358bd5cdc (diff)
downloadgit-c898bbc5e4b582c28379bc64b7f9c9ec96106993.tar.gz
parse-options: add precision handling for OPTION_SET_INT
Similar to 09705696f7 (parse-options: introduce precision handling for `OPTION_INTEGER`, 2025-04-17) support value variables of different sizes for OPTION_SET_INT. Do that by requiring their "precision" to be set, casting their "value" pointer accordingly and checking whether the value fits. Factor out the casting code from the part of do_get_value() that handles OPTION_INTEGER to avoid code duplication. We're going to use it in the next patches as well. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'parse-options.c')
-rw-r--r--parse-options.c56
1 files changed, 36 insertions, 20 deletions
diff --git a/parse-options.c b/parse-options.c
index ddac008a5e..639f41b83b 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -88,6 +88,36 @@ static int do_get_int_value(const void *value, size_t precision, intmax_t *ret)
}
}
+static enum parse_opt_result set_int_value(const struct option *opt,
+ enum opt_parsed flags,
+ intmax_t value)
+{
+ switch (opt->precision) {
+ case sizeof(int8_t):
+ *(int8_t *)opt->value = value;
+ return 0;
+ case sizeof(int16_t):
+ *(int16_t *)opt->value = value;
+ return 0;
+ case sizeof(int32_t):
+ *(int32_t *)opt->value = value;
+ return 0;
+ case sizeof(int64_t):
+ *(int64_t *)opt->value = value;
+ return 0;
+ default:
+ BUG("invalid precision for option %s", optname(opt, flags));
+ }
+}
+
+static int signed_int_fits(intmax_t value, size_t precision)
+{
+ size_t bits = precision * CHAR_BIT;
+ intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - bits);
+ intmax_t lower_bound = -upper_bound - 1;
+ return lower_bound <= value && value <= upper_bound;
+}
+
static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
const struct option *opt,
enum opt_parsed flags,
@@ -136,8 +166,7 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
return 0;
case OPTION_SET_INT:
- *(int *)opt->value = unset ? 0 : opt->defval;
- return 0;
+ return set_int_value(opt, flags, unset ? 0 : opt->defval);
case OPTION_STRING:
if (unset)
@@ -219,23 +248,7 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
return error(_("value %s for %s not in range [%"PRIdMAX",%"PRIdMAX"]"),
arg, optname(opt, flags), (intmax_t)lower_bound, (intmax_t)upper_bound);
- switch (opt->precision) {
- case 1:
- *(int8_t *)opt->value = value;
- return 0;
- case 2:
- *(int16_t *)opt->value = value;
- return 0;
- case 4:
- *(int32_t *)opt->value = value;
- return 0;
- case 8:
- *(int64_t *)opt->value = value;
- return 0;
- default:
- BUG("invalid precision for option %s",
- optname(opt, flags));
- }
+ return set_int_value(opt, flags, value);
}
case OPTION_UNSIGNED:
{
@@ -617,10 +630,13 @@ static void parse_options_check(const struct option *opts)
opts->long_name && !(opts->flags & PARSE_OPT_NONEG))
optbug(opts, "OPTION_SET_INT 0 should not be negatable");
switch (opts->type) {
+ case OPTION_SET_INT:
+ if (!signed_int_fits(opts->defval, opts->precision))
+ optbug(opts, "has invalid defval");
+ /* fallthru */
case OPTION_COUNTUP:
case OPTION_BIT:
case OPTION_NEGBIT:
- case OPTION_SET_INT:
case OPTION_NUMBER:
case OPTION_BITOP:
if ((opts->flags & PARSE_OPT_OPTARG) ||