Skip to content

Commit 791aedd

Browse files
pks-tgitster
authored andcommitted
parse-options: detect mismatches in integer signedness
It was reported that "t5620-backfill.sh" fails on s390x and sparc64 in a test that exercises the "--min-batch-size" command line option. The symptom was that the option didn't seem to have an effect: we didn't fetch objects with a batch size of 20, but instead fetched all objects at once. As it turns out, the root cause is that `--min-batch-size` uses `OPT_INTEGER()` to parse the command line option. While this macro expects the caller to pass a pointer to an integer, we instead pass a pointer to a `size_t`. This coincidentally works on most platforms, but it breaks apart on the mentioned platforms because they are big endian. This issue isn't specific to git-backfill(1): there are a couple of other places where we have the same type confusion going on. This indicates that the issue really is the interface that the parse-options subsystem provides -- it is simply too easy to get this wrong as there isn't any kind of compiler warning, and things just work on the most common systems. Address the systemic issue by introducing two new build asserts `BARF_UNLESS_SIGNED()` and `BARF_UNLESS_UNSIGNED()`. As the names already hint at, those macros will cause a compiler error when passed a value that is not signed or unsigned, respectively. Adapt `OPT_INTEGER()`, `OPT_UNSIGNED()` as well as `OPT_MAGNITUDE()` to use those asserts. This uncovers a small set of sites where we indeed have the same bug as in git-backfill(1). Adapt all of them to use the correct option. Reported-by: Todd Zullinger <[email protected]> Reported-by: John Paul Adrian Glaubitz <[email protected]> Helped-by: SZEDER Gábor <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bc288c5 commit 791aedd

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
lines changed

apply.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5123,8 +5123,8 @@ int apply_parse_options(int argc, const char **argv,
51235123
/* Think twice before adding "--nul" synonym to this */
51245124
OPT_SET_INT('z', NULL, &state->line_termination,
51255125
N_("paths are separated with NUL character"), '\0'),
5126-
OPT_INTEGER('C', NULL, &state->p_context,
5127-
N_("ensure at least <n> lines of context match")),
5126+
OPT_UNSIGNED('C', NULL, &state->p_context,
5127+
N_("ensure at least <n> lines of context match")),
51285128
OPT_CALLBACK(0, "whitespace", state, N_("action"),
51295129
N_("detect new or modified lines that have whitespace errors"),
51305130
apply_option_parse_whitespace),

builtin/backfill.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ int cmd_backfill(int argc, const char **argv, const char *prefix, struct reposit
123123
.sparse = 0,
124124
};
125125
struct option options[] = {
126-
OPT_INTEGER(0, "min-batch-size", &ctx.min_batch_size,
127-
N_("Minimum number of objects to request at a time")),
126+
OPT_UNSIGNED(0, "min-batch-size", &ctx.min_batch_size,
127+
N_("Minimum number of objects to request at a time")),
128128
OPT_BOOL(0, "sparse", &ctx.sparse,
129129
N_("Restrict the missing objects to the current sparse-checkout")),
130130
OPT_END(),

builtin/column.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int cmd_column(int argc,
3131
struct option options[] = {
3232
OPT_STRING(0, "command", &real_command, N_("name"), N_("lookup config vars")),
3333
OPT_COLUMN(0, "mode", &colopts, N_("layout to use")),
34-
OPT_INTEGER(0, "raw-mode", &colopts, N_("layout to use")),
34+
OPT_UNSIGNED(0, "raw-mode", &colopts, N_("layout to use")),
3535
OPT_INTEGER(0, "width", &copts.width, N_("maximum width")),
3636
OPT_STRING(0, "indent", &copts.indent, N_("string"), N_("padding space on left border")),
3737
OPT_STRING(0, "nl", &copts.nl, N_("string"), N_("padding space on right border")),

builtin/grep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,9 +983,9 @@ int cmd_grep(int argc,
983983
OPT_CALLBACK('C', "context", &opt, N_("n"),
984984
N_("show <n> context lines before and after matches"),
985985
context_callback),
986-
OPT_INTEGER('B', "before-context", &opt.pre_context,
986+
OPT_UNSIGNED('B', "before-context", &opt.pre_context,
987987
N_("show <n> context lines before matches")),
988-
OPT_INTEGER('A', "after-context", &opt.post_context,
988+
OPT_UNSIGNED('A', "after-context", &opt.post_context,
989989
N_("show <n> context lines after matches")),
990990
OPT_INTEGER(0, "threads", &num_threads,
991991
N_("use <n> worker threads")),

git-compat-util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,19 @@ DISABLE_WARNING(-Wsign-compare)
110110
# define BARF_UNLESS_COPYABLE(dst, src) \
111111
BUILD_ASSERT_OR_ZERO(__builtin_types_compatible_p(__typeof__(*(dst)), \
112112
__typeof__(*(src))))
113+
114+
# define BARF_UNLESS_SIGNED(var) BUILD_ASSERT_OR_ZERO(((__typeof__(var)) -1) < 0)
115+
# define BARF_UNLESS_UNSIGNED(var) BUILD_ASSERT_OR_ZERO(((__typeof__(var)) -1) > 0)
113116
#else
114117
# define BARF_UNLESS_AN_ARRAY(arr) 0
115118
# define BARF_UNLESS_COPYABLE(dst, src) \
116119
BUILD_ASSERT_OR_ZERO(0 ? ((*(dst) = *(src)), 0) : \
117120
sizeof(*(dst)) == sizeof(*(src)))
121+
122+
# define BARF_UNLESS_SIGNED(var) 0
123+
# define BARF_UNLESS_UNSIGNED(var) 0
118124
#endif
125+
119126
/*
120127
* ARRAY_SIZE - get the number of elements in a visible array
121128
* @x: the array whose size you want.

parse-options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ struct option {
218218
.type = OPTION_INTEGER, \
219219
.short_name = (s), \
220220
.long_name = (l), \
221-
.value = (v), \
221+
.value = (v) + BARF_UNLESS_SIGNED(*(v)), \
222222
.precision = sizeof(*v), \
223223
.argh = N_("n"), \
224224
.help = (h), \
@@ -280,7 +280,7 @@ struct option {
280280
.type = OPTION_UNSIGNED, \
281281
.short_name = (s), \
282282
.long_name = (l), \
283-
.value = (v), \
283+
.value = (v) + BARF_UNLESS_UNSIGNED(*(v)), \
284284
.precision = sizeof(*v), \
285285
.argh = N_("n"), \
286286
.help = (h), \

0 commit comments

Comments
 (0)