parse-options: add precision handling for OPTION_NEGBIT

Similar to 09705696f7 (parse-options: introduce precision handling for `OPTION_INTEGER`, 2025-04-17) support value variables of different sizes for OPTION_NEGBIT. 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>

René Scharfe committed Jul 9, 2025 at 11:45 UTC feeebbf1b7d5ed8761355d354e9529c791b77e7d
3 files changed +9 -4
builtin/rebase.c
+1
@@ -1128,6 +1128,7 @@ int cmd_rebase(int argc,
1128 .short_name = 'n',
1129 .long_name = "no-stat",
1130 .value = &options.flags,
1131 + .precision = sizeof(options.flags),
1132 .help = N_("do not show diffstat of what changed upstream"),
1133 .flags = PARSE_OPT_NOARG,
1134 .defval = REBASE_DIFFSTAT,
parse-options.c
+7 -4
@@ -157,11 +157,14 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
157 }
158
159 case OPTION_NEGBIT:
160 + {
161 + intmax_t value = get_int_value(opt, flags);
162 if (unset)
161 - *(int *)opt->value |= opt->defval;
163 + value |= opt->defval;
164 else
163 - *(int *)opt->value &= ~opt->defval;
164 - return 0;
165 + value &= ~opt->defval;
166 + return set_int_value(opt, flags, value);
167 + }
168
169 case OPTION_BITOP:
170 if (unset)
@@ -643,11 +646,11 @@ static void parse_options_check(const struct option *opts)
646 switch (opts->type) {
647 case OPTION_SET_INT:
648 case OPTION_BIT:
649 + case OPTION_NEGBIT:
650 if (!signed_int_fits(opts->defval, opts->precision))
651 optbug(opts, "has invalid defval");
652 /* fallthru */
653 case OPTION_COUNTUP:
650 - case OPTION_NEGBIT:
654 case OPTION_NUMBER:
655 case OPTION_BITOP:
656 if ((opts->flags & PARSE_OPT_OPTARG) ||
parse-options.h
+1
@@ -250,6 +250,7 @@ struct option {
250 .short_name = (s), \
251 .long_name = (l), \
252 .value = (v), \
253 + .precision = sizeof(*v), \
254 .help = (h), \
255 .flags = PARSE_OPT_NOARG, \
256 .defval = (b), \