{"thread":{"id":"34372","subject":"[PATCH] range_set: fix coalescing bug when range is a subset of another","startedAt":"2013-07-07T13:02:32Z","lastAt":"2013-07-07T13:02:32Z","messageCount":1,"participants":["Eric Sunshine"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"222745","messageId":"1373202152-3231-1-git-send-email-sunshine@sunshineco.com","threadId":"34372","inReplyTo":null,"subject":"[PATCH] range_set: fix coalescing bug when range is a subset of another","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2013-07-07T13:02:32Z","receivedAt":"2013-07-07T13:02:32Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"When coalescing ranges, sort_and_merge_range_set() unconditionally\nassumes that the end of a range being folded into a preceding range\nshould become the end of the coalesced range. This assumption, however,\nis invalid when one range is a subset of another.  For example, given\nranges 1-5 and 2-3 added via range_set_append_unsafe(),\nsort_and_merge_range_set() incorrectly coalesces them to range 1-3\nrather than the correct union range 1-5. Fix this bug.\n\nSigned-off-by: Eric Sunshine <sunshine@sunshineco.com>\n---\n\nPresumably, this problem does not happen in practice, so it's not clear\nif the patch should be applied.  I discovered it when teaching git-blame\nto accept multiple -L options, one iteration of which (after making the\nrange_set API public) employed sort_and_merge_range_set() to sort and\ncoalesce input -L ranges added via range_set_append_unsafe().\n\nIt might make sense to apply this patch in order to future-proof\nsort_and_merge_range_set() in case the range_set API ever becomes\npublic.\n\n\n line-log.c | 3 ++-\n 1 file changed, 2 insertions(+), 1 deletion(-)\n\ndiff --git a/line-log.c b/line-log.c\nindex 4bbb09b..8cc29a0 100644\n--- a/line-log.c\n+++ b/line-log.c\n@@ -116,7 +116,8 @@ static void sort_and_merge_range_set(struct range_set *rs)\n \n \tfor (i = 1; i < rs->nr; i++) {\n \t\tif (rs->ranges[i].start <= rs->ranges[o-1].end) {\n-\t\t\trs->ranges[o-1].end = rs->ranges[i].end;\n+\t\t\tif (rs->ranges[o-1].end < rs->ranges[i].end)\n+\t\t\t\trs->ranges[o-1].end = rs->ranges[i].end;\n \t\t} else {\n \t\t\trs->ranges[o].start = rs->ranges[i].start;\n \t\t\trs->ranges[o].end = rs->ranges[i].end;\n-- \n1.8.3.2\n"}]}