diff options
author | benm@chromium.org <benm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-28 22:47:24 +0000 |
---|---|---|
committer | benm@chromium.org <benm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-28 22:47:24 +0000 |
commit | e6f97e77a434808ffb46647c8d7207a1dccffaaf (patch) | |
tree | ed02ce040ecbf13a2be64403c325a0a0a419a718 /content | |
parent | f9bf323f8355ebfa008efc16900b6600be83dec2 (diff) | |
download | chromium_src-e6f97e77a434808ffb46647c8d7207a1dccffaaf.zip chromium_src-e6f97e77a434808ffb46647c8d7207a1dccffaaf.tar.gz chromium_src-e6f97e77a434808ffb46647c8d7207a1dccffaaf.tar.bz2 |
[Android] Fail fast when trying to parse a non-existant CommandLine file.
If the file passed to CommandLine.initFromFile doesn't exist or has
a 0 length, fail fast rather than waiting for FileNotFoundException
to be thrown.
BUG=b/9996857
NOTRY=true
Review URL: https://chromiumcodereview.appspot.com/20112003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@214143 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r-- | content/public/android/java/src/org/chromium/content/common/CommandLine.java | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/content/public/android/java/src/org/chromium/content/common/CommandLine.java b/content/public/android/java/src/org/chromium/content/common/CommandLine.java index 83cb348a..c147b71 100644 --- a/content/public/android/java/src/org/chromium/content/common/CommandLine.java +++ b/content/public/android/java/src/org/chromium/content/common/CommandLine.java @@ -162,16 +162,9 @@ public abstract class CommandLine { * @param file The fully qualified command line file. */ public static void initFromFile(String file) { - char[] buffer = new char[0]; - try { - // Arbitrary clamp of 8k on the amount of file we read in. - buffer = readUtf8FileFully(file, 8 * 1024); - } catch (FileNotFoundException e) { - // Ignore: having a command line file is optional. - } catch (IOException e) { - Log.w(TAG, "error reading command line file " + file + e); - } - init(tokenizeQuotedAruments(buffer)); + // Arbitrary clamp of 8k on the amount of file we read in. + char[] buffer = readUtf8FileFully(file, 8 * 1024); + init(buffer == null ? null : tokenizeQuotedAruments(buffer)); } /** @@ -257,29 +250,43 @@ public abstract class CommandLine { /** * @param fileName the file to read in. - * @param sizeLimit cap on the file size: will throw an exception if exceeded - * @return Array of chars read from the file - * @throws FileNotFoundException file does not exceed - * @throws IOException error encountered accessing the file + * @param sizeLimit cap on the file size. + * @return Array of chars read from the file, or null if the file cannot be read + * or if its length exceeds |sizeLimit|. */ - private static char[] readUtf8FileFully(String fileName, int sizeLimit) throws - FileNotFoundException, IOException { + private static char[] readUtf8FileFully(String fileName, int sizeLimit) { Reader reader = null; + File f = new File(fileName); + long fileLength = f.length(); + + if (fileLength == 0) { + return null; + } + + if (fileLength > sizeLimit) { + Log.w(TAG, "File " + fileName + " length " + fileLength + " exceeds limit " + + sizeLimit); + return null; + } + try { - File f = new File(fileName); - if (f.length() > sizeLimit) { - throw new IOException("File " + fileName + " length " + f.length() + - " exceeds limit " + sizeLimit); - } - char[] buffer = new char[(int) f.length()]; + char[] buffer = new char[(int) fileLength]; reader = new InputStreamReader(new FileInputStream(f), "UTF-8"); int charsRead = reader.read(buffer); // Debug check that we've exhausted the input stream (will fail e.g. if the // file grew after we inspected its length). assert !reader.ready(); return charsRead < buffer.length ? Arrays.copyOfRange(buffer, 0, charsRead) : buffer; + } catch (FileNotFoundException e) { + return null; + } catch (IOException e) { + return null; } finally { - if (reader != null) reader.close(); + try { + if (reader != null) reader.close(); + } catch (IOException e) { + Log.e(TAG, "Unable to close file reader.", e); + } } } |