Skip to content

Commit 1575c4f

Browse files
authored
fix(config): close resource streams (#7002)
1 parent f62b25f commit 1575c4f

9 files changed

Lines changed: 197 additions & 59 deletions

File tree

‎actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.tron.common.crypto.zksnark.PairingCheck;
6262
import org.tron.common.es.ExecutorServiceManager;
6363
import org.tron.common.math.StrictMathWrapper;
64+
import org.tron.common.parameter.CommonParameter;
6465
import org.tron.common.runtime.ProgramResult;
6566
import org.tron.common.runtime.vm.DataWord;
6667
import org.tron.common.utils.BIUtil;

‎common/src/main/java/org/tron/core/config/Configuration.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private static void resolveConfigFile(String fileName, File confFile) {
5050
if (confFile.exists()) {
5151
config = ConfigFactory.parseFile(confFile)
5252
.withFallback(ConfigFactory.defaultReference());
53-
} else if (Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName)
53+
} else if (Thread.currentThread().getContextClassLoader().getResource(fileName)
5454
!= null) {
5555
config = ConfigFactory.load(fileName);
5656
} else {
@@ -59,4 +59,3 @@ private static void resolveConfigFile(String fileName, File confFile) {
5959
}
6060
}
6161
}
62-

‎framework/src/main/java/org/tron/core/config/args/Args.java‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,9 +1186,8 @@ public String getOutputDirectory() {
11861186
private static void printVersion() {
11871187
Properties properties = new Properties();
11881188
boolean noGitProperties = true;
1189-
try {
1190-
InputStream in = Thread.currentThread()
1191-
.getContextClassLoader().getResourceAsStream("git.properties");
1189+
try (InputStream in = Thread.currentThread()
1190+
.getContextClassLoader().getResourceAsStream("git.properties")) {
11921191
if (in != null) {
11931192
noGitProperties = false;
11941193
properties.load(in);
@@ -1276,9 +1275,8 @@ public static String upperFirst(String name) {
12761275

12771276
private static String getCommitIdAbbrev() {
12781277
Properties properties = new Properties();
1279-
try {
1280-
InputStream in = Thread.currentThread()
1281-
.getContextClassLoader().getResourceAsStream("git.properties");
1278+
try (InputStream in = Thread.currentThread()
1279+
.getContextClassLoader().getResourceAsStream("git.properties")) {
12821280
if (in == null) {
12831281
logger.warn("git.properties not found on classpath");
12841282
return "";

‎framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ public static void librustzcashInitZksnarkParams() {
5757
}
5858

5959
private static String getParamsFile(String fileName) {
60-
InputStream in = Thread.currentThread().getContextClassLoader()
61-
.getResourceAsStream("params" + File.separator + fileName);
60+
String resourcePath = "params" + File.separator + fileName;
6261
File fileOut = new File(System.getProperty("java.io.tmpdir")
6362
+ File.separator + fileName + "." + System.currentTimeMillis());
64-
try {
63+
try (InputStream in = Thread.currentThread().getContextClassLoader()
64+
.getResourceAsStream(resourcePath)) {
65+
if (in == null) {
66+
throw new TronError("Resource not found: " + resourcePath, TronError.ErrCode.ZCASH_INIT);
67+
}
6568
FileUtils.copyToFile(in, fileOut);
6669
} catch (IOException e) {
6770
logger.error(e.getMessage(), e);

‎framework/src/test/java/org/tron/common/utils/FileUtilTest.java‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@
88
import java.io.File;
99
import java.io.FileWriter;
1010
import java.io.IOException;
11-
import java.nio.file.FileVisitResult;
12-
import java.nio.file.FileVisitor;
1311
import java.nio.file.Files;
1412
import java.nio.file.Path;
15-
import java.nio.file.Paths;
16-
import java.nio.file.attribute.BasicFileAttributes;
17-
import java.util.ArrayList;
1813
import java.util.Arrays;
1914
import java.util.Comparator;
2015
import java.util.List;
16+
import java.util.stream.Stream;
2117
import org.junit.After;
22-
import org.junit.Assert;
2318
import org.junit.Before;
2419
import org.junit.Test;
2520

@@ -39,15 +34,16 @@ public void setUp() throws IOException {
3934

4035
@After
4136
public void tearDown() throws IOException {
42-
Files.walk(tempDir)
43-
.sorted(Comparator.reverseOrder())
44-
.forEach(path -> {
45-
try {
46-
Files.delete(path);
47-
} catch (IOException e) {
48-
e.printStackTrace();
49-
}
50-
});
37+
try (Stream<Path> paths = Files.walk(tempDir)) {
38+
paths.sorted(Comparator.reverseOrder())
39+
.forEach(path -> {
40+
try {
41+
Files.delete(path);
42+
} catch (IOException e) {
43+
e.printStackTrace();
44+
}
45+
});
46+
}
5147
}
5248

5349
@Test
@@ -126,4 +122,4 @@ public void testCreateDirIfNotExists() {
126122
}
127123

128124

129-
}
125+
}

‎framework/src/test/java/org/tron/core/exception/TronErrorTest.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.tron.core.exception;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertSame;
46
import static org.junit.Assert.assertThrows;
57
import static org.junit.Assert.fail;
68
import static org.mockito.ArgumentMatchers.any;
@@ -14,7 +16,9 @@
1416
import com.typesafe.config.Config;
1517
import com.typesafe.config.ConfigFactory;
1618
import com.typesafe.config.ConfigObject;
19+
import java.io.File;
1720
import java.io.IOException;
21+
import java.io.InputStream;
1822
import java.lang.reflect.Field;
1923
import java.nio.file.Path;
2024
import java.util.Collections;
@@ -33,6 +37,7 @@
3337
import org.slf4j.LoggerFactory;
3438
import org.tron.common.TestConstants;
3539
import org.tron.common.arch.Arch;
40+
import org.tron.common.exit.ExitManager;
3641
import org.tron.common.log.LogService;
3742
import org.tron.common.parameter.RateLimiterInitialization;
3843
import org.tron.common.utils.ReflectUtils;
@@ -88,6 +93,37 @@ public void ZksnarkInitTest() throws IllegalAccessException, NoSuchFieldExceptio
8893
}
8994
}
9095

96+
@Test
97+
public void testMissingZksnarkParams() throws IllegalAccessException, NoSuchFieldException {
98+
Field field = ZksnarkInitService.class.getDeclaredField("initialized");
99+
field.setAccessible(true);
100+
AtomicBoolean initialized = (AtomicBoolean) field.get(null);
101+
boolean originalValue = initialized.get();
102+
Thread thread = Thread.currentThread();
103+
ClassLoader originalClassLoader = thread.getContextClassLoader();
104+
105+
try {
106+
initialized.set(false);
107+
thread.setContextClassLoader(new ClassLoader(originalClassLoader) {
108+
@Override
109+
public InputStream getResourceAsStream(String name) {
110+
return null;
111+
}
112+
});
113+
TronError thrown = assertThrows(TronError.class,
114+
ZksnarkInitService::librustzcashInitZksnarkParams);
115+
assertEquals(TronError.ErrCode.ZCASH_INIT, thrown.getErrCode());
116+
assertEquals(1, thrown.getErrCode().getCode());
117+
assertEquals("Resource not found: params" + File.separator + "sapling-spend.params",
118+
thrown.getMessage());
119+
assertFalse(initialized.get());
120+
assertSame(thrown, ExitManager.findTronError(new RuntimeException(thrown)).get());
121+
} finally {
122+
thread.setContextClassLoader(originalClassLoader);
123+
initialized.set(originalValue);
124+
}
125+
}
126+
91127
@Test
92128
public void LogLoadTest() throws IOException {
93129
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

‎framework/src/test/java/org/tron/core/services/http/BroadcastServletTest.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,17 @@ public void doPostTest() throws IOException {
146146
}
147147
Assert.assertNotNull(result);
148148
in.close();
149-
writer.flush();
150-
FileInputStream fileInputStream = new FileInputStream("temp.txt");
151-
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
152-
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
149+
writer.close();
153150

154151
StringBuilder sb = new StringBuilder();
155-
String text;
156-
while ((text = bufferedReader.readLine()) != null) {
157-
sb.append(text);
152+
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
153+
new FileInputStream("temp.txt"), StandardCharsets.UTF_8))) {
154+
String text;
155+
while ((text = bufferedReader.readLine()) != null) {
156+
sb.append(text);
157+
}
158158
}
159159
Assert.assertTrue(sb.toString().contains("{\"Error\":\"internal server error\"}"));
160160
httpUrlConnection.disconnect();
161161
}
162-
}
162+
}

‎plugins/src/main/java/common/org/tron/plugins/utils/FileUtils.java‎

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.nio.file.StandardCopyOption;
2121
import java.util.List;
2222
import java.util.Properties;
23+
import java.util.stream.Stream;
2324
import lombok.extern.slf4j.Slf4j;
2425

2526
@Slf4j
@@ -137,39 +138,44 @@ public static boolean isSymbolicLink(File file) throws IOException {
137138
*/
138139
public static void copyDatabases(Path src, Path dest, List<String> subDirs)
139140
throws IOException {
140-
// create subdirs, as using parallel() to run, so should create dirs first.
141-
subDirs.forEach(dir -> {
142-
if (isExists(Paths.get(src.toString(), dir).toString())) {
143-
try {
144-
Files.walk(Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS)
145-
.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
146-
} catch (IOException e) {
147-
logger.error("copy database failed, src: {}, dest: {}, error: {}",
148-
Paths.get(src.toString(), dir), Paths.get(dest.toString(), dir), e.getMessage());
149-
throw new RuntimeException(e);
150-
}
141+
for (String dir : subDirs) {
142+
if (!isExists(Paths.get(src.toString(), dir).toString())) {
143+
continue;
151144
}
152-
});
153-
}
154-
155-
public static void copyDir(Path src, Path dest, String dir) {
156-
if (isExists(Paths.get(src.toString(), dir).toString())) {
157145
try {
158-
if (createDirIfNotExists(Paths.get(dest.toString(), dir).toString())) {
159-
Files.walk(Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS)
160-
.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
161-
} else {
162-
throw new IOException(String.format("dest %s create fail ",
163-
Paths.get(dest.toString(), dir)));
164-
}
146+
copyTree(src, dest, dir);
165147
} catch (IOException e) {
166-
logger.error("copy dir failed, src: {}, dest: {}, error: {}",
148+
logger.error("copy database failed, src: {}, dest: {}, error: {}",
167149
Paths.get(src.toString(), dir), Paths.get(dest.toString(), dir), e.getMessage());
168150
throw new RuntimeException(e);
169151
}
170152
}
171153
}
172154

155+
public static void copyDir(Path src, Path dest, String dir) {
156+
if (!isExists(Paths.get(src.toString(), dir).toString())) {
157+
return;
158+
}
159+
try {
160+
if (!createDirIfNotExists(Paths.get(dest.toString(), dir).toString())) {
161+
throw new IOException(String.format("dest %s create fail ",
162+
Paths.get(dest.toString(), dir)));
163+
}
164+
copyTree(src, dest, dir);
165+
} catch (IOException e) {
166+
logger.error("copy dir failed, src: {}, dest: {}, error: {}",
167+
Paths.get(src.toString(), dir), Paths.get(dest.toString(), dir), e.getMessage());
168+
throw new RuntimeException(e);
169+
}
170+
}
171+
172+
private static void copyTree(Path src, Path dest, String dir) throws IOException {
173+
try (Stream<Path> paths = Files.walk(
174+
Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS)) {
175+
paths.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
176+
}
177+
}
178+
173179
public static void copy(Path source, Path dest) {
174180
try {
175181
// create hard link when file is .sst
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.tron.plugins.utils;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.util.Arrays;
8+
import java.util.Collections;
9+
import org.junit.Assert;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.rules.TemporaryFolder;
13+
14+
public class FileUtilsTest {
15+
16+
@Rule
17+
public final TemporaryFolder folder = new TemporaryFolder();
18+
19+
@Test
20+
public void testCopyDatabasesCopiesTreeAfterMissingDirectory() throws IOException {
21+
Path src = createSourceTree();
22+
Path dest = folder.newFolder().toPath();
23+
24+
FileUtils.copyDatabases(src, dest, Arrays.asList("missing", "database"));
25+
26+
Assert.assertFalse(Files.exists(dest.resolve("missing")));
27+
Assert.assertArrayEquals(new byte[]{1, 2, 3},
28+
Files.readAllBytes(dest.resolve("database/nested/data")));
29+
}
30+
31+
@Test
32+
public void testCopyMethodsSkipMissingSource() throws IOException {
33+
Path src = folder.newFolder().toPath();
34+
Path dest = folder.getRoot().toPath().resolve("missing-dest");
35+
36+
FileUtils.copyDatabases(src, dest, Collections.singletonList("missing"));
37+
FileUtils.copyDir(src, dest, "missing");
38+
39+
Assert.assertFalse(Files.exists(dest));
40+
}
41+
42+
@Test
43+
public void testCopyDatabasesDoesNotCreateDestinationParents() throws IOException {
44+
Path src = createSourceTree();
45+
Path dest = folder.getRoot().toPath().resolve("missing-parent/dest");
46+
47+
RuntimeException error = Assert.assertThrows(RuntimeException.class,
48+
() -> FileUtils.copyDatabases(src, dest, Collections.singletonList("database")));
49+
50+
Assert.assertTrue(error.getCause() instanceof IOException);
51+
Assert.assertFalse(Files.exists(dest.getParent()));
52+
}
53+
54+
@Test
55+
public void testCopyDirCreatesDestinationParents() throws IOException {
56+
Path src = createSourceTree();
57+
Path dest = folder.getRoot().toPath().resolve("missing-parent/dest");
58+
59+
FileUtils.copyDir(src, dest, "database");
60+
61+
Assert.assertArrayEquals(new byte[]{1, 2, 3},
62+
Files.readAllBytes(dest.resolve("database/nested/data")));
63+
}
64+
65+
@Test
66+
public void testCopyMethodsPreserveLeadingSeparatorInDirectory() throws IOException {
67+
Path src = createSourceTree();
68+
Path databasesDest = folder.newFolder().toPath();
69+
Path directoryDest = folder.newFolder().toPath();
70+
String dir = File.separator + "database";
71+
72+
FileUtils.copyDatabases(src, databasesDest, Collections.singletonList(dir));
73+
FileUtils.copyDir(src, directoryDest, dir);
74+
75+
Assert.assertArrayEquals(new byte[]{1, 2, 3},
76+
Files.readAllBytes(databasesDest.resolve("database/nested/data")));
77+
Assert.assertArrayEquals(new byte[]{1, 2, 3},
78+
Files.readAllBytes(directoryDest.resolve("database/nested/data")));
79+
}
80+
81+
@Test
82+
public void testCopyDirWrapsDestinationCreationFailure() throws IOException {
83+
Path src = createSourceTree();
84+
Path dest = folder.newFile().toPath();
85+
86+
RuntimeException error = Assert.assertThrows(RuntimeException.class,
87+
() -> FileUtils.copyDir(src, dest, "database"));
88+
89+
Assert.assertTrue(error.getCause() instanceof IOException);
90+
Assert.assertTrue(error.getCause().getMessage().contains("create fail"));
91+
}
92+
93+
private Path createSourceTree() throws IOException {
94+
Path src = folder.newFolder().toPath();
95+
Path nested = Files.createDirectories(src.resolve("database/nested"));
96+
Files.write(nested.resolve("data"), new byte[]{1, 2, 3});
97+
return src;
98+
}
99+
}

0 commit comments

Comments
 (0)