Skip to content

Commit feeebbf

Browse files
rscharfegitster
authored andcommitted
parse-options: add precision handling for OPTION_NEGBIT
Similar to 0970569 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5228211 commit feeebbf

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

builtin/rebase.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,7 @@ int cmd_rebase(int argc,
11281128
.short_name = 'n',
11291129
.long_name = "no-stat",
11301130
.value = &options.flags,
1131+
.precision = sizeof(options.flags),
11311132
.help = N_("do not show diffstat of what changed upstream"),
11321133
.flags = PARSE_OPT_NOARG,
11331134
.defval = REBASE_DIFFSTAT,

parse-options.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,14 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
157157
}
158158

159159
case OPTION_NEGBIT:
160+
{
161+
intmax_t value = get_int_value(opt, flags);
160162
if (unset)
161-
*(int *)opt->value |= opt->defval;
163+
value |= opt->defval;
162164
else
163-
*(int *)opt->value &= ~opt->defval;
164-
return 0;
165+
value &= ~opt->defval;
166+
return set_int_value(opt, flags, value);
167+
}
165168

166169
case OPTION_BITOP:
167170
if (unset)
@@ -643,11 +646,11 @@ static void parse_options_check(const struct option *opts)
643646
switch (opts->type) {
644647
case OPTION_SET_INT:
645648
case OPTION_BIT:
649+
case OPTION_NEGBIT:
646650
if (!signed_int_fits(opts->defval, opts->precision))
647651
optbug(opts, "has invalid defval");
648652
/* fallthru */
649653
case OPTION_COUNTUP:
650-
case OPTION_NEGBIT:
651654
case OPTION_NUMBER:
652655
case OPTION_BITOP:
653656
if ((opts->flags & PARSE_OPT_OPTARG) ||

parse-options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ struct option {
250250
.short_name = (s), \
251251
.long_name = (l), \
252252
.value = (v), \
253+
.precision = sizeof(*v), \
253254
.help = (h), \
254255
.flags = PARSE_OPT_NOARG, \
255256
.defval = (b), \

0 commit comments

Comments
 (0)