Title: DiffLine.rawContent() returns string instead of Buffer, causing non-UTF-8 encoding corruption · Issue #2038 · nodegit/nodegit · GitHub
Open Graph Title: DiffLine.rawContent() returns string instead of Buffer, causing non-UTF-8 encoding corruption · Issue #2038 · nodegit/nodegit
X Title: DiffLine.rawContent() returns string instead of Buffer, causing non-UTF-8 encoding corruption · Issue #2038 · nodegit/nodegit
Description: Description Thanks to the nodegit maintainers for this excellent library! This issue was debugged with the assistance of Cursor and Opus 4.5. Current Behavior DiffLine.rawContent() returns a JavaScript string type, but the underlying lib...
Open Graph Description: Description Thanks to the nodegit maintainers for this excellent library! This issue was debugged with the assistance of Cursor and Opus 4.5. Current Behavior DiffLine.rawContent() returns a JavaSc...
X Description: Description Thanks to the nodegit maintainers for this excellent library! This issue was debugged with the assistance of Cursor and Opus 4.5. Current Behavior DiffLine.rawContent() returns a JavaSc...
Opengraph URL: https://github.com/nodegit/nodegit/issues/2038
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"DiffLine.rawContent() returns string instead of Buffer, causing non-UTF-8 encoding corruption","articleBody":"## Description\n\nThanks to the nodegit maintainers for this excellent library! \n\nThis issue was debugged with the assistance of Cursor and Opus 4.5.\n\n### Current Behavior\n\n`DiffLine.rawContent()` returns a JavaScript `string` type, but the underlying libgit2 `git_diff_line.content` is a raw byte pointer (`const char *`) that is **not NUL-terminated** and may contain non-UTF-8 encoded content (e.g., GBK, GB18030).\n\nThe current implementation in `lib/diff_line.js`:\n\n```javascript\nvar _rawContent = DiffLine.prototype.content; // Save original native method\n\nDiffLine.prototype.content = function() {\n // ...\n this._cache.content = Buffer.from(this.rawContent())\n .slice(0, this.contentLen())\n .toString(\"utf8\");\n return this._cache.content;\n};\n\nDiffLine.prototype.rawContent = function() {\n return _rawContent.call(this); // Calls native binding\n};\n```\n\nThe problem is that `_rawContent` (the native binding) already converts `const char *` to a JavaScript string, presumably using `v8::String::NewFromUtf8()` or similar, which **assumes UTF-8 encoding**.\n\n### Expected Behavior\n\n`rawContent()` should return a `Buffer` containing the original bytes, allowing users to detect and decode the encoding themselves:\n\n```javascript\nDiffLine.prototype.rawContent = function() {\n // Return Buffer instead of string\n return _rawContent.call(this); // Should return Buffer\n};\n\nDiffLine.prototype.content = function() {\n // ... existing implementation\n return this.rawContent()\n .slice(0, this.contentLen())\n .toString(\"utf8\");\n};\n```","author":{"url":"https://github.com/liangjingyang","@type":"Person","name":"liangjingyang"},"datePublished":"2026-01-13T01:51:21.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/2038/nodegit/issues/2038"}
| 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:4aa5495e-7d58-7056-3c0a-c5d711e9472a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E9B4:2713AA:109351:1687BB:696FFACB |
| html-safe-nonce | 63b0aa7371efeb4c8ed02b513323ce7300a9f941074083c6493a9c183cfc99a1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOUI0OjI3MTNBQToxMDkzNTE6MTY4N0JCOjY5NkZGQUNCIiwidmlzaXRvcl9pZCI6IjQ5NzY5NTkzNjkzNzk4Mzg2NjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e3a28dfeee176dc9ae6855c2004e5ca9b77a046b645d4e854364cf0f4d191648 |
| hovercard-subject-tag | issue:3806716122 |
| 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/nodegit/nodegit/2038/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7e45ae34e9f53149789c536aa83a9c0e2c46bdc7e82a853a799e673506c8855b/nodegit/nodegit/issues/2038 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7e45ae34e9f53149789c536aa83a9c0e2c46bdc7e82a853a799e673506c8855b/nodegit/nodegit/issues/2038 |
| og:image:alt | Description Thanks to the nodegit maintainers for this excellent library! This issue was debugged with the assistance of Cursor and Opus 4.5. Current Behavior DiffLine.rawContent() returns a JavaSc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | liangjingyang |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0366807b865cee6776368231232f84d6c8096e6bce43f701a4fb28ea795ec427 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodegit/nodegit git https://github.com/nodegit/nodegit.git |
| octolytics-dimension-user_id | 657068 |
| octolytics-dimension-user_login | nodegit |
| octolytics-dimension-repository_id | 1383170 |
| octolytics-dimension-repository_nwo | nodegit/nodegit |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1383170 |
| octolytics-dimension-repository_network_root_nwo | nodegit/nodegit |
| 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 | 33f356bb2fb58726ccb2f26395bf8ddc9a2d9eaa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width