Permalink
Browse files

Ensure all smali files are closed after the writing is complete

This makes sure that all smali files are closed after writing to them by
surrounding the writer code with a try/finally block that closes the output
stream once the writing has completed or when an exception occurs.
  • Loading branch information...
agorski3 authored and JesusFreke committed Oct 10, 2016
1 parent e75f2b2 commit 43669ecc6e3f5d95ef1653fb7ba0c0445040fd3c
Showing with 44 additions and 41 deletions.
  1. +44 −41 smali/src/main/java/org/jf/smali/Smali.java
@@ -147,59 +147,62 @@ private static void getSmaliFilesInDir(@Nonnull File dir, @Nonnull Set<File> sma
private static boolean assembleSmaliFile(File smaliFile, DexBuilder dexBuilder, SmaliOptions options)
throws Exception {
- CommonTokenStream tokens;
-
- LexerErrorInterface lexer;
-
- FileInputStream fis = new FileInputStream(smaliFile);
- InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
-
- lexer = new smaliFlexLexer(reader);
- ((smaliFlexLexer)lexer).setSourceFile(smaliFile);
- tokens = new CommonTokenStream((TokenSource)lexer);
-
- if (options.printTokens) {
- tokens.getTokens();
+ FileInputStream fis = null;
+ try {
+ fis = new FileInputStream(smaliFile);
+ InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
+
+ LexerErrorInterface lexer = new smaliFlexLexer(reader);
+ ((smaliFlexLexer)lexer).setSourceFile(smaliFile);
+ CommonTokenStream tokens = new CommonTokenStream((TokenSource)lexer);
+
+ if (options.printTokens) {
+ tokens.getTokens();
+
+ for (int i=0; i<tokens.size(); i++) {
+ Token token = tokens.get(i);
+ if (token.getChannel() == smaliParser.HIDDEN) {
+ continue;
+ }
- for (int i=0; i<tokens.size(); i++) {
- Token token = tokens.get(i);
- if (token.getChannel() == smaliParser.HIDDEN) {
- continue;
+ System.out.println(smaliParser.tokenNames[token.getType()] + ": " + token.getText());
}
- System.out.println(smaliParser.tokenNames[token.getType()] + ": " + token.getText());
+ System.out.flush();
}
- System.out.flush();
- }
-
- smaliParser parser = new smaliParser(tokens);
- parser.setVerboseErrors(options.verboseErrors);
- parser.setAllowOdex(options.allowOdexOpcodes);
- parser.setApiLevel(options.apiLevel);
+ smaliParser parser = new smaliParser(tokens);
+ parser.setVerboseErrors(options.verboseErrors);
+ parser.setAllowOdex(options.allowOdexOpcodes);
+ parser.setApiLevel(options.apiLevel);
- smaliParser.smali_file_return result = parser.smali_file();
+ smaliParser.smali_file_return result = parser.smali_file();
- if (parser.getNumberOfSyntaxErrors() > 0 || lexer.getNumberOfSyntaxErrors() > 0) {
- return false;
- }
+ if (parser.getNumberOfSyntaxErrors() > 0 || lexer.getNumberOfSyntaxErrors() > 0) {
+ return false;
+ }
- CommonTree t = result.getTree();
+ CommonTree t = result.getTree();
- CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
- treeStream.setTokenStream(tokens);
+ CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
+ treeStream.setTokenStream(tokens);
- if (options.printTokens) {
- System.out.println(t.toStringTree());
- }
+ if (options.printTokens) {
+ System.out.println(t.toStringTree());
+ }
- smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
- dexGen.setApiLevel(options.apiLevel);
+ smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
+ dexGen.setApiLevel(options.apiLevel);
- dexGen.setVerboseErrors(options.verboseErrors);
- dexGen.setDexBuilder(dexBuilder);
- dexGen.smali_file();
+ dexGen.setVerboseErrors(options.verboseErrors);
+ dexGen.setDexBuilder(dexBuilder);
+ dexGen.smali_file();
- return dexGen.getNumberOfSyntaxErrors() == 0;
+ return dexGen.getNumberOfSyntaxErrors() == 0;
+ } finally {
+ if (fis != null) {
+ fis.close();
+ }
+ }
}
}

0 comments on commit 43669ec

Please sign in to comment.