HBASE-15460 Fix infer issues in hbase-common

Summary: Fix issues found by static analysis

Test Plan: Unit test pass

Differential Revision: https://reviews.facebook.net/D55551
This commit is contained in:
Elliott Clark 2016-03-15 09:52:24 -07:00
parent 1123be9181
commit 75252af3a9
8 changed files with 234 additions and 139 deletions

View File

@ -164,22 +164,28 @@ public class EncodedDataBlock {
*/ */
public static int getCompressedSize(Algorithm algo, Compressor compressor, public static int getCompressedSize(Algorithm algo, Compressor compressor,
byte[] inputBuffer, int offset, int length) throws IOException { byte[] inputBuffer, int offset, int length) throws IOException {
DataOutputStream compressedStream = new DataOutputStream(
new IOUtils.NullOutputStream()); // Create streams
// Storing them so we can close them
final IOUtils.NullOutputStream nullOutputStream = new IOUtils.NullOutputStream();
final DataOutputStream compressedStream = new DataOutputStream(nullOutputStream);
OutputStream compressingStream = null;
try {
if (compressor != null) { if (compressor != null) {
compressor.reset(); compressor.reset();
} }
OutputStream compressingStream = null;
try { compressingStream = algo.createCompressionStream(compressedStream, compressor, 0);
compressingStream = algo.createCompressionStream(
compressedStream, compressor, 0);
compressingStream.write(inputBuffer, offset, length); compressingStream.write(inputBuffer, offset, length);
compressingStream.flush(); compressingStream.flush();
return compressedStream.size(); return compressedStream.size();
} finally { } finally {
nullOutputStream.close();
compressedStream.close();
if (compressingStream != null) compressingStream.close(); if (compressingStream != null) compressingStream.close();
} }
} }

View File

@ -52,8 +52,10 @@ public class HFileBlockDefaultDecodingContext implements
@Override @Override
public void prepareDecoding(int onDiskSizeWithoutHeader, int uncompressedSizeWithoutHeader, public void prepareDecoding(int onDiskSizeWithoutHeader, int uncompressedSizeWithoutHeader,
ByteBuff blockBufferWithoutHeader, ByteBuff onDiskBlock) throws IOException { ByteBuff blockBufferWithoutHeader, ByteBuff onDiskBlock) throws IOException {
InputStream in = new DataInputStream(new ByteBuffInputStream(onDiskBlock)); final ByteBuffInputStream byteBuffInputStream = new ByteBuffInputStream(onDiskBlock);
InputStream dataInputStream = new DataInputStream(byteBuffInputStream);
try {
Encryption.Context cryptoContext = fileContext.getEncryptionContext(); Encryption.Context cryptoContext = fileContext.getEncryptionContext();
if (cryptoContext != Encryption.Context.NONE) { if (cryptoContext != Encryption.Context.NONE) {
@ -70,16 +72,16 @@ public class HFileBlockDefaultDecodingContext implements
// | encrypted block data ... | // | encrypted block data ... |
// +--------------------------+ // +--------------------------+
int ivLength = in.read(); int ivLength = dataInputStream.read();
if (ivLength > 0) { if (ivLength > 0) {
byte[] iv = new byte[ivLength]; byte[] iv = new byte[ivLength];
IOUtils.readFully(in, iv); IOUtils.readFully(dataInputStream, iv);
decryptor.setIv(iv); decryptor.setIv(iv);
// All encrypted blocks will have a nonzero IV length. If we see an IV // All encrypted blocks will have a nonzero IV length. If we see an IV
// length of zero, this means the encoding context had 0 bytes of // length of zero, this means the encoding context had 0 bytes of
// plaintext to encode. // plaintext to encode.
decryptor.reset(); decryptor.reset();
in = decryptor.createDecryptionStream(in); dataInputStream = decryptor.createDecryptionStream(dataInputStream);
} }
onDiskSizeWithoutHeader -= Bytes.SIZEOF_BYTE + ivLength; onDiskSizeWithoutHeader -= Bytes.SIZEOF_BYTE + ivLength;
} }
@ -88,12 +90,16 @@ public class HFileBlockDefaultDecodingContext implements
assert blockBufferWithoutHeader.hasArray(); assert blockBufferWithoutHeader.hasArray();
if (compression != Compression.Algorithm.NONE) { if (compression != Compression.Algorithm.NONE) {
Compression.decompress(blockBufferWithoutHeader.array(), Compression.decompress(blockBufferWithoutHeader.array(),
blockBufferWithoutHeader.arrayOffset(), in, onDiskSizeWithoutHeader, blockBufferWithoutHeader.arrayOffset(), dataInputStream, onDiskSizeWithoutHeader,
uncompressedSizeWithoutHeader, compression); uncompressedSizeWithoutHeader, compression);
} else { } else {
IOUtils.readFully(in, blockBufferWithoutHeader.array(), IOUtils.readFully(dataInputStream, blockBufferWithoutHeader.array(),
blockBufferWithoutHeader.arrayOffset(), onDiskSizeWithoutHeader); blockBufferWithoutHeader.arrayOffset(), onDiskSizeWithoutHeader);
} }
} finally {
byteBuffInputStream.close();
dataInputStream.close();
}
} }
@Override @Override

View File

@ -200,9 +200,11 @@ public class SingleByteBuff extends ByteBuff {
// create a copy. // create a copy.
ObjectIntPair<ByteBuffer> pair = new ObjectIntPair<ByteBuffer>(); ObjectIntPair<ByteBuffer> pair = new ObjectIntPair<ByteBuffer>();
src.asSubByteBuffer(srcOffset, length, pair); src.asSubByteBuffer(srcOffset, length, pair);
if (pair.getFirst() != null) {
ByteBufferUtils.copyFromBufferToBuffer(pair.getFirst(), this.buf, pair.getSecond(), offset, ByteBufferUtils.copyFromBufferToBuffer(pair.getFirst(), this.buf, pair.getSecond(), offset,
length); length);
} }
}
return this; return this;
} }

View File

@ -1092,7 +1092,9 @@ public class Base64 {
*/ */
public static byte[] decodeFromFile(String filename) { public static byte[] decodeFromFile(String filename) {
byte[] decodedData = null; byte[] decodedData = null;
Base64InputStream bis = null; BufferedInputStream bufferedInputStream = null;
FileInputStream fileInputStream = null;
Base64InputStream base64InputStream = null;
try { try {
File file = new File(filename); File file = new File(filename);
byte[] buffer; byte[] buffer;
@ -1107,14 +1109,14 @@ public class Base64 {
buffer = new byte[(int) file.length()]; buffer = new byte[(int) file.length()];
// Open a stream // Open a stream
fileInputStream = new FileInputStream(file);
bis = new Base64InputStream(new BufferedInputStream( bufferedInputStream = new BufferedInputStream(fileInputStream);
new FileInputStream(file)), DECODE); base64InputStream = new Base64InputStream(bufferedInputStream, DECODE);
// Read until done // Read until done
int length = 0; int length = 0;
for (int numBytes; (numBytes = bis.read(buffer, length, 4096)) >= 0; ) { for (int numBytes; (numBytes = base64InputStream.read(buffer, length, 4096)) >= 0; ) {
length += numBytes; length += numBytes;
} }
@ -1127,9 +1129,23 @@ public class Base64 {
LOG.error("Error decoding from file " + filename, e); LOG.error("Error decoding from file " + filename, e);
} finally { } finally {
if (bis != null) { if (fileInputStream != null) {
try { try {
bis.close(); fileInputStream.close();
} catch (Exception e) {
LOG.error("error closing FileInputStream", e);
}
}
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (Exception e) {
LOG.error("error closing BufferedInputStream", e);
}
}
if (base64InputStream != null) {
try {
base64InputStream.close();
} catch (Exception e) { } catch (Exception e) {
LOG.error("error closing Base64InputStream", e); LOG.error("error closing Base64InputStream", e);
} }
@ -1149,7 +1165,10 @@ public class Base64 {
*/ */
public static String encodeFromFile(String filename) { public static String encodeFromFile(String filename) {
String encodedData = null; String encodedData = null;
Base64InputStream bis = null; FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
Base64InputStream base64InputStream = null;
try { try {
File file = new File(filename); File file = new File(filename);
@ -1159,12 +1178,13 @@ public class Base64 {
// Open a stream // Open a stream
bis = new Base64InputStream(new BufferedInputStream( fileInputStream = new FileInputStream(file);
new FileInputStream(file)), ENCODE); bufferedInputStream = new BufferedInputStream(fileInputStream);
base64InputStream = new Base64InputStream(bufferedInputStream, ENCODE);
// Read until done // Read until done
int length = 0; int length = 0;
for (int numBytes; (numBytes = bis.read(buffer, length, 4096)) >= 0; ) { for (int numBytes; (numBytes = base64InputStream.read(buffer, length, 4096)) >= 0; ) {
length += numBytes; length += numBytes;
} }
@ -1176,9 +1196,24 @@ public class Base64 {
LOG.error("Error encoding from file " + filename, e); LOG.error("Error encoding from file " + filename, e);
} finally { } finally {
if (bis != null) { // Can't leak exceptions but still need to clean things up.
if (fileInputStream != null) {
try { try {
bis.close(); fileInputStream.close();
} catch (Exception e) {
LOG.error("error closing FileInputStream", e);
}
}
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (Exception e) {
LOG.error("error closing BufferedInputStream", e);
}
}
if (base64InputStream != null) {
try {
base64InputStream.close();
} catch (Exception e) { } catch (Exception e) {
LOG.error("error closing Base64InputStream", e); LOG.error("error closing Base64InputStream", e);
} }

View File

@ -117,7 +117,7 @@ public class ClassSize {
static { static {
final String version = System.getProperty("java.version"); final String version = System.getProperty("java.version");
// Verify String looks like this: 1.6.0_29 // Verify String looks like this: 1.6.0_29
if (!version.matches("\\d\\.\\d\\..*")) { if (version == null || !version.matches("\\d\\.\\d\\..*")) {
throw new RuntimeException("Unexpected version format: " + version); throw new RuntimeException("Unexpected version format: " + version);
} }
// Convert char to int // Convert char to int
@ -331,7 +331,8 @@ public class ClassSize {
* know this too. * know this too.
*/ */
public static boolean is32BitJVM() { public static boolean is32BitJVM() {
return System.getProperty("sun.arch.data.model").equals("32"); final String model = System.getProperty("sun.arch.data.model");
return model != null && model.equals("32");
} }
} }

View File

@ -179,6 +179,7 @@ public class DynamicClassLoader extends ClassLoaderBase {
private synchronized void loadNewJars() { private synchronized void loadNewJars() {
// Refresh local jar file lists // Refresh local jar file lists
if (localDir != null) {
for (File file : localDir.listFiles()) { for (File file : localDir.listFiles()) {
String fileName = file.getName(); String fileName = file.getName();
if (jarModifiedTime.containsKey(fileName)) { if (jarModifiedTime.containsKey(fileName)) {
@ -195,6 +196,7 @@ public class DynamicClassLoader extends ClassLoaderBase {
} }
} }
} }
}
// Check remote files // Check remote files
FileStatus[] statuses = null; FileStatus[] statuses = null;

View File

@ -45,13 +45,19 @@ public class JVM {
private OperatingSystemMXBean osMbean; private OperatingSystemMXBean osMbean;
private static final boolean ibmvendor = private static final boolean ibmvendor =
System.getProperty("java.vendor") != null &&
System.getProperty("java.vendor").contains("IBM"); System.getProperty("java.vendor").contains("IBM");
private static final boolean windows = private static final boolean windows =
System.getProperty("os.name") != null &&
System.getProperty("os.name").startsWith("Windows"); System.getProperty("os.name").startsWith("Windows");
private static final boolean linux = private static final boolean linux =
System.getProperty("os.name") != null &&
System.getProperty("os.name").startsWith("Linux"); System.getProperty("os.name").startsWith("Linux");
private static final boolean amd64 =
System.getProperty("os.arch") != null &&
System.getProperty("os.arch").contains("amd64");
private static final String JVMVersion = System.getProperty("java.version"); private static final String JVMVersion = System.getProperty("java.version");
private static final boolean amd64 = System.getProperty("os.arch").contains("amd64");
/** /**
* Constructor. Get the running Operating System instance * Constructor. Get the running Operating System instance
@ -83,6 +89,7 @@ public class JVM {
/** /**
* Check if the arch is amd64; * Check if the arch is amd64;
*
* @return whether this is amd64 or not. * @return whether this is amd64 or not.
*/ */
public static boolean isAmd64() { public static boolean isAmd64() {
@ -101,7 +108,9 @@ public class JVM {
/** /**
* Load the implementation of UnixOperatingSystemMXBean for Oracle jvm * Load the implementation of UnixOperatingSystemMXBean for Oracle jvm
* and runs the desired method. * and runs the desired method.
*
* @param mBeanMethodName : method to run from the interface UnixOperatingSystemMXBean * @param mBeanMethodName : method to run from the interface UnixOperatingSystemMXBean
*
* @return the method result * @return the method result
*/ */
private Long runUnixMXBeanMethod(String mBeanMethodName) { private Long runUnixMXBeanMethod(String mBeanMethodName) {
@ -112,12 +121,11 @@ public class JVM {
try { try {
classRef = Class.forName("com.sun.management.UnixOperatingSystemMXBean"); classRef = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
if (classRef.isInstance(osMbean)) { if (classRef.isInstance(osMbean)) {
mBeanMethod = classRef.getMethod(mBeanMethodName, new Class[0]); mBeanMethod = classRef.getMethod(mBeanMethodName);
unixos = classRef.cast(osMbean); unixos = classRef.cast(osMbean);
return (Long) mBeanMethod.invoke(unixos); return (Long) mBeanMethod.invoke(unixos);
} }
} } catch (Exception e) {
catch(Exception e) {
LOG.warn("Not able to load class or method for" + LOG.warn("Not able to load class or method for" +
" com.sun.management.UnixOperatingSystemMXBean.", e); " com.sun.management.UnixOperatingSystemMXBean.", e);
} }
@ -128,18 +136,19 @@ public class JVM {
* Get the number of opened filed descriptor for the runtime jvm. * Get the number of opened filed descriptor for the runtime jvm.
* If Oracle java, it will use the com.sun.management interfaces. * If Oracle java, it will use the com.sun.management interfaces.
* Otherwise, this methods implements it (linux only). * Otherwise, this methods implements it (linux only).
*
* @return number of open file descriptors for the jvm * @return number of open file descriptors for the jvm
*/ */
public long getOpenFileDescriptorCount() { public long getOpenFileDescriptorCount() {
Long ofdc; Long ofdc;
if (!ibmvendor) { if (!ibmvendor) {
ofdc = runUnixMXBeanMethod("getOpenFileDescriptorCount"); ofdc = runUnixMXBeanMethod("getOpenFileDescriptorCount");
return (ofdc != null ? ofdc.longValue () : -1); return (ofdc != null ? ofdc : -1);
} }
InputStream in = null; InputStream inputStream = null;
BufferedReader output = null; InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try { try {
//need to get the PID number of the process first //need to get the PID number of the process first
RuntimeMXBean rtmbean = ManagementFactory.getRuntimeMXBean(); RuntimeMXBean rtmbean = ManagementFactory.getRuntimeMXBean();
@ -150,24 +159,33 @@ public class JVM {
Process p = Runtime.getRuntime().exec( Process p = Runtime.getRuntime().exec(
new String[]{"bash", "-c", new String[]{"bash", "-c",
"ls /proc/" + pidhost[0] + "/fdinfo | wc -l"}); "ls /proc/" + pidhost[0] + "/fdinfo | wc -l"});
in = p.getInputStream(); inputStream = p.getInputStream();
output = new BufferedReader(new InputStreamReader(in)); inputStreamReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputStreamReader);
String openFileDesCount; String openFileDesCount;
if ((openFileDesCount = output.readLine()) != null) if ((openFileDesCount = bufferedReader.readLine()) != null) {
return Long.parseLong(openFileDesCount); return Long.parseLong(openFileDesCount);
}
} catch (IOException ie) { } catch (IOException ie) {
LOG.warn("Not able to get the number of open file descriptors", ie); LOG.warn("Not able to get the number of open file descriptors", ie);
} finally { } finally {
if (output != null) { if (bufferedReader != null) {
try { try {
output.close(); bufferedReader.close();
} catch (IOException e) { } catch (IOException e) {
LOG.warn("Not able to close the InputStream", e); LOG.warn("Not able to close the BufferedReader", e);
} }
} }
if (in != null){ if (inputStreamReader != null) {
try { try {
in.close(); inputStreamReader.close();
} catch (IOException e) {
LOG.warn("Not able to close the InputStreamReader", e);
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) { } catch (IOException e) {
LOG.warn("Not able to close the InputStream", e); LOG.warn("Not able to close the InputStream", e);
} }
@ -210,21 +228,40 @@ public class JVM {
return 0; return 0;
} }
BufferedReader input = null; InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try { try {
int count = 0; int count = 0;
Process p = Runtime.getRuntime().exec("ps -e"); Process p = Runtime.getRuntime().exec("ps -e");
input = new BufferedReader(new InputStreamReader(p.getInputStream())); inputStream = p.getInputStream();
while (input.readLine() != null) { inputStreamReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputStreamReader);
while (bufferedReader.readLine() != null) {
count++; count++;
} }
return count - 1; // -1 because there is a headline return count - 1; // -1 because there is a headline
} catch (IOException e) { } catch (IOException e) {
return -1; return -1;
} finally { } finally {
if (input != null){ if (bufferedReader != null) {
try { try {
input.close(); bufferedReader.close();
} catch (IOException e) {
LOG.warn("Not able to close the BufferedReader", e);
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
LOG.warn("Not able to close the InputStreamReader", e);
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) { } catch (IOException e) {
LOG.warn("Not able to close the InputStream", e); LOG.warn("Not able to close the InputStream", e);
} }
@ -236,13 +273,14 @@ public class JVM {
* Get the number of the maximum file descriptors the system can use. * Get the number of the maximum file descriptors the system can use.
* If Oracle java, it will use the com.sun.management interfaces. * If Oracle java, it will use the com.sun.management interfaces.
* Otherwise, this methods implements it (linux only). * Otherwise, this methods implements it (linux only).
*
* @return max number of file descriptors the operating system can use. * @return max number of file descriptors the operating system can use.
*/ */
public long getMaxFileDescriptorCount() { public long getMaxFileDescriptorCount() {
Long mfdc; Long mfdc;
if (!ibmvendor) { if (!ibmvendor) {
mfdc = runUnixMXBeanMethod("getMaxFileDescriptorCount"); mfdc = runUnixMXBeanMethod("getMaxFileDescriptorCount");
return (mfdc != null ? mfdc.longValue () : -1); return (mfdc != null ? mfdc : -1);
} }
InputStream in = null; InputStream in = null;
BufferedReader output = null; BufferedReader output = null;
@ -252,7 +290,9 @@ public class JVM {
in = p.getInputStream(); in = p.getInputStream();
output = new BufferedReader(new InputStreamReader(in)); output = new BufferedReader(new InputStreamReader(in));
String maxFileDesCount; String maxFileDesCount;
if ((maxFileDesCount = output.readLine()) != null) return Long.parseLong(maxFileDesCount); if ((maxFileDesCount = output.readLine()) != null) {
return Long.parseLong(maxFileDesCount);
}
} catch (IOException ie) { } catch (IOException ie) {
LOG.warn("Not able to get the max number of file descriptors", ie); LOG.warn("Not able to get the max number of file descriptors", ie);
} finally { } finally {

View File

@ -107,7 +107,10 @@ public class LoadTestKVGenerator {
private static byte[] getValueForRowColumn(int dataSize, byte[]... seedStrings) { private static byte[] getValueForRowColumn(int dataSize, byte[]... seedStrings) {
long seed = dataSize; long seed = dataSize;
for (byte[] str : seedStrings) { for (byte[] str : seedStrings) {
seed += Bytes.toString(str).hashCode(); final String bytesString = Bytes.toString(str);
if (bytesString != null) {
seed += bytesString.hashCode();
}
} }
Random seededRandom = new Random(seed); Random seededRandom = new Random(seed);
byte[] randomBytes = new byte[dataSize]; byte[] randomBytes = new byte[dataSize];