Project

Profile

Help

Bug #2492

closed

ClassCastException in StringValue class when calling xquery function fn:string-length()

Added by Michal Jagannathan over 8 years ago. Updated over 8 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Multithreading
Sprint/Milestone:
Start date:
2015-11-06
Due date:
% Done:

100%

Estimated time:
Legacy ID:
Applies to branch:
9.6
Fix Committed on Branch:
9.6
Fixed in Maintenance Release:
Platforms:

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.

  1. The bug is caused by StringValue class not being thread safe:

    1. 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.

    2. There is no any synchronization used

  2. 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.

  3. 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

saxon_issue.zip (4.24 KB) saxon_issue.zip Michal Jagannathan, 2015-11-06 13:05

Please register to edit this issue

Also available in: Atom PDF