Skip to content

Commit

Permalink
(2020/07/15) POI: [bug-64600] Avoid XWPF NPE when styleid is null. Th…
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Mar 16, 2023
1 parent fe975de commit d3043cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
20 changes: 5 additions & 15 deletions ooxml/XWPF/Usermodel/XWPFStyles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,9 @@ public void SetStyles(CT_Styles styles)
*/
public bool StyleExist(String styleID)
{
foreach (XWPFStyle style in listStyle)
{
if (style.StyleId.Equals(styleID))
return true;
}
return false;
return null != GetStyle(styleID);
}

/**
* add a style to the document
* @param style
Expand All @@ -191,6 +187,7 @@ public void AddStyle(XWPFStyle style)
int pos = (ctStyles.GetStyleList().Count) - 1;
ctStyles.SetStyleArray(pos, style.GetCTStyle());
}

/**
*get style by a styleID
* @param styleID styleID of the searched style
Expand All @@ -200,15 +197,8 @@ public XWPFStyle GetStyle(String styleID)
{
foreach (XWPFStyle style in listStyle)
{
try
{
if (style.StyleId.Equals(styleID))
return style;
}
catch (NullReferenceException e)
{
// Ignore NPE
}
if (style.StyleId?.Equals(styleID) ?? false)
return style;
}
return null;
}
Expand Down
5 changes: 5 additions & 0 deletions testcases/ooxml/XWPF/UserModel/TestXWPFStyles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ public void TestMissingStyleId()
Assert.IsNotNull(styles.GetStyle("NoList"));
Assert.IsNull(styles.GetStyle("EmptyCellLayoutStyle"));
Assert.IsNotNull(styles.GetStyle("BalloonText"));

// Bug 64600: styleExist throws NPE
Assert.IsTrue(styles.StyleExist("NoList"));
Assert.IsFalse(styles.StyleExist("EmptyCellLayoutStyle"));
Assert.IsTrue(styles.StyleExist("BalloonText"));
}
catch (NullReferenceException e)
{
Expand Down

0 comments on commit d3043cc

Please # to comment.