Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

java Strings odd behavior with collator

While using compareIgnoreCase I get NegativeArraySizeException / 'NullPointerException' ( Running in a Java 7 JRE).

Caused by: java.lang.NegativeArraySizeException
    at java.lang.String.toCharArray(String.java:2748)
    at sun.text.normalizer.NormalizerImpl.canonicalDecomposeWithSingleQuotation(NormalizerImpl.java:2506)
    at java.text.RBTableBuilder.build(RBTableBuilder.java:107)
    at java.text.RBCollationTables.<init>(RBCollationTables.java:83)
    at java.text.RuleBasedCollator.<init>(RuleBasedCollator.java:300)
    at java.text.RuleBasedCollator.<init>(RuleBasedCollator.java:281)
    at java.text.Collator.getInstance(Collator.java:276)

and

Caused by: java.lang.NullPointerException
    at java.lang.String.length(String.java:623)
    at java.text.RBTableBuilder.build(RBTableBuilder.java:84)
    at java.text.RBCollationTables.<init>(RBCollationTables.java:83)
    at java.text.RuleBasedCollator.<init>(RuleBasedCollator.java:300)
    at java.text.RuleBasedCollator.<init>(RuleBasedCollator.java:281)
    at java.text.Collator.getInstance(Collator.java:276)

These seem to happen in String.compareIgnoreCase and String.equalsIgnoreCase

Sample code:

  public static int compareIgnoreCase(Locale locale, String a, String b) {
    if (a != null && a.length() == 0) {
      a = null;
    }
    if (b != null && b.length() == 0) {
      b = null;
    }
    //
    if (a == b) {
      return 0;
    }
    if (a == null) {
      return -1;
    }
    if (b == null) {
      return 1;
    }
    Collator collator = Collator.getInstance(locale);
    collator.setStrength(Collator.SECONDARY);
    return collator.compare(a, b);
  }

Which got called by:

  public static int compareIgnoreCase(String a, String b) {
    return compareIgnoreCase(NlsUtility.getDefaultLocale(), a, b);
  }

NlsUtility.getDefaultLocale:

  public static Locale getDefaultLocale() {
    Locale locale = LocaleThreadLocal.get();
    if (locale == null) {
      locale = Locale.getDefault();
    }
    return locale;
  }

It appears the problem has returned, followed by a JVM crash. I'm not sure how to solve this:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f1dabb9d4f0, pid=4290, tid=139765228910336
#
# JRE version: Java(TM) SE Runtime Environment (7.0_80-b15) (build 1.7.0_80-b15)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.80-b11 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.so+0x7ee4f0]  nmethod::can_unload(BoolObjectClosure*, OopClosure*, oopDesc**, bool)+0x70
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /tmp/hs_err_pid4290.log
Compiled method (c2) 191914528 4305  s!         java.text.Collator::getInstance (157 bytes)
 total in heap  [0x00007f1da19f7f90,0x00007f1da19fa8f8] = 10600
 relocation     [0x00007f1da19f80b0,0x00007f1da19f8290] = 480
 constants      [0x00007f1da19f82a0,0x00007f1da19f82c0] = 32
 main code      [0x00007f1da19f82c0,0x00007f1da19f9980] = 5824
 stub code      [0x00007f1da19f9980,0x00007f1da19f9a40] = 192
 oops           [0x00007f1da19f9a40,0x00007f1da19f9b30] = 240
 scopes data    [0x00007f1da19f9b30,0x00007f1da19fa3d0] = 2208
 scopes pcs     [0x00007f1da19fa3d0,0x00007f1da19fa690] = 704
 dependencies   [0x00007f1da19fa690,0x00007f1da19fa6a8] = 24
 handler table  [0x00007f1da19fa6a8,0x00007f1da19fa8d0] = 552
 nul chk table  [0x00007f1da19fa8d0,0x00007f1da19fa8f8] = 40
Compiled method (c2) 191914528 4305  s!         java.text.Collator::getInstance (157 bytes)
 total in heap  [0x00007f1da19f7f90,0x00007f1da19fa8f8] = 10600
 relocation     [0x00007f1da19f80b0,0x00007f1da19f8290] = 480
 constants      [0x00007f1da19f82a0,0x00007f1da19f82c0] = 32
 main code      [0x00007f1da19f82c0,0x00007f1da19f9980] = 5824
 stub code      [0x00007f1da19f9980,0x00007f1da19f9a40] = 192
 oops           [0x00007f1da19f9a40,0x00007f1da19f9b30] = 240
 scopes data    [0x00007f1da19f9b30,0x00007f1da19fa3d0] = 2208
 scopes pcs     [0x00007f1da19fa3d0,0x00007f1da19fa690] = 704
 dependencies   [0x00007f1da19fa690,0x00007f1da19fa6a8] = 24
 handler table  [0x00007f1da19fa6a8,0x00007f1da19fa8d0] = 552
 nul chk table  [0x00007f1da19fa8d0,0x00007f1da19fa8f8] = 40
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

it appears this bug is making the JVM segfault. You could look at How do I debug Segfaults occurring in the JVM when it runs my code? if you have a similar problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...