DiffLine.rawContent() returns string instead of Buffer, causing non-UTF-8 encoding corruption

Ouverte
#2,038 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
45/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
À l'abandon
Stack technique
git, javascript, node.js
Domaine
backend, devtools

Piste de recherche

Inspect lib/diff_line.js and the native binding behind DiffLine.rawContent(); first trace how content bytes cross the binding boundary and how contentLen() is used. Reproduce the behavior with non-UTF-8 input such as GBK or GB18030, then verify that rawContent() preserves the original bytes and content() still returns UTF-8 text.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

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 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).

The current implementation in lib/diff_line.js:

var _rawContent = DiffLine.prototype.content;  // Save original native method

DiffLine.prototype.content = function() {
  // ...
  this._cache.content = Buffer.from(this.rawContent())
    .slice(0, this.contentLen())
    .toString("utf8");
  return this._cache.content;
};

DiffLine.prototype.rawContent = function() {
  return _rawContent.call(this);  // Calls native binding
};

The 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.

Expected Behavior

rawContent() should return a Buffer containing the original bytes, allowing users to detect and decode the encoding themselves:

DiffLine.prototype.rawContent = function() {
  // Return Buffer instead of string
  return _rawContent.call(this);  // Should return Buffer
};

DiffLine.prototype.content = function() {
  // ... existing implementation
  return this.rawContent()
    .slice(0, this.contentLen())
    .toString("utf8");
};
Langage dominant
JavaScript
Étoiles
5.8k
Forks
704
Métriques de merge des PR
Aucune PR mergée en 30 j

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de nodegit/nodegit

Toutes les issues de nodegit/nodegit

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.