Skip to content

Commit 56d17ef

Browse files
committed
feat: wrap native calls in null checks to prevent panic
Signed-off-by: jogerj <30559735+jogerj@users.noreply.github.com>
1 parent fc63072 commit 56d17ef

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

minify-html-java/src/main/java/in/wilsonl/minifyhtml/MinifyHtml.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public class MinifyHtml {
2222
throw new RuntimeException(String.format("Platform not supported (os.name=%s, os.arch=%s)", osName, osArch));
2323
}
2424

25-
final String nativeLibFile = String.format("/%s-%s.nativelib", nativeLibNameOs, nativeLibNameArch);
25+
final String nativeLibFile = String.format("%s-%s.nativelib", nativeLibNameOs, nativeLibNameArch);
2626

27-
try (InputStream is = MinifyHtml.class.getResourceAsStream(nativeLibFile)) {
28-
final File temp = File.createTempFile("minify-html-java-nativelib", nativeLibFile.substring(1));
27+
try (InputStream is = MinifyHtml.class.getResourceAsStream("/" + nativeLibFile)) {
28+
final File temp = File.createTempFile("minify-html-java-nativelib", nativeLibFile);
2929
temp.deleteOnExit();
3030
Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
3131
System.load(temp.getAbsolutePath());
@@ -41,11 +41,22 @@ private MinifyHtml() {
4141
* Minify HTML code represented as a {@link String}.
4242
* The {@link String} will be copied to a UTF-8 byte array in native code, and then copied back into a Java {@link String}.
4343
*
44-
* @param code HTML code to minify
45-
* @param cfg {@link Configuration} minification settings to use
44+
* @param code HTML code to minify, cannot be null
45+
* @param cfg {@link Configuration} minification settings to use, cannot be null
4646
* @return minified HTML code
47+
* @throws IllegalArgumentException if either {@code code} or {@code cfg} is null
4748
*/
48-
public static native String minify(String code, Configuration cfg);
49+
public static String minify(String code, Configuration cfg) {
50+
if (code == null) {
51+
throw new IllegalArgumentException("code cannot be null");
52+
}
53+
if (cfg == null) {
54+
throw new IllegalArgumentException("configuration cannot be null");
55+
}
56+
return minifyRs(code, cfg);
57+
}
58+
59+
private static native String minifyRs(String code, Configuration cfg);
4960

5061
private static String getNativeLibNameOs(String osName) {
5162
if (osName.startsWith("windows")) {

minify-html-java/src/main/rust/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn build_cfg(env: &JNIEnv, obj: &JObject) -> Cfg {
3131
}
3232

3333
#[no_mangle]
34-
pub extern "system" fn Java_in_wilsonl_minifyhtml_MinifyHtml_minify(
34+
pub extern "system" fn Java_in_wilsonl_minifyhtml_MinifyHtml_minifyRs(
3535
env: JNIEnv,
3636
_class: JClass,
3737
input: JString,

0 commit comments

Comments
 (0)