Skip to content

Commit 1d918bf

Browse files
rscharfegitster
authored andcommitted
parse-options: add precision handling for OPTION_BITOP
Similar to 0970569 (parse-options: introduce precision handling for `OPTION_INTEGER`, 2025-04-17) support value variables of different sizes for OPTION_BITOP. Do that by requiring their "precision" to be set, casting their "value" pointer accordingly and checking whether the value fits. Check if "devfal" fits into an integer variable with the given "precision", but don't check "extra", as its value is only used to clear bits, so cannot lead to an overflow. Not checking continues to allow e.g., using -1 to clear all bits even if the value variable has a narrower type than intptr_t. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent feeebbf commit 1d918bf

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

parse-options.c

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

169169
case OPTION_BITOP:
170+
{
171+
intmax_t value = get_int_value(opt, flags);
170172
if (unset)
171173
BUG("BITOP can't have unset form");
172-
*(int *)opt->value &= ~opt->extra;
173-
*(int *)opt->value |= opt->defval;
174-
return 0;
174+
value &= ~opt->extra;
175+
value |= opt->defval;
176+
return set_int_value(opt, flags, value);
177+
}
175178

176179
case OPTION_COUNTUP:
177180
if (*(int *)opt->value < 0)
@@ -647,12 +650,12 @@ static void parse_options_check(const struct option *opts)
647650
case OPTION_SET_INT:
648651
case OPTION_BIT:
649652
case OPTION_NEGBIT:
653+
case OPTION_BITOP:
650654
if (!signed_int_fits(opts->defval, opts->precision))
651655
optbug(opts, "has invalid defval");
652656
/* fallthru */
653657
case OPTION_COUNTUP:
654658
case OPTION_NUMBER:
655-
case OPTION_BITOP:
656659
if ((opts->flags & PARSE_OPT_OPTARG) ||
657660
!(opts->flags & PARSE_OPT_NOARG))
658661
optbug(opts, "should not accept an argument");

parse-options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ struct option {
240240
.short_name = (s), \
241241
.long_name = (l), \
242242
.value = (v), \
243+
.precision = sizeof(*v), \
243244
.help = (h), \
244245
.flags = PARSE_OPT_NOARG|PARSE_OPT_NONEG, \
245246
.defval = (set), \

0 commit comments

Comments
 (0)