Skip to content

Commit 966468d

Browse files
authored
Merge pull request #14 from FlintMC/fix/bad-stream-read
Fix/bad stream read
2 parents 54c0f63 + 32a5b69 commit 966468d

File tree

8 files changed

+84
-90
lines changed

8 files changed

+84
-90
lines changed

src/main/java/net/flintmc/gradle/environment/DeobfuscationEnvironment.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ static DeobfuscationEnvironment createFor(EnvironmentInput input) {
4242
String name();
4343

4444
/**
45-
* Runs the deobfuscation on the given client and server artifacts. One of the 2 artifacts may
46-
* be null, but never both at the same time.
45+
* Runs the deobfuscation on the given client and server artifacts. One of the 2 artifacts may be null, but never both
46+
* at the same time.
4747
*
4848
* @param clientPom The client artifact, may be null if serverPom is not null
4949
* @param serverPom The client artifact, may be null if clientPom is not null
5050
* @param utilities Various utilities useful during deobfuscation
51+
* @throws DeobfuscationException If the deobfuscation fails
5152
*/
5253
void runDeobfuscation(MavenPom clientPom, MavenPom serverPom, DeobfuscationUtilities utilities)
5354
throws DeobfuscationException;

src/main/java/net/flintmc/gradle/environment/mcp/ModCoderPackEnvironment.java

-12
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,6 @@ public void runDeobfuscation(MavenPom clientPom, MavenPom serverPom, Deobfuscati
296296
}
297297
}
298298

299-
/**
300-
* {@inheritDoc}
301-
*
302-
* @param client
303-
* @param server
304-
*/
305299
@Override
306300
public Collection<MavenArtifact> getCompileArtifacts(MavenArtifact client, MavenArtifact server) {
307301
String version = getVersion(client, server);
@@ -311,12 +305,6 @@ public Collection<MavenArtifact> getCompileArtifacts(MavenArtifact client, Maven
311305
return Collections.singletonList(getJoinedArtifact(version));
312306
}
313307

314-
/**
315-
* {@inheritDoc}
316-
*
317-
* @param client
318-
* @param server
319-
*/
320308
@Override
321309
public Collection<MavenArtifact> getRuntimeArtifacts(MavenArtifact client, MavenArtifact server) {
322310
String version = getVersion(client, server);

src/main/java/net/flintmc/gradle/environment/mcp/function/InjectFunction.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class InjectFunction extends MCPFunction {
3838
*
3939
* @param name The name of the function
4040
* @param output The output of the function
41+
* @param input The input of the function
4142
*/
4243
public InjectFunction(String name, Path input, Path output) {
4344
super(name, output);
@@ -49,14 +50,14 @@ public InjectFunction(String name, Path input, Path output) {
4950
*/
5051
@Override
5152
public void execute(DeobfuscationUtilities utilities) throws DeobfuscationException {
52-
try (
53+
try(
5354
ZipInputStream inputStream = new ZipInputStream(Files.newInputStream(input));
5455
ZipOutputStream outputStream = new ZipOutputStream(Files.newOutputStream(output))
5556
) {
5657
ZipEntry entry;
5758

5859
// Iterate over every entry
59-
while ((entry = inputStream.getNextEntry()) != null) {
60+
while((entry = inputStream.getNextEntry()) != null) {
6061
// Copy the entry one to one
6162
outputStream.putNextEntry(entry);
6263
Util.copyStream(inputStream, outputStream);
@@ -69,7 +70,7 @@ public void execute(DeobfuscationUtilities utilities) throws DeobfuscationExcept
6970
entry = new ZipEntry(".mcp-processed");
7071
outputStream.putNextEntry(entry);
7172
outputStream.closeEntry();
72-
} catch (IOException e) {
73+
} catch(IOException e) {
7374
throw new DeobfuscationException("Failed to execute inject function named " + name, e);
7475
}
7576
}

src/main/java/net/flintmc/gradle/environment/mcp/function/JavaExecutionFunction.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ public class JavaExecutionFunction extends MCPFunction {
4343
/**
4444
* Constructs a new java execution function with the given name, input and output.
4545
*
46-
* @param name The name of the function
47-
* @param output The output of the function
46+
* @param name The name of the function
47+
* @param output The output of the function
48+
* @param executionArtifact The artifact to execute
49+
* @param artifactRepository The repository to retrieve the artifact from if it is missing
50+
* @param args The arguments to pass to the executable
51+
* @param jvmArgs The arguments to pass to the JVM executing the executable
4852
*/
4953
public JavaExecutionFunction(
5054
String name,
@@ -67,31 +71,31 @@ public JavaExecutionFunction(
6771
@Override
6872
public void prepare(DeobfuscationUtilities utilities) throws DeobfuscationException {
6973
SimpleMavenRepository localRepository = utilities.getInternalRepository();
70-
if (!localRepository.isInstalled(executionArtifact)) {
74+
if(!localRepository.isInstalled(executionArtifact)) {
7175
// Retrieve utilities
7276
MavenArtifactDownloader downloader = utilities.getDownloader();
7377

7478
// The artifact is not installed already, install it
75-
if (artifactRepository == null) {
79+
if(artifactRepository == null) {
7680
// Can't download anything in offline mode
7781
throw new DeobfuscationException("Missing artifact " + executionArtifact + " in local repository, " +
7882
"but working in offline mode");
7983
}
8084

8185
boolean setupSource = !downloader.hasSource(artifactRepository);
82-
if (setupSource) {
86+
if(setupSource) {
8387
// The download has the source not set already, add it now
8488
downloader.addSource(artifactRepository);
8589
}
8690

8791
try {
8892
// Install the artifact including dependencies
8993
downloader.installAll(executionArtifact, localRepository, true);
90-
} catch (IOException | MavenResolveException e) {
94+
} catch(IOException | MavenResolveException e) {
9195
throw new DeobfuscationException("Failed to install execution artifact", e);
9296
}
9397

94-
if (setupSource) {
98+
if(setupSource) {
9599
// We added the source, clean up afterwards
96100
downloader.removeSource(artifactRepository);
97101
}
@@ -118,18 +122,18 @@ public void execute(DeobfuscationUtilities utilities) throws DeobfuscationExcept
118122
args,
119123
jvmArgs
120124
);
121-
} catch (IOException e) {
125+
} catch(IOException e) {
122126
// This happens if the execution fails, but has nothing to do with the exit code
123127
throw new DeobfuscationException("Failed to execute java process for function " + name);
124128
}
125129

126-
if (result.getExitCode() != 0) {
130+
if(result.getExitCode() != 0) {
127131
// The process failed to execute properly
128132
Path standardOutput;
129133
try {
130134
// Try to save the standard output
131135
standardOutput = saveOutput("stdout", result.getStdout());
132-
} catch (IOException e) {
136+
} catch(IOException e) {
133137
throw new DeobfuscationException("Failed to save standard output after process failed, " +
134138
"some is very wrong", e);
135139
}
@@ -138,7 +142,7 @@ public void execute(DeobfuscationUtilities utilities) throws DeobfuscationExcept
138142
try {
139143
// Try to save the standard error
140144
standardError = saveOutput("stderr", result.getStderr());
141-
} catch (IOException e) {
145+
} catch(IOException e) {
142146
throw new DeobfuscationException("Failed to save standard error after process failed, " +
143147
"some is very wrong", e);
144148
}

src/main/java/net/flintmc/gradle/environment/mcp/function/PatchFunction.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ public class PatchFunction extends MCPFunction {
4545
/**
4646
* Constructs a new Patch function with the given name and output.
4747
*
48-
* @param name The name of the function
49-
* @param output The output of the function
48+
* @param name The name of the function
49+
* @param output The output of the function
50+
* @param input The input of the function
51+
* @param patches The path to the patches to apply
5052
*/
5153
public PatchFunction(String name, Path input, Path output, Path patches) {
5254
super(name, output);
@@ -74,7 +76,7 @@ public void prepare(DeobfuscationUtilities utilities) throws DeobfuscationExcept
7476
.replace("\\", "/");
7577
indexedPatches.put(relativePath, patchPath);
7678
});
77-
} catch (IOException e) {
79+
} catch(IOException e) {
7880
throw new DeobfuscationException("IO error occurred while collecting patch files", e);
7981
}
8082
}
@@ -84,17 +86,17 @@ public void prepare(DeobfuscationUtilities utilities) throws DeobfuscationExcept
8486
*/
8587
@Override
8688
public void execute(DeobfuscationUtilities utilities) throws DeobfuscationException {
87-
try (
89+
try(
8890
ZipInputStream inputStream = new ZipInputStream(Files.newInputStream(input));
8991
ZipOutputStream outputStream = new ZipOutputStream(Files.newOutputStream(output))
9092
) {
9193
ZipEntry entry;
9294
// Iterate all entries
93-
while ((entry = inputStream.getNextEntry()) != null) {
95+
while((entry = inputStream.getNextEntry()) != null) {
9496
outputStream.putNextEntry(entry);
9597

9698
// Check if there is a patch for the entry if it is not a directory
97-
if (!entry.isDirectory() && indexedPatches.containsKey(entry.getName())) {
99+
if(!entry.isDirectory() && indexedPatches.containsKey(entry.getName())) {
98100
// Parse the patch file (unified diff format)
99101
Patch<String> patch =
100102
UnifiedDiffUtils.parseUnifiedDiff(Files.readAllLines(indexedPatches.get(entry.getName())));
@@ -104,7 +106,7 @@ public void execute(DeobfuscationUtilities utilities) throws DeobfuscationExcept
104106

105107
try {
106108
patchedLines = DiffUtils.patch(originalLines, patch);
107-
} catch (PatchFailedException e) {
109+
} catch(PatchFailedException e) {
108110
throw new DeobfuscationException("Failed to patch file " + entry.getName(), e);
109111
}
110112

@@ -117,7 +119,7 @@ public void execute(DeobfuscationUtilities utilities) throws DeobfuscationExcept
117119
// Make sure to close the entry
118120
outputStream.closeEntry();
119121
}
120-
} catch (IOException e) {
122+
} catch(IOException e) {
121123
throw new DeobfuscationException("IO error occurred while patching", e);
122124
}
123125
}

src/main/java/net/flintmc/gradle/extension/FlintStaticFileDescription.java

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public FlintStaticFileDescription(Project project, String name) {
6262
* Sets the source this static file should be obtained from.
6363
*
6464
* @param source The source of this static file
65+
* @throws URISyntaxException If source is not a valid URL
6566
*/
6667
public void from(Object source) throws URISyntaxException {
6768
if(source instanceof URI) {

src/main/java/net/flintmc/gradle/manifest/tasks/GenerateStaticFileChecksumsTask.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@
1919

2020
package net.flintmc.gradle.manifest.tasks;
2121

22-
import net.flintmc.gradle.FlintGradleException;
2322
import net.flintmc.gradle.manifest.cache.StaticFileChecksums;
2423
import net.flintmc.gradle.manifest.data.ManifestStaticFile;
2524
import net.flintmc.gradle.manifest.data.ManifestStaticFileInput;
2625
import net.flintmc.gradle.util.MaybeNull;
2726
import net.flintmc.gradle.util.Util;
2827
import okhttp3.OkHttpClient;
29-
import okhttp3.Request;
30-
import okhttp3.Response;
3128
import org.gradle.api.DefaultTask;
3229
import org.gradle.api.tasks.Input;
3330
import org.gradle.api.tasks.InputFiles;
@@ -139,7 +136,7 @@ public void generate() throws IOException {
139136
for(URI remoteFile : getRemoteFiles()) {
140137
// Calculate the checksum
141138
try(InputStream stream = Util.getURLStream(httpClient, remoteFile)) {
142-
checksums.add(remoteFile, Util.md5Hex(Util.toByteArray(stream)));
139+
checksums.add(remoteFile, Util.md5Hex(Util.readAllAsBytes(stream)));
143140
}
144141
}
145142
} else {

0 commit comments

Comments
 (0)