From 558172f206cc87cd709df41f4afd472a3a517057 Mon Sep 17 00:00:00 2001 From: Danny Rorabaugh Date: Mon, 8 Jun 2026 13:25:29 -0400 Subject: [PATCH 1/3] Fix NRE in XLiffBody.AddTransUnit when tu.Source is null (#149) --- src/L10NSharp/XLiffUtils/XLiffBody.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/L10NSharp/XLiffUtils/XLiffBody.cs b/src/L10NSharp/XLiffUtils/XLiffBody.cs index 695b1f3..515dc2c 100644 --- a/src/L10NSharp/XLiffUtils/XLiffBody.cs +++ b/src/L10NSharp/XLiffUtils/XLiffBody.cs @@ -176,7 +176,7 @@ public bool AddTransUnit(XLiffTransUnit tu) // the source value there. if (tu.Target != null && tu.Target.Value != null) TranslationsById[tu.Id] = tu.Target.Value; - else + else if (tu.Source != null) TranslationsById[tu.Id] = tu.Source.Value; return true; } @@ -246,7 +246,7 @@ internal int NumberTranslated { ++_translatedCount; } - else if (tu.Target.Value != tu.Source.Value && + else if (tu.Source != null && tu.Target.Value != tu.Source.Value && tu.Target.TargetState == XLiffTransUnitVariant.TranslationState.Undefined) { ++_translatedCount; From 227cfd0d34c2566d90a129256aa5aaf602af079b Mon Sep 17 00:00:00 2001 From: Danny Rorabaugh Date: Mon, 8 Jun 2026 13:31:17 -0400 Subject: [PATCH 2/3] Add test and changelog for NRE fix in XLiffBody.AddTransUnit (#149) --- CHANGELOG.md | 1 + src/L10NSharp.Tests/XLiffLocalizationManagerTests.cs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 895773a..3c5cc92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - [L10NSharp.Windows.Forms] Restored project-local Resources support for `FallbackLanguagesDlgBase` button images (`Move`, `Move_up`, and `Move_down`). - [L10NSharp.Windows.Forms] Corrected resource manager base name to `L10NSharp.Windows.Forms.Properties.Resources`. - [L10NSharp.Windows.Forms.Tests] Corrected resource manager base name to `L10NSharp.Windows.Forms.Tests.Properties.Resources`. +- [L10NSharp] Fixed `NullReferenceException` in `XLiffBody.AddTransUnit` when a trans-unit has no source variant (e.g. from a malformed XLIFF file). (#149) ### Removed diff --git a/src/L10NSharp.Tests/XLiffLocalizationManagerTests.cs b/src/L10NSharp.Tests/XLiffLocalizationManagerTests.cs index 72a02f0..c8264a0 100644 --- a/src/L10NSharp.Tests/XLiffLocalizationManagerTests.cs +++ b/src/L10NSharp.Tests/XLiffLocalizationManagerTests.cs @@ -239,6 +239,16 @@ public void MergeXliffDocuments_WorksAsExpected() }, true); } + [Test] + public void AddTransUnit_NullSourceNullTarget_DoesNotThrow() + { + var body = new XLiffBody(); + // Target is null (default), so the else branch in AddTransUnit fires. + // Before the fix: tu.Source.Value throws NRE when Source is also null. + var tu = new XLiffTransUnit { Id = "some-id", Source = null }; + Assert.DoesNotThrow(() => body.AddTransUnit(tu)); + } + private void CheckMergedTransUnit(XLiffTransUnit tu, string sourceText, string[] notes, bool isDynamic) { Assert.IsNotNull(tu); From 1f9ff828093a3912fbbdca7c1aee5d02f523e4f2 Mon Sep 17 00:00:00 2001 From: Danny Rorabaugh Date: Mon, 8 Jun 2026 13:42:27 -0400 Subject: [PATCH 3/3] Simplify AddTransUnit_NullSourceNullTarget_DoesNotThrow test --- src/L10NSharp.Tests/XLiffLocalizationManagerTests.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/L10NSharp.Tests/XLiffLocalizationManagerTests.cs b/src/L10NSharp.Tests/XLiffLocalizationManagerTests.cs index c8264a0..8b73894 100644 --- a/src/L10NSharp.Tests/XLiffLocalizationManagerTests.cs +++ b/src/L10NSharp.Tests/XLiffLocalizationManagerTests.cs @@ -243,9 +243,7 @@ public void MergeXliffDocuments_WorksAsExpected() public void AddTransUnit_NullSourceNullTarget_DoesNotThrow() { var body = new XLiffBody(); - // Target is null (default), so the else branch in AddTransUnit fires. - // Before the fix: tu.Source.Value throws NRE when Source is also null. - var tu = new XLiffTransUnit { Id = "some-id", Source = null }; + var tu = new XLiffTransUnit { Id = "some-id", Source = null, Target = null }; Assert.DoesNotThrow(() => body.AddTransUnit(tu)); }