Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

IndexOutOfBoundsException - Wrong formatted content when dealing with latex String

Open
#1,439 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
java
Domain
tooling

Research direction

Reproduce the failure with the supplied Java class, then inspect StringWrapper#applyReplacements and FormatterException#formatDiagnostics. Check the generated source and diagnostic line handling against the original input. Done means formatting no longer produces uncompilable Java and the formatter reports the error without IndexOutOfBoundsException.

Written by the indexing model from the issue text.

Description

Give this class

package question;

class T {

  private static final String JSON =
      "{\n"
          + "  \"a\": \"AAAAA AAA AAA AAA AA AAA AA AAAAA AA AAAAA AAA AAAA AAAA AA AAAA AAAA AAAAA AAAAA AAAAA AAAAA AAAAAA AAAAA AAAAAA AAAAA AAA at $-15.4{^{\\\\,\\\\circ}\\\\text{C}}$ BBB BBBBB BB BBB $42.7{^{\\\\,\\\\circ}\\\\text{C}}$?\\n\\n(CC CC CCC CC CCCCC CCC CCC CCC CCC CCC CC = $1.95\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$; DDD DDD DDDD DDDD DDDDD DDDD DDD DD DDDD = $1.18\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$C; $\\\\Delta H_\\\\text{fus}$ = 5.72 kJ/mol; normal freezing point, $T_f$ = $28.3{^{\\\\,\\\\circ}\\\\text{C}}$)\",\n"
          + "  \"b\": \"*EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EEE.*\\n\\nFF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F $-15.4^\\\\circ\\\\text C$ to $28.3^\\\\circ\\\\text C$.\\n\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.95\\\\;\\\\mathrm{J/g\\\\cdot °C})(28.3\\\\;\\\\mathrm{ °C}-(-15.4)\\\\;\\\\mathrm{ °C})=860.6715\\\\;\\\\mathrm{J} \\\\]\\n\\nGG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GGGGGGGGGGGGGGGGGGGGG.\\n\\\\[q=nH_{fus}=(\\\\frac{10.1\\\\;\\\\mathrm{g}}{67.44\\\\;\\\\mathrm{g/mol}})(5720\\\\;\\\\mathrm{J})=856.6429\\\\;\\\\mathrm{J}\\\\]\\n\\nHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH $28.3^\\\\circ\\\\text C$ to $42.7^\\\\circ\\\\text C$.\\n\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.18\\\\;\\\\mathrm{J/g\\\\cdot °C})(42.7\\\\;\\\\mathrm{ °C}-28.3\\\\;\\\\mathrm{ °C})=171.6192\\\\;\\\\mathrm{J}\\\\]\\n\\nIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII.\\n\\\\[860.6715\\\\;\\\\mathrm{J}+856.6429\\\\;\\\\mathrm{J}+171.6192\\\\;\\\\mathrm{J}=1888.9336\\\\;\\\\mathrm{J}=1.89\\\\;\\\\mathrm{kJ}\\\\]\",\n"
          + "}";

  private static final String JSON2 =
      "{\n"
          + "  \"a\": \"AAAAA AAA AAA AAA AA AAA AA AAAAA AA AAAAA AAA AAAA AAAA AA AAAA AAAA AAAAA AAAAA AAAAA AAAAA AAAAAA AAAAA AAAAAA AAAAA AAA at $-15.4{^{\\\\,\\\\circ}\\\\text{C}}$ BBB BBBBB BB BBB $42.7{^{\\\\,\\\\circ}\\\\text{C}}$?\\n\\n(CC CC CCC CC CCCCC CCC CCC CCC CCC CCC CC = $1.95\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$; DDD DDD DDDD DDDD DDDDD DDDD DDD DD DDDD = $1.18\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$C; $\\\\Delta H_\\\\text{fus}$ = 5.72 kJ/mol; normal freezing point, $T_f$ = $28.3{^{\\\\,\\\\circ}\\\\text{C}}$)\",\n"
          + "  \"b\": \"*EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EEE.*\\n\\nFF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F $-15.4^\\\\circ\\\\text C$ to $28.3^\\\\circ\\\\text C$.\\n\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.95\\\\;\\\\mathrm{J/g\\\\cdot °C})(28.3\\\\;\\\\mathrm{ °C}-(-15.4)\\\\;\\\\mathrm{ °C})=860.6715\\\\;\\\\mathrm{J} \\\\]\\n\\nGG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GGGGGGGGGGGGGGGGGGGGG.\\n\\\\[q=nH_{fus}=(\\\\frac{10.1\\\\;\\\\mathrm{g}}{67.44\\\\;\\\\mathrm{g/mol}})(5720\\\\;\\\\mathrm{J})=856.6429\\\\;\\\\mathrm{J}\\\\]\\n\\nHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH $28.3^\\\\circ\\\\text C$ to $42.7^\\\\circ\\\\text C$.\\n\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.18\\\\;\\\\mathrm{J/g\\\\cdot °C})(42.7\\\\;\\\\mathrm{ °C}-28.3\\\\;\\\\mathrm{ °C})=171.6192\\\\;\\\\mathrm{J}\\\\]\\n\\nIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII.\\n\\\\[860.6715\\\\;\\\\mathrm{J}+856.6429\\\\;\\\\mathrm{J}+171.6192\\\\;\\\\mathrm{J}=1888.9336\\\\;\\\\mathrm{J}=1.89\\\\;\\\\mathrm{kJ}\\\\]\",\n"
          + "}";

  public static void main(String[] args) {
    System.out.println(JSON);
  }

}

I ran the main method to ensure it is valid java source code
Image

But when trying to format it using GJF, I got

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 42 out of bounds for length 22
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
	at java.base/java.util.Objects.checkIndex(Objects.java:385)
	at java.base/java.util.ArrayList.get(ArrayList.java:427)
	at java.base/java.util.Collections$UnmodifiableList.get(Collections.java:1502)
	at com.google.googlejavaformat.java.FormatterException.formatDiagnostics(FormatterException.java:70)
	at com.google.googlejavaformat.java.Main.formatFiles(Main.java:177)
	at com.google.googlejavaformat.java.Main.format(Main.java:128)
	at com.google.googlejavaformat.java.Main.main(Main.java:91)
	at com.google.googlejavaformat.java.Main.main(Main.java:81)
	at com.google.googlejavaformat.java.Main.main(Main.java:70)

Digging into the GJF, I found 2 issues:

  1. The com.google.googlejavaformat.java.StringWrapper#applyReplacements give wrong result:
package question;

class T {

  private static final String JSON =
      "{\n"
          + "  \"a\": \"AAAAA AAA AAA AAA AA AAA AA AAAAA AA AAAAA AAA AAAA AAAA AA AAAA AAAA AAAAA"
          + " AAAAA AAAAA AAAAA AAAAAA AAAAA AAAAAA AAAAA AAA at $-15.4{^{\\\\,\\\\circ}\\\"
          + "\text{C}}$ BBB BBBBB BB BBB $42.7{^{\\\\,\\\\circ}\\\\text{C}}$?\\n"
          + "\\n"
          + "(CC CC CCC CC CCCCC CCC CCC CCC CCC CCC CC = $1.95\\\\;{\\\\text{J}/\\\"
          + "\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$; DDD DDD DDDD DDDD DDDDD DDDD DDD DD DDDD ="
          + " $1.18\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$C; $\\\\Delta"
          + " H_\\\\text{fus}$ = 5.72 kJ/mol; normal freezing point, $T_f$ ="
          + " $28.3{^{\\\\,\\\\circ}\\\\text{C}}$)\",\n"
          + "  \"b\": \"*EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE"
          + " EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE"
          + " EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EEE.*\\n"
          + "\\n"
          + "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F"
          + " $-15.4^\\\\circ\\\\text C$ to $28.3^\\\\circ\\\\text C$.\\n"
          + "\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.95\\\\;\\\\mathrm{J/g\\\\cdot"
          + " °C})(28.3\\\\;\\\\mathrm{ °C}-(-15.4)\\\\;\\\\mathrm{ °C})=860.6715\\\\;\\\\mathrm{J}"
          + " \\\\]\\n"
          + "\\n"
          + "GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG"
          + " GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GGGGGGGGGGGGGGGGGGGGG.\\n"
          + "\\\\[q=nH_{fus}=(\\\\frac{10.1\\\\;\\\\mathrm{g}}{67.44\\\\;\\\\mathrm{g/mol}})(5720\\\\;\\\\mathrm{J})=856.6429\\\\;\\\\mathrm{J}\\\\]\\n"
          + "\\n"
          + "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH $28.3^\\\\circ\\\\text C$ to"
          + " $42.7^\\\\circ\\\\text C$.\\n"
          + "\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.18\\\\;\\\\mathrm{J/g\\\\cdot"
          + " °C})(42.7\\\\;\\\\mathrm{ °C}-28.3\\\\;\\\\mathrm{"
          + " °C})=171.6192\\\\;\\\\mathrm{J}\\\\]\\n"
          + "\\n"
          + "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII.\\n"
          + "\\\\[860.6715\\\\;\\\\mathrm{J}+856.6429\\\\;\\\\mathrm{J}+171.6192\\\\;\\\\mathrm{J}=1888.9336\\\\;\\\\mathrm{J}=1.89\\\\;\\\\mathrm{kJ}\\\\]\",\n"
          + "}";

  private static final String JSON2 =
      "{\n"
          + "  \"a\": \"AAAAA AAA AAA AAA AA AAA AA AAAAA AA AAAAA AAA AAAA AAAA AA AAAA AAAA AAAAA"
          + " AAAAA AAAAA AAAAA AAAAAA AAAAA AAAAAA AAAAA AAA at $-15.4{^{\\\\,\\\\circ}\\\"
          + "\text{C}}$ BBB BBBBB BB BBB $42.7{^{\\\\,\\\\circ}\\\\text{C}}$?\\n"
          + "\\n"
          + "(CC CC CCC CC CCCCC CCC CCC CCC CCC CCC CC = $1.95\\\\;{\\\\text{J}/\\\"
          + "\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$; DDD DDD DDDD DDDD DDDDD DDDD DDD DD DDDD ="
          + " $1.18\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$C; $\\\\Delta"
          + " H_\\\\text{fus}$ = 5.72 kJ/mol; normal freezing point, $T_f$ ="
          + " $28.3{^{\\\\,\\\\circ}\\\\text{C}}$)\",\n"
          + "  \"b\": \"*EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE"
          + " EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE"
          + " EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EEE.*\\n"
          + "\\n"
          + "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F"
          + " $-15.4^\\\\circ\\\\text C$ to $28.3^\\\\circ\\\\text C$.\\n"
          + "\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.95\\\\;\\\\mathrm{J/g\\\\cdot"
          + " °C})(28.3\\\\;\\\\mathrm{ °C}-(-15.4)\\\\;\\\\mathrm{ °C})=860.6715\\\\;\\\\mathrm{J}"
          + " \\\\]\\n"
          + "\\n"
          + "GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG"
          + " GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GGGGGGGGGGGGGGGGGGGGG.\\n"
          + "\\\\[q=nH_{fus}=(\\\\frac{10.1\\\\;\\\\mathrm{g}}{67.44\\\\;\\\\mathrm{g/mol}})(5720\\\\;\\\\mathrm{J})=856.6429\\\\;\\\\mathrm{J}\\\\]\\n"
          + "\\n"
          + "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH $28.3^\\\\circ\\\\text C$ to"
          + " $42.7^\\\\circ\\\\text C$.\\n"
          + "\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.18\\\\;\\\\mathrm{J/g\\\\cdot"
          + " °C})(42.7\\\\;\\\\mathrm{ °C}-28.3\\\\;\\\\mathrm{"
          + " °C})=171.6192\\\\;\\\\mathrm{J}\\\\]\\n"
          + "\\n"
          + "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII.\\n"
          + "\\\\[860.6715\\\\;\\\\mathrm{J}+856.6429\\\\;\\\\mathrm{J}+171.6192\\\\;\\\\mathrm{J}=1888.9336\\\\;\\\\mathrm{J}=1.89\\\\;\\\\mathrm{kJ}\\\\]\",\n"
          + "}";

  public static void main(String[] args) {
    System.out.println(JSON);
  }
}

This result is completely not compilable
Image

  1. com.google.googlejavaformat.java.FormatterException#formatDiagnostics using wrong param to format the diagnostics: It uses the original java content (22 lines intotal) instead of formatted java content (79 lines in total). That is why we getting IndexOutOfBoundsException
Dominant language
Java
Stars
6.2k
Forks
936
Avg merge
6m
Merged PRs (30d)
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from google/google-java-format

All issues in google/google-java-format

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.