Skip to content

Commit a50e295

Browse files
committed
merge-ort: avoid accidental API mis-use
Previously, callers of the merge-ort API could have passed an uninitialized value for struct merge_result *result. However, we want to check result to see if it has cached renames from a previous merge that we can reuse; such values would be found behind result->priv. However, if result->priv is uninitialized, attempting to access behind it will give a segfault. So, we need result->priv to be NULL (which will be the case if the caller does a memset(&result, 0)), or be written by a previous call to the merge-ort machinery. Documenting this requirement may help, but despite being the person who introduced this requirement, I still missed it once and it did not fail in a very clear way and led to a long debugging session. Add a _properly_initialized field to merge_result; that value will be 0 if the caller zero'ed the merge_result, it will be set to a very specific value by a previous run by the merge-ort machinery, and if it's uninitialized it will most likely either be 0 or some value that does not match the specific one we'd expect allowing us to throw a much more meaningful error. Signed-off-by: Elijah Newren <[email protected]>
1 parent e123d13 commit a50e295

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

merge-ort.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ enum merge_side {
5151
MERGE_SIDE2 = 2
5252
};
5353

54+
static unsigned RESULT_INITIALIZED = 0x1abe11ed; /* unlikely accidental value */
55+
5456
struct traversal_callback_data {
5557
unsigned long mask;
5658
unsigned long dirmask;
@@ -3563,6 +3565,10 @@ static void merge_start(struct merge_options *opt, struct merge_result *result)
35633565
assert(opt->obuf.len == 0);
35643566

35653567
assert(opt->priv == NULL);
3568+
if (result->_properly_initialized != 0 &&
3569+
result->_properly_initialized != RESULT_INITIALIZED)
3570+
BUG("struct merge_result passed to merge_incore_*recursive() must be zeroed or filled with values from a previous run");
3571+
assert(!!result->priv == !!result->_properly_initialized);
35663572
if (result->priv) {
35673573
opt->priv = result->priv;
35683574
result->priv = NULL;
@@ -3711,6 +3717,7 @@ static void merge_ort_nonrecursive_internal(struct merge_options *opt,
37113717
result->clean &= strmap_empty(&opt->priv->conflicted);
37123718
if (!opt->priv->call_depth) {
37133719
result->priv = opt->priv;
3720+
result->_properly_initialized = RESULT_INITIALIZED;
37143721
opt->priv = NULL;
37153722
}
37163723
}

merge-ort.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct merge_result {
2929
* !clean) and to print "CONFLICT" messages. Not for external use.
3030
*/
3131
void *priv;
32+
/* Also private */
33+
unsigned _properly_initialized;
3234
};
3335

3436
/*

0 commit comments

Comments
 (0)