Skip to content

Commit 5228211

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

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

builtin/write-tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ int cmd_write_tree(int argc,
3535
.type = OPTION_BIT,
3636
.long_name = "ignore-cache-tree",
3737
.value = &flags,
38+
.precision = sizeof(flags),
3839
.help = N_("only useful for debugging"),
3940
.flags = PARSE_OPT_HIDDEN | PARSE_OPT_NOARG,
4041
.defval = WRITE_TREE_IGNORE_CACHE_TREE,

parse-options.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ static int do_get_int_value(const void *value, size_t precision, intmax_t *ret)
8888
}
8989
}
9090

91+
static intmax_t get_int_value(const struct option *opt, enum opt_parsed flags)
92+
{
93+
intmax_t ret;
94+
if (do_get_int_value(opt->value, opt->precision, &ret))
95+
BUG("invalid precision for option %s", optname(opt, flags));
96+
return ret;
97+
}
98+
9199
static enum parse_opt_result set_int_value(const struct option *opt,
92100
enum opt_parsed flags,
93101
intmax_t value)
@@ -139,11 +147,14 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
139147
return opt->ll_callback(p, opt, NULL, unset);
140148

141149
case OPTION_BIT:
150+
{
151+
intmax_t value = get_int_value(opt, flags);
142152
if (unset)
143-
*(int *)opt->value &= ~opt->defval;
153+
value &= ~opt->defval;
144154
else
145-
*(int *)opt->value |= opt->defval;
146-
return 0;
155+
value |= opt->defval;
156+
return set_int_value(opt, flags, value);
157+
}
147158

148159
case OPTION_NEGBIT:
149160
if (unset)
@@ -631,11 +642,11 @@ static void parse_options_check(const struct option *opts)
631642
optbug(opts, "OPTION_SET_INT 0 should not be negatable");
632643
switch (opts->type) {
633644
case OPTION_SET_INT:
645+
case OPTION_BIT:
634646
if (!signed_int_fits(opts->defval, opts->precision))
635647
optbug(opts, "has invalid defval");
636648
/* fallthru */
637649
case OPTION_COUNTUP:
638-
case OPTION_BIT:
639650
case OPTION_NEGBIT:
640651
case OPTION_NUMBER:
641652
case OPTION_BITOP:

parse-options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ struct option {
172172
.short_name = (s), \
173173
.long_name = (l), \
174174
.value = (v), \
175+
.precision = sizeof(*v), \
175176
.help = (h), \
176177
.flags = PARSE_OPT_NOARG|(f), \
177178
.callback = NULL, \

0 commit comments

Comments
 (0)