diff options
| author | René Scharfe <l.s.r@web.de> | 2025-07-09 11:45:33 +0200 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2025-07-09 08:39:28 -0700 |
| commit | 5228211c4b92052c0a38f2ab67cd0b87a7baec30 (patch) | |
| tree | dde177448f99f7729ca341cd86442aa4bb897725 /parse-options.c | |
| parent | parse-options: add precision handling for OPTION_SET_INT (diff) | |
| download | git-5228211c4b92052c0a38f2ab67cd0b87a7baec30.tar.gz git-5228211c4b92052c0a38f2ab67cd0b87a7baec30.zip | |
parse-options: add precision handling for OPTION_BIT
Similar to 09705696f7 (parse-options: introduce precision handling for
`OPTION_INTEGER`, 2025-04-17) support value variables of different sizes
for OPTION_BIT. Do that by requiring their "precision" to be set,
casting their "value" pointer accordingly and checking whether the value
fits.
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.c | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/parse-options.c b/parse-options.c index 639f41b83b..b5c877d5e1 100644 --- a/parse-options.c +++ b/parse-options.c @@ -88,6 +88,14 @@ static int do_get_int_value(const void *value, size_t precision, intmax_t *ret) } } +static intmax_t get_int_value(const struct option *opt, enum opt_parsed flags) +{ + intmax_t ret; + if (do_get_int_value(opt->value, opt->precision, &ret)) + BUG("invalid precision for option %s", optname(opt, flags)); + return ret; +} + static enum parse_opt_result set_int_value(const struct option *opt, enum opt_parsed flags, intmax_t value) @@ -139,11 +147,14 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p, return opt->ll_callback(p, opt, NULL, unset); case OPTION_BIT: + { + intmax_t value = get_int_value(opt, flags); if (unset) - *(int *)opt->value &= ~opt->defval; + value &= ~opt->defval; else - *(int *)opt->value |= opt->defval; - return 0; + value |= opt->defval; + return set_int_value(opt, flags, value); + } case OPTION_NEGBIT: if (unset) @@ -631,11 +642,11 @@ static void parse_options_check(const struct option *opts) optbug(opts, "OPTION_SET_INT 0 should not be negatable"); switch (opts->type) { case OPTION_SET_INT: + case OPTION_BIT: 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_NUMBER: case OPTION_BITOP: |
