Skip to content

Commit 7f3445e

Browse files
author
Clifford W. Johnson
authored
Merge pull request #5 from myronkscott/fix_array_parsing
TDB-19029: avoid for loop that is unbounded based on input
2 parents 327b8c0 + be4da5f commit 7f3445e

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

src/main/java/com/tc/object/management/SerializationHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, Clas
4747
if (cname.startsWith("[")) {
4848
// An array
4949
Class<?> component;
50-
int dcount;
51-
for (dcount = 1; cname.charAt(dcount) == '['; dcount++) ;
52-
if (cname.charAt(dcount) == 'L') {
50+
int dcount = cname.lastIndexOf('[') + 1;
51+
if (dcount < cname.length() && cname.charAt(dcount) == 'L') {
5352
component = lookupClass(cname.substring(dcount + 1, cname.length() - 1));
5453
} else {
5554
// primitive array
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2003-2008 Terracotta, Inc.
3+
* Copyright Super iPaaS Integration LLC, an IBM Company 2024
4+
*/
5+
6+
package com.tc.object.management;
7+
8+
import com.tc.util.Assert;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.ObjectOutputStream;
11+
import org.junit.After;
12+
import org.junit.AfterClass;
13+
import org.junit.Before;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
17+
/**
18+
*
19+
*/
20+
public class SerializationHelperTest {
21+
22+
public SerializationHelperTest() {
23+
}
24+
25+
@BeforeClass
26+
public static void setUpClass() {
27+
}
28+
29+
@AfterClass
30+
public static void tearDownClass() {
31+
}
32+
33+
@Before
34+
public void setUp() {
35+
}
36+
37+
@After
38+
public void tearDown() {
39+
}
40+
41+
@Test
42+
public void testArrayClassResolve() throws Exception {
43+
byte[] raw;
44+
String[] sa = new String[] {"foo", "bar", "baz"};
45+
46+
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
47+
ObjectOutputStream oo = new ObjectOutputStream(out)) {
48+
oo.writeObject(sa);
49+
oo.flush();
50+
raw = out.toByteArray();
51+
}
52+
53+
Object check = SerializationHelper.deserialize(raw, getClass().getClassLoader());
54+
Assert.assertTrue(check, check.getClass().isArray());
55+
Assert.assertTrue(check, check.getClass().getComponentType().isAssignableFrom(String.class));
56+
String[] look = (String[])check;
57+
Assert.assertEquals(sa.length, look.length);
58+
for (int x=0;x<sa.length;x++) {
59+
Assert.assertEquals(sa[x], look[x]);
60+
}
61+
}
62+
63+
64+
@Test
65+
public void testMultiArrayResolve() throws Exception {
66+
byte[] raw;
67+
String[][] sa = new String[][] {{"foo"}, {"bar"}, {"baz"}};
68+
69+
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
70+
ObjectOutputStream oo = new ObjectOutputStream(out)) {
71+
oo.writeObject(sa);
72+
oo.flush();
73+
raw = out.toByteArray();
74+
}
75+
76+
Object check = SerializationHelper.deserialize(raw, getClass().getClassLoader());
77+
Assert.assertTrue(check, check.getClass().isArray());
78+
Assert.assertTrue(check, check.getClass().getComponentType().isArray());
79+
}
80+
}

0 commit comments

Comments
 (0)