Title: generateUnifiedDiff starts an empty hunk range one line too high · Issue #239 · java-diff-utils/java-diff-utils · GitHub
Open Graph Title: generateUnifiedDiff starts an empty hunk range one line too high · Issue #239 · java-diff-utils/java-diff-utils
X Title: generateUnifiedDiff starts an empty hunk range one line too high · Issue #239 · java-diff-utils/java-diff-utils
Description: This bug was found by an automated program we have running that writes tests with hegel (but I have personally reviewed it). More details at the bottom, but the short version is that I am not personally affected by this bug, and if that'...
Open Graph Description: This bug was found by an automated program we have running that writes tests with hegel (but I have personally reviewed it). More details at the bottom, but the short version is that I am not perso...
X Description: This bug was found by an automated program we have running that writes tests with hegel (but I have personally reviewed it). More details at the bottom, but the short version is that I am not perso...
Opengraph URL: https://github.com/java-diff-utils/java-diff-utils/issues/239
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"generateUnifiedDiff starts an empty hunk range one line too high","articleBody":"> This bug was found by an automated program we have running that writes tests with [hegel](https://github.com/hegeldev/hegel-java) (but I have personally reviewed it). More details at the bottom, but the short version is that I am not personally affected by this bug, and if that's not welcome then please feel free to disregard it.\n\n**Describe the bug**\n\nWhen one side of a hunk is empty (an insertion written with no context lines, or the deletion of a whole file), `generateUnifiedDiff` starts that range one line later than `diff -U0` and `git diff -U0` do: appending `x` to a three-line file gives `@@ -4,0 +4,1 @@` where both tools write `@@ -3,0 +4,1 @@`, and deleting all three lines gives `@@ -1,3 +1,0 @@` where both write `@@ -1,3 +0,0 @@`. `patch` refuses the first diff, since the original has no line 4. The same append with one line of context is numbered correctly.\n\n**To Reproduce**\n\n```java\nimport com.github.difflib.DiffUtils;\nimport com.github.difflib.UnifiedDiffUtils;\nimport java.util.Arrays;\nimport java.util.List;\n\npublic class Main {\n static void show(List\u003cString> orig, List\u003cString> rev, int context) {\n UnifiedDiffUtils.generateUnifiedDiff(\"f\", \"f\", orig, DiffUtils.diff(orig, rev), context).forEach(System.out::println);\n System.out.println();\n }\n\n public static void main(String[] args) {\n List\u003cString> abc = Arrays.asList(\"a\", \"b\", \"c\");\n show(abc, Arrays.asList(\"a\", \"b\", \"c\", \"x\"), 0);\n show(abc, Arrays.asList(\"a\", \"b\", \"c\", \"x\"), 1);\n show(abc, Arrays.asList(), 3);\n }\n}\n```\n\nOutput:\n\n```\n--- f\n+++ f\n@@ -4,0 +4,1 @@\n+x\n\n--- f\n+++ f\n@@ -3,1 +3,2 @@\n c\n+x\n\n--- f\n+++ f\n@@ -1,3 +1,0 @@\n-a\n-b\n-c\n```\n\nSaving the first diff as `append.diff` and applying it to a file `f` containing the lines a, b, c:\n\n```\n$ patch f append.diff\npatching file f\nNo such line 4 in input file, ignoring\npatch: **** misordered hunks! output would be garbled\n```\n\nWith the header changed to `@@ -3,0 +4,1 @@`, `patch` applies it and `f` ends in `x`. `patch` does accept the `@@ -1,3 +1,0 @@` deletion, but `diff -u` and `git diff` both write `@@ -1,3 +0,0 @@` for that change.\n\n**Expected behavior**\n\nAn empty range to start at the line before the change, as `diff -U0` and `git diff -U0` write it: `@@ -3,0 +4,1 @@` for the append and `@@ -1,3 +0,0 @@` for the deletion, so that `patch` applies the generated diff.\n\n**System**\n\n- Java: OpenJDK 27\n- Version: java-diff-utils 4.17 from Maven Central, and 4.18-SNAPSHOT built from current `master` (0643852); the output is identical on both. `patch` is macOS's (2.0-12u11-Apple, FreeBSD-derived), `diff` is Apple's, `git` is 2.50.1.\n\nAs mentioned at the top, this was found by an automated program that writes property-based tests for various open source projects using [hegel](https://github.com/hegeldev/hegel-java), and the report was reviewed by hand before filing. We've also potentially found (but not yet hand validated) 8 other bugs in java-diff-utils. You can see the tests at https://github.com/hegeldev/hegel-zoo/tree/main/targets/java/java-diff-utils. Let us know if you would like us to file the other bugs found and/or contribute the tests. NB the tests are currently LLM generated and probably not yet suitable for inclusion as is, but we're happy to help get them into a better state if you want them.\n","author":{"url":"https://github.com/DRMacIver","@type":"Person","name":"DRMacIver"},"datePublished":"2026-10-08T15:00:06Z","url":"https://github.com/java-diff-utils/java-diff-utils/issues/239"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:11d18ee0-ae5f-2f79-9c47-f23dd8b24ab1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8EF8:1D20B7:2AAA0E4:361888A:6B01DDC8 |
| html-safe-nonce | bb12f0e39fff09d742c67dd9005741d4637cd101c34263ab0ffeda9d6773726d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RUY4OjFEMjBCNzoyQUFBMEU0OjM2MTg4OEE6NkIwMUREQzgiLCJ2aXNpdG9yX2lkIjoiOTA5NDc2NzMwMDMyOTk4NzUyOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b17f371b4cf93fa49cc5d1053d13e920d4bde199e09c0669684c2bd2a84e69e3 |
| hovercard-subject-tag | issue:5764063455 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/java-diff-utils/java-diff-utils/239/issue_layout |
| twitter:image | https://opengraph.githubassets.com/914a8864c35a9ee990c3c3165fbbd9e803979c7f51ff36e3d1a58877b7480f9f/java-diff-utils/java-diff-utils/issues/239 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/914a8864c35a9ee990c3c3165fbbd9e803979c7f51ff36e3d1a58877b7480f9f/java-diff-utils/java-diff-utils/issues/239 |
| og:image:alt | This bug was found by an automated program we have running that writes tests with hegel (but I have personally reviewed it). More details at the bottom, but the short version is that I am not perso... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DRMacIver |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/java-diff-utils/java-diff-utils git https://github.com/java-diff-utils/java-diff-utils.git |
| octolytics-dimension-user_id | 40540835 |
| octolytics-dimension-user_login | java-diff-utils |
| octolytics-dimension-repository_id | 86663812 |
| octolytics-dimension-repository_nwo | java-diff-utils/java-diff-utils |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 86663812 |
| octolytics-dimension-repository_network_root_nwo | java-diff-utils/java-diff-utils |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width