Skip to content

Commit b111547

Browse files
Include defensive programming to handle unclosed tags (#900)
* Include defensive programming to handle unclosed tags when the 'WriteElement' method receives a null value * Add the same defensive programming to handle unclosed tags on .NET.
1 parent 745cac6 commit b111547

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ internal void AppendNode(GXSOAPContext ctx, Node node, int extent)
11321132

11331133
char[] trimChars = { '\t', ' ' };
11341134

1135-
if (node.NodeType != ElementType) return;
1135+
if (node==null || node.NodeType != ElementType) return;
11361136
switch (node.NodeType)
11371137
{
11381138
case ElementType:
@@ -2131,6 +2131,10 @@ string removeUnallowedChars( string s)
21312131
public short WriteElement (string Name, string Value)
21322132
{
21332133
WriteStartElement(Name);
2134+
if (Value==null)
2135+
{
2136+
Value = string.Empty;
2137+
}
21342138
valueBuffer = Value;
21352139
return 0;
21362140
}

dotnet/src/dotnetframework/GxClasses/Domain/GXXmlReadWrite.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2137,6 +2137,10 @@ string removeUnallowedChars( string s)
21372137
public short WriteElement (string Name, string Value)
21382138
{
21392139
WriteStartElement(Name);
2140+
if (Value==null)
2141+
{
2142+
Value = string.Empty;
2143+
}
21402144
valueBuffer = Value;
21412145
return 0;
21422146
}
@@ -2150,7 +2154,7 @@ public short WriteElement (string Name, object Value)
21502154

21512155
public short WriteElement (string Name)
21522156
{
2153-
WriteElement (Name, "");
2157+
WriteElement (Name, string.Empty);
21542158
return 0;
21552159
}
21562160

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using GeneXus.Application;
8+
using GeneXus.XML;
9+
using Xunit;
10+
11+
namespace UnitTesting
12+
{
13+
public class XMLWriterTest : FileSystemTest
14+
{
15+
[Fact]
16+
public void dfwpnumNullElementTest()
17+
{
18+
string content = dfwpnumTest(null, false);
19+
Assert.Contains("<varchar xmlns=\"StorageExpiration\" />", content, StringComparison.OrdinalIgnoreCase);
20+
}
21+
[Fact]
22+
public void dfwpnumEmptyElementWithoutEndTest()
23+
{
24+
string content = dfwpnumTest("validValue", false);
25+
Assert.Contains("<varchar xmlns=\"StorageExpiration\">validValue</varchar>", content, StringComparison.OrdinalIgnoreCase);
26+
}
27+
internal string dfwpnumTest(string varcharValue, bool closeElements)
28+
{
29+
GxContext context = new GxContext();
30+
string fileName = Path.Combine(BaseDir, "dfwpnumTest.txt");
31+
GXXMLWriter GXSoapXMLWriter = new GXXMLWriter(context.GetPhysicalPath());
32+
GXSoapXMLWriter.Open(fileName);
33+
GXSoapXMLWriter.WriteStartDocument("utf-8", 0);
34+
GXSoapXMLWriter.WriteStartElement("SOAP-ENV:Envelope");
35+
GXSoapXMLWriter.WriteElement("varchar", varcharValue);
36+
GXSoapXMLWriter.WriteAttribute("xmlns", "StorageExpiration");
37+
string sDateCnv = "0000-00-00";
38+
GXSoapXMLWriter.WriteElement("date", sDateCnv);
39+
GXSoapXMLWriter.WriteAttribute("xmlns", "StorageExpiration");
40+
if (closeElements)
41+
{
42+
GXSoapXMLWriter.WriteEndElement();
43+
}
44+
GXSoapXMLWriter.Close();
45+
return File.ReadAllText(fileName);
46+
}
47+
48+
}
49+
}

0 commit comments

Comments
 (0)