Bug #2492
closedClassCastException in StringValue class when calling xquery function fn:string-length()
100%
Description
We have noticed a ClassCastExceptions in our application when calling xquery function "fn:sting-length()".
It happens every now and then in our system tests running on CI. We have created a simple test application that shows the issue.
Steps:
Run the attached demo application.
Actual:
ClassCastException is thrown
Expected:
No exceptions
Additional info:
This demo application will execute two methods on the same instance of StringValue in different threads.
After few runs an output as showed in the 'example_output.txt' should eventually be observed.
On my machine (windows 7) it can be reproduced almost every time a test is run.
-
The bug is caused by StringValue class not being thread safe:
-
This class has an instance variable 'value' of type CharSequence which is being modified by public methods - its type is being switched between java.util.String and net.sf.saxon.regex.UnicodeString classes.
-
There is no any synchronization used
-
-
This is very important for the case when empty string is used, because there are public values used:
StringValue.EMPTY_STRING StringValue.SINGLE_SPACE StringValue.TRUE StringValue.FALSE
These values can be used by other classes, in different threads.
-
For the case of an empty string StringValue can be created using this static method:
public static StringValue makeStringValue(CharSequence value)
which internally uses the StringValue.EMPTY_STRING for an empty string.
This static method is for example used in the net.sf.saxon.functions.StringLength class (implementation of xquery function 'fn:string-length') in method:
public Int64Value evaluateItem(XPathContext c)
Looking at the implementation of StringLenth class I can see this bug may affect only EMPTY_STRING case, but there are concerns about the rest of the code - are there more possibilities of a similar issues that could affect arbitrary string values. Also it could be checked if similar potential issues could happen in other classes the StringValue.
Files
Please register to edit this issue