Skip to content

Commit 50738b0

Browse files
authored
#246 fix InvalidCastException in ThreadContextStack.InternalStack.set (fixes #245) (#246)
1 parent afda7d2 commit 50738b0

File tree

6 files changed

+119
-6
lines changed

6 files changed

+119
-6
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
////
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
////
17+
18+
////
19+
██ ██ █████ ██████ ███ ██ ██ ███ ██ ██████ ██
20+
██ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██ ██ ██
21+
██ █ ██ ███████ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██
22+
██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
23+
███ ███ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██████ ██
24+
25+
IF THIS FILE DOESN'T HAVE A `.ftl` SUFFIX, IT IS AUTO-GENERATED, DO NOT EDIT IT!
26+
27+
Version-specific release notes (`7.8.0.adoc`, etc.) are generated from `src/changelog/*/.release-notes.adoc.ftl`.
28+
Auto-generation happens during `generate-sources` phase of Maven.
29+
Hence, you must always
30+
31+
1. Find and edit the associated `.release-notes.adoc.ftl`
32+
2. Run `./mvnw generate-sources`
33+
3. Commit both `.release-notes.adoc.ftl` and the generated `7.8.0.adoc`
34+
////
35+
36+
[#release-notes-${release.version?replace("[^a-zA-Z0-9]", "-", "r")}]
37+
== ${release.version}
38+
39+
<#if release.date?has_content>Release date:: ${release.date}</#if>
40+
41+
<#include "../.changelog.adoc.ftl">

src/changelog/3.1.1/.release.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<release xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
date="2025-07-12"
6+
version="3.1.1"/>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="245" link="https://github.com/apache/logging-log4net/issues/245"/>
7+
<issue id="246" link="https://github.com/apache/logging-log4net/pull/246"/>
8+
<description format="asciidoc">
9+
Fix InvalidCastException in NDC.Inherit(System.Collections.Stack)
10+
(reported by @jberg7, implemented by @freeandnil in https://github.com/apache/logging-log4net/pull/246[#246])
11+
</description>
12+
</entry>

src/log4net.Tests/NdcTest.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#region Apache License
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one or more
4+
// contributor license agreements. See the NOTICE file distributed with
5+
// this work for additional information regarding copyright ownership.
6+
// The ASF licenses this file to you under the Apache License, Version 2.0
7+
// (the "License"); you may not use this file except in compliance with
8+
// the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
#endregion
19+
20+
using System;
21+
using System.Diagnostics;
22+
using System.Diagnostics.CodeAnalysis;
23+
using System.Globalization;
24+
using System.IO;
25+
using System.Linq;
26+
using System.Threading;
27+
using log4net.Config;
28+
using log4net.Core;
29+
using log4net.Layout;
30+
using log4net.Layout.Pattern;
31+
using log4net.Repository;
32+
using log4net.Tests.Appender;
33+
using log4net.Util;
34+
using NUnit.Framework;
35+
36+
namespace log4net.Tests;
37+
38+
/// <summary>
39+
/// Used for internal unit testing the <see cref="NDC"/> class.
40+
/// </summary>
41+
[TestFixture]
42+
public class NdcTest
43+
{
44+
/// <summary>
45+
/// Test regression (https://github.com/apache/logging-log4net/issues/245)
46+
/// </summary>
47+
[Test]
48+
public void InheritTest()
49+
{
50+
NDC.Push("first");
51+
NDC.Push("last");
52+
System.Collections.Stack context = NDC.CloneStack();
53+
NDC.Clear();
54+
NDC.Inherit(context);
55+
Assert.That(NDC.Pop(), Is.EqualTo("last"));
56+
Assert.That(NDC.Pop(), Is.EqualTo("first"));
57+
}
58+
}

src/log4net.sln

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ EndProject
4141
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".doc", ".doc", "{33D80AD3-8048-4220-8BB0-38E3BDE5FCF4}"
4242
ProjectSection(SolutionItems) = preProject
4343
..\doc\BUILDING.md = ..\doc\BUILDING.md
44-
site\xdoc\release\config-examples.xml = site\xdoc\release\config-examples.xml
45-
site\xdoc\download_log4net.xml = site\xdoc\download_log4net.xml
4644
..\doc\MailTemplate.Announce.txt = ..\doc\MailTemplate.Announce.txt
4745
..\doc\MailTemplate.Result.txt = ..\doc\MailTemplate.Result.txt
4846
..\doc\MailTemplate.txt = ..\doc\MailTemplate.txt
49-
site\xdoc\release\release-notes.xml = site\xdoc\release\release-notes.xml
50-
site\xdoc\release\release-review-instructions.xml = site\xdoc\release\release-review-instructions.xml
5147
..\doc\RELEASING.md = ..\doc\RELEASING.md
5248
EndProjectSection
5349
EndProject

src/log4net/Util/ThreadContextStack.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ internal Stack InternalStack
188188
set
189189
{
190190
_stack.Clear();
191-
var frames = (StackFrame[])value.ToArray();
191+
object[] frames = value.ToArray();
192192
for (int i = frames.Length - 1; i >= 0; i--)
193193
{
194-
_stack.Push(frames[i]);
194+
_stack.Push(frames[i].EnsureIs<StackFrame>());
195195
}
196196
}
197197
}

0 commit comments

Comments
 (0)