Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: s/title/script in 1st NCCE/CS

...

In HTML, tags are case-insensitive, and can therefore be specified using uppercase, lowercase, or any mixture of cases. This noncompliant code example uses the locale-dependent String.toUpperCase() method to convert an HTML tag to upper case, to check it for further processing. The code must ignore <SCRIPT> tags, as they indicate code that is to be discarded. While the English locale would convert "titlescript" to "TITLESCRIPT", the Turkish locale will convert "titlescript" to  to "TİTLESCRİPT", and the check will fail to prune scripts from further processing.

Code Block
bgColor#ffcccc
langjava
public static void processTitleprocessTag(String tag) {
  if (!tag.toUpperCase().equals("TITLESCRIPT")) {
    return;
  } 
  // process titletag
}

Compliant Solution (Explicit Locale)

...

Code Block
bgColor#ccccff
langjava
public static void processTitleprocessTag(String tag) {
  if (!tag.toUpperCase(Locale.ENGLISH).equals("TITLESCRIPT")) {
    return;
  }
  // process titletag
}

Specifying Locale.ROOT is a suitable alternative under conditions where an English-specific locale would not be appropriate.

...

Code Block
bgColor#ccccff
langjava
public static void processTitleprocessTag(String tag) {
  Locale.setDefault(Locale.ENGLISH);

  if (!tag.toUpperCase().equals("TITLESCRIPT")) {
    return;
  }
  // process titletag
}

Compliant Solution (String.equalsIgnoreCase())

...

Code Block
bgColor#ccccff
langjava
public static void processTitleprocessTag(String tag) {
  if (!tag.equalsIgnoreCase("TITLESCRIPT")) {
    return;
  }
  // process titletag
}

Noncompliant Code Example (FileReader)

...