RFR: 8349241: Fix the concurrent execution JVM crash of StringBuilder::append(int/long) [v3]

Shaojin Wen swen at openjdk.org
Mon Feb 3 19:38:02 UTC 2025


> The following code can reproduce the problem, writing out of bounds causes JVM Crash
> 
> 
>    StringBuilder buf = new StringBuilder();
>     buf.append('中');
> 
>     final CountDownLatch latch = new CountDownLatch(10);
>     Runnable r = () -> {
>         for (int i = 0; i < 10000; i++) {
>             buf.setLength(0);
>             buf.trimToSize();
>             buf.append(123456789);
>         }
>         latch.countDown();
>     };
>     Thread[] threads = new Thread[10];
>     for (int i = 0; i < threads.length; i++) {
>         threads[i] = new Thread(r);
>     }
>     for (Thread t : threads) {
>         t.start();
>     }
>     latch.await();
> }
> 
> 
> This problem can be avoided by using the value of ensureCapacityInternal directly.

Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:

  simplify

-------------

Changes:
  - all: https://git.openjdk.org/jdk/pull/23427/files
  - new: https://git.openjdk.org/jdk/pull/23427/files/037973d5..26c0cb4a

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=23427&range=02
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=23427&range=01-02

  Stats: 5 lines in 1 file changed: 1 ins; 0 del; 4 mod
  Patch: https://git.openjdk.org/jdk/pull/23427.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/23427/head:pull/23427

PR: https://git.openjdk.org/jdk/pull/23427


More information about the core-libs-dev mailing list