Skip to content

Commit

Permalink
PARQUET-825: Static analyzer findings (NPEs, resource leaks)
Browse files Browse the repository at this point in the history
Some trivial code fixes based on findings on static code analyzer tools (Sonar, Fortify)
@piyushnarang: Sorry, renaming the branch caused the closing of the original PR...

Author: Gabor Szadovszky <[email protected]>
Author: Gabor Szadovszky <[email protected]>

Closes apache#399 from gszadovszky/PARQUET-825 and squashes the following commits:

68a4764 [Gabor Szadovszky] PARQUET-825 - Static analyzer findings (NPEs, resource leaks)
a689c1c [Gabor Szadovszky] Code fixes related to null checks, exception handling and closing streams
  • Loading branch information
Gabor Szadovszky authored and julienledem committed Jan 26, 2017
1 parent 89e0607 commit f68dbc3
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public boolean equals(Object obj) {
// }

public boolean equals(Case other) {
return startLevel == other.startLevel
return other != null
&& startLevel == other.startLevel
&& depth == other.depth
&& nextLevel == other.nextLevel
&& nextState == other.nextState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String get() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
return getClass() == o.getClass() && s.equals(((Atom) o).s);
return o != null && getClass() == o.getClass() && s.equals(((Atom) o).s);
}

@Override
Expand Down Expand Up @@ -97,7 +97,7 @@ public List<GlobNode> getChildren() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
return getClass() == o.getClass() && children.equals(((OneOf) o).children);
return o != null && getClass() == o.getClass() && children.equals(((OneOf) o).children);
}

@Override
Expand Down Expand Up @@ -136,7 +136,7 @@ public List<GlobNode> getChildren() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
return getClass() == o.getClass() && children.equals(((OneOf) o).children);
return o != null && getClass() == o.getClass() && children.equals(((OneOf) o).children);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,42 +65,42 @@ private static void generateScheme(boolean isLong, boolean msbFirst,
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileWriter fw = new FileWriter(file);
fw.append("package org.apache.parquet.column.values.bitpacking;\n");
fw.append("import java.nio.ByteBuffer;\n");
fw.append("\n");
fw.append("/**\n");
if (msbFirst) {
fw.append(" * Packs from the Most Significant Bit first\n");
} else {
fw.append(" * Packs from the Least Significant Bit first\n");
}
fw.append(" * \n");
fw.append(" * @author automatically generated\n");
fw.append(" * @see ByteBasedBitPackingGenerator\n");
fw.append(" *\n");
fw.append(" */\n");
fw.append("public abstract class " + className + " {\n");
fw.append("\n");
fw.append(" private static final BytePacker" + nameSuffix + "[] packers = new BytePacker" + nameSuffix + "[" + (maxBits + 1) + "];\n");
fw.append(" static {\n");
for (int i = 0; i <= maxBits; i++) {
fw.append(" packers[" + i + "] = new Packer" + i + "();\n");
}
fw.append(" }\n");
fw.append("\n");
fw.append(" public static final BytePacker" + nameSuffix + "Factory factory = new BytePacker" + nameSuffix + "Factory() {\n");
fw.append(" public BytePacker" + nameSuffix + " newBytePacker" + nameSuffix + "(int bitWidth) {\n");
fw.append(" return packers[bitWidth];\n");
fw.append(" }\n");
fw.append(" };\n");
fw.append("\n");
for (int i = 0; i <= maxBits; i++) {
generateClass(fw, i, isLong, msbFirst);
try (FileWriter fw = new FileWriter(file)) {
fw.append("package org.apache.parquet.column.values.bitpacking;\n");
fw.append("import java.nio.ByteBuffer;\n");
fw.append("\n");
fw.append("/**\n");
if (msbFirst) {
fw.append(" * Packs from the Most Significant Bit first\n");
} else {
fw.append(" * Packs from the Least Significant Bit first\n");
}
fw.append(" * \n");
fw.append(" * @author automatically generated\n");
fw.append(" * @see ByteBasedBitPackingGenerator\n");
fw.append(" *\n");
fw.append(" */\n");
fw.append("public abstract class " + className + " {\n");
fw.append("\n");
fw.append(" private static final BytePacker" + nameSuffix + "[] packers = new BytePacker" + nameSuffix + "[" + (maxBits + 1) + "];\n");
fw.append(" static {\n");
for (int i = 0; i <= maxBits; i++) {
fw.append(" packers[" + i + "] = new Packer" + i + "();\n");
}
fw.append(" }\n");
fw.append("\n");
fw.append(" public static final BytePacker" + nameSuffix + "Factory factory = new BytePacker" + nameSuffix + "Factory() {\n");
fw.append(" public BytePacker" + nameSuffix + " newBytePacker" + nameSuffix + "(int bitWidth) {\n");
fw.append(" return packers[bitWidth];\n");
fw.append(" }\n");
fw.append(" };\n");
fw.append("\n");
for (int i = 0; i <= maxBits; i++) {
generateClass(fw, i, isLong, msbFirst);
fw.append("\n");
}
fw.append("}\n");
}
fw.append("}\n");
fw.close();
}

private static void generateClass(FileWriter fw, int bitWidth, boolean isLong, boolean msbFirst) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,45 +56,45 @@ private static void generateScheme(String className, boolean msbFirst, String ba
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileWriter fw = new FileWriter(file);
fw.append("package org.apache.parquet.column.values.bitpacking;\n");
fw.append("\n");
fw.append("/**\n");
fw.append(" * Based on the original implementation at at https://github.com/lemire/JavaFastPFOR/blob/master/src/integercompression/BitPacking.java\n");
fw.append(" * Which is released under the\n");
fw.append(" * Apache License Version 2.0 http://www.apache.org/licenses/.\n");
fw.append(" * By Daniel Lemire, http://lemire.me/en/\n");
fw.append(" * \n");
fw.append(" * Scheme designed by D. Lemire\n");
if (msbFirst) {
fw.append(" * Adapted to pack from the Most Significant Bit first\n");
}
fw.append(" * \n");
fw.append(" * @author automatically generated\n");
fw.append(" * @see IntBasedBitPackingGenerator\n");
fw.append(" *\n");
fw.append(" */\n");
fw.append("abstract class " + className + " {\n");
fw.append("\n");
fw.append(" private static final IntPacker[] packers = new IntPacker[32];\n");
fw.append(" static {\n");
for (int i = 0; i < 32; i++) {
fw.append(" packers[" + i + "] = new Packer" + i + "();\n");
}
fw.append(" }\n");
fw.append("\n");
fw.append(" public static final IntPackerFactory factory = new IntPackerFactory() {\n");
fw.append(" public IntPacker newIntPacker(int bitWidth) {\n");
fw.append(" return packers[bitWidth];\n");
fw.append(" }\n");
fw.append(" };\n");
fw.append("\n");
for (int i = 0; i < 32; i++) {
generateClass(fw, i, msbFirst);
try (FileWriter fw = new FileWriter(file)) {
fw.append("package org.apache.parquet.column.values.bitpacking;\n");
fw.append("\n");
fw.append("/**\n");
fw.append(" * Based on the original implementation at at https://github.com/lemire/JavaFastPFOR/blob/master/src/integercompression/BitPacking.java\n");
fw.append(" * Which is released under the\n");
fw.append(" * Apache License Version 2.0 http://www.apache.org/licenses/.\n");
fw.append(" * By Daniel Lemire, http://lemire.me/en/\n");
fw.append(" * \n");
fw.append(" * Scheme designed by D. Lemire\n");
if (msbFirst) {
fw.append(" * Adapted to pack from the Most Significant Bit first\n");
}
fw.append(" * \n");
fw.append(" * @author automatically generated\n");
fw.append(" * @see IntBasedBitPackingGenerator\n");
fw.append(" *\n");
fw.append(" */\n");
fw.append("abstract class " + className + " {\n");
fw.append("\n");
fw.append(" private static final IntPacker[] packers = new IntPacker[32];\n");
fw.append(" static {\n");
for (int i = 0; i < 32; i++) {
fw.append(" packers[" + i + "] = new Packer" + i + "();\n");
}
fw.append(" }\n");
fw.append("\n");
fw.append(" public static final IntPackerFactory factory = new IntPackerFactory() {\n");
fw.append(" public IntPacker newIntPacker(int bitWidth) {\n");
fw.append(" return packers[bitWidth];\n");
fw.append(" }\n");
fw.append(" };\n");
fw.append("\n");
for (int i = 0; i < 32; i++) {
generateClass(fw, i, msbFirst);
fw.append("\n");
}
fw.append("}\n");
}
fw.append("}\n");
fw.close();
}

private static void generateClass(FileWriter fw, int bitWidth, boolean msbFirst) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public void run() throws IOException {
throw new IOException("/parquet-version.properties not found");
}
Properties props = new Properties();
props.load(in);
try {
props.load(in);
} finally {
in.close();
}

add("package org.apache.parquet;\n" +
"\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public static String binaryToString(Binary value) {
try {
CharBuffer buffer = UTF8_DECODER.decode(value.toByteBuffer());
return buffer.toString();
} catch (Throwable th) {
} catch (Exception ex) {
}

return "<bytes...>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static Map<String,Command> allCommands() {
for (Map.Entry<String,Class<? extends Command>> entry : registry.entrySet()) {
try {
results.put(entry.getKey(), entry.getValue().newInstance());
} catch (Throwable th) {
} catch (Exception ex) {
}
}

Expand All @@ -54,7 +54,7 @@ public static Command getCommandByName(String name) {

try {
return clazz.newInstance();
} catch (Throwable th) {
} catch (Exception ex) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public enum WhiteSpaceHandler { ELIMINATE_NEWLINES, COLLAPSE_WHITESPACE }
if (columns != null && !columns.isEmpty()) {
try {
consoleWidth = Integer.parseInt(columns);
} catch (Throwable th) {
} catch (Exception ex) {
}
}

Expand All @@ -88,7 +88,7 @@ public enum WhiteSpaceHandler { ELIMINATE_NEWLINES, COLLAPSE_WHITESPACE }
try {
numColors = Integer.parseInt(colors);
if (numColors < 0) numColors = 0;
} catch (Throwable th) {
} catch (Exception exa) {
}
}

Expand Down

0 comments on commit f68dbc3

Please sign in to comment.