diff --git a/infer/models/java/src/com/facebook/infer/models/InferUtils.java b/infer/models/java/src/com/facebook/infer/models/InferUtils.java new file mode 100644 index 000000000..b0b87e8f2 --- /dev/null +++ b/infer/models/java/src/com/facebook/infer/models/InferUtils.java @@ -0,0 +1,24 @@ +// Copyright (c) 2015-Present Facebook. All rights reserved. + +package com.facebook.infer.models; + +public class InferUtils { + + public static boolean isValidCharset(String charsetName) { + return charsetName == "UTF8" + || charsetName == "utf8" + || charsetName == "UTF-8" + || charsetName == "utf-8" + || charsetName == "US-ASCII" + || charsetName == "us-ascii" + || charsetName == "ISO-8859-1" + || charsetName == "iso-8859-1" + || charsetName == "UTF-16BE" + || charsetName == "utf-16be" + || charsetName == "UTF-16LE" + || charsetName == "utf-16le" + || charsetName == "UTF-16" + || charsetName == "utf-16"; + } + +} diff --git a/infer/models/java/src/java/io/ByteArrayInputStream.java b/infer/models/java/src/java/io/ByteArrayInputStream.java index f17d35503..fdf92d310 100644 --- a/infer/models/java/src/java/io/ByteArrayInputStream.java +++ b/infer/models/java/src/java/io/ByteArrayInputStream.java @@ -1,3 +1,5 @@ +// Copyright (c) 2015-Present Facebook. All rights reserved. + package java.io; public class ByteArrayInputStream extends InputStream implements Closeable { diff --git a/infer/models/java/src/java/io/ByteArrayOutputStream.java b/infer/models/java/src/java/io/ByteArrayOutputStream.java index b4a30643e..d9ebdb5be 100644 --- a/infer/models/java/src/java/io/ByteArrayOutputStream.java +++ b/infer/models/java/src/java/io/ByteArrayOutputStream.java @@ -1,3 +1,5 @@ +// Copyright (c) 2015-Present Facebook. All rights reserved. + package java.io; public class ByteArrayOutputStream extends OutputStream implements Closeable { diff --git a/infer/models/java/src/java/io/InputStreamReader.java b/infer/models/java/src/java/io/InputStreamReader.java index aa5805f8c..3898764fa 100644 --- a/infer/models/java/src/java/io/InputStreamReader.java +++ b/infer/models/java/src/java/io/InputStreamReader.java @@ -6,6 +6,7 @@ package java.io; import com.facebook.infer.models.InferUndefined; +import com.facebook.infer.models.InferUtils; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; @@ -22,10 +23,7 @@ public class InputStreamReader extends Reader { throws UnsupportedEncodingException { if (charsetName == null) throw new NullPointerException("charsetName"); - else if (charsetName == "UTF8" || charsetName == "UTF-8" - || charsetName == "US-ASCII" || charsetName == "ISO-8859-1" - || charsetName == "UTF-16BE" || charsetName == "UTF-16LE" - || charsetName == "UTF-16") { + else if (InferUtils.isValidCharset(charsetName)) { this.in = in; } else throw new UnsupportedEncodingException(); diff --git a/infer/models/java/src/java/io/OutputStreamWriter.java b/infer/models/java/src/java/io/OutputStreamWriter.java index 2fb28883e..49626ba3f 100644 --- a/infer/models/java/src/java/io/OutputStreamWriter.java +++ b/infer/models/java/src/java/io/OutputStreamWriter.java @@ -6,6 +6,7 @@ package java.io; import com.facebook.infer.models.InferUndefined; +import com.facebook.infer.models.InferUtils; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; @@ -18,10 +19,7 @@ public class OutputStreamWriter extends Writer { throws UnsupportedEncodingException { if (charsetName == null) throw new NullPointerException("charsetName"); - else if (charsetName == "UTF8" || charsetName == "UTF-8" - || charsetName == "US-ASCII" || charsetName == "ISO-8859-1" - || charsetName == "UTF-16BE" || charsetName == "UTF-16LE" - || charsetName == "UTF-16") { + else if (InferUtils.isValidCharset(charsetName)) { this.out = out; } else throw new UnsupportedEncodingException(); diff --git a/infer/tests/codetoanalyze/java/infer/ResourceLeaks.java b/infer/tests/codetoanalyze/java/infer/ResourceLeaks.java index 50ca6fed7..9304bd1d9 100644 --- a/infer/tests/codetoanalyze/java/infer/ResourceLeaks.java +++ b/infer/tests/codetoanalyze/java/infer/ResourceLeaks.java @@ -12,6 +12,7 @@ import com.google.common.base.Preconditions; import com.google.common.io.Closeables; import java.io.BufferedInputStream; +import java.io.BufferedReader; import java.io.Closeable; import java.io.DataOutputStream; import java.io.File; @@ -20,6 +21,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; @@ -880,4 +882,24 @@ public class ResourceLeaks { } } + public InputStreamReader withCharset(URLConnection urlConnection) { + InputStreamReader reader = null; + try { + reader = new InputStreamReader( + urlConnection.getInputStream(), + "iso-8859-1"); + } catch (Exception e) { + return null; + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + // do nothing + } + } + } + return reader; + } + }