git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/5] range-set: satisfy non-empty ranges invariant

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jul 23, 2013, 14:28 UTC
Message-ID
<1374589688-27751-4-git-send-email-sunshine@sunshineco.com>
In-Reply-To
<1374589688-27751-1-git-send-email-sunshine@sunshineco.com>

range-set invariants are: ranges must be (1) non-empty, (2) disjoint, (3) sorted in ascending order.

During processing, various range-set utility functions break the invariants (for instance, by adding empty ranges), with the expectation that a finalizing sort_and_merge_range_set() will restore sanity.

sort_and_merge_range_set(), however, neglects to fold out empty ranges, thus it fails to satisfy the non-empty constraint. Subsequent range-set functions crash or throw an assertion failure upon encountering such an anomaly. Rectify the situation by having sort_and_merge_range_set() fold out empty ranges.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 line-log.c          | 2 ++
 t/t4211-line-log.sh | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/line-log.c b/line-log.c
index 5234879..6f94d56 100644
--- a/line-log.c
+++ b/line-log.c
@@ -115,6 +115,8 @@ static void sort_and_merge_range_set(struct range_set *rs)
 	qsort(rs->ranges, rs->nr, sizeof(struct range), range_cmp);
 
 	for (i = 0; i < rs->nr; i++) {
+		if (rs->ranges[i].start == rs->ranges[i].end)
+			continue;
 		if (o > 0 && rs->ranges[i].start <= rs->ranges[o-1].end) {
 			if (rs->ranges[o-1].end < rs->ranges[i].end)
 				rs->ranges[o-1].end = rs->ranges[i].end;
diff --git a/t/t4211-line-log.sh b/t/t4211-line-log.sh
index 1db1edd..12814c0 100755
--- a/t/t4211-line-log.sh
+++ b/t/t4211-line-log.sh
@@ -67,7 +67,7 @@ test_bad_opts "-L :foo:b.c" "no match"
 # There is a separate bug when an empty -L range is the first -L encountered,
 # thus to demonstrate this particular bug, the empty -L range must follow a
 # non-empty -L range.
-test_expect_failure '-L {empty-range} (any -L)' '
+test_expect_success '-L {empty-range} (any -L)' '
 	n=$(expr $(cat b.c | wc -l) + 1) &&
 	git log -L1,1:b.c -L$n:b.c
 '
-- 
1.8.3.4.1120.gc240c48
Previous: Eric SunshineNext: Eric Sunshine
Message 10 of 17 in “range-set and line-log bug fixes”
  1. 0/5 range-set and line-log bug fixesEric Sunshine, Jul 23, 2013
  2. 1/5 range-set: fix sort_and_merge_range_set() corner case bugEric Sunshine, Jul 23, 2013
  3. 2/5 t4211: demonstrate empty -L range crashEric Sunshine, Jul 23, 2013
  4. SZEDER GáborJul 23, 2013
  5. Junio C HamanoJul 23, 2013
  6. SZEDER GáborJul 23, 2013
  7. Eric SunshineJul 23, 2013
  8. Junio C HamanoJul 24, 2013
  9. Eric SunshineJul 24, 2013
  10. 3/5 range-set: satisfy non-empty ranges invariantEric Sunshine, Jul 23, 2013
  11. 4/5 t4211: demonstrate crash when first -L encountered is empty rangeEric Sunshine, Jul 23, 2013
  12. 5/5 line-log: fix "log -LN" crash when N is last line of fileEric Sunshine, Jul 23, 2013
  13. Thomas RastJul 23, 2013
  14. Eric SunshineJul 25, 2013
  15. Eric SunshineJul 25, 2013
  16. Johannes SixtJul 25, 2013
  17. Eric SunshineJul 25, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.