Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a17110e2 authored by Charles Chen's avatar Charles Chen Committed by charleschen
Browse files

Fix NPE when pfd is not present

When a pfd is returned from VIS, we should check if pfd is null or not
to avod NPE. If the pfd is null, we need to throw a IOException as the
javadoc alludes.

Bug: 315207356
Test: atest CtsVoiceInteractionTestCases
Flag: N/A
Change-Id: Ibaed9970c1ea841432361254468ac87c703a84f2
parent 1fba603b
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -338,16 +338,24 @@ public abstract class VisualQueryDetectionService extends Service

    /**
     * Overrides {@link Context#openFileInput} to read files with the given file names under the
     * internal app storage of the {@link VoiceInteractionService}, i.e., only files stored in
     * {@link Context#getFilesDir()} can be opened.
     * internal app storage of the {@link VoiceInteractionService}, i.e., the input file path would
     * be added with {@link Context#getFilesDir()} as prefix.
     *
     * @param filename Relative path of a file under {@link Context#getFilesDir()}.
     * @throws FileNotFoundException if the file does not exist or cannot be open.
     */
    @Override
    public @Nullable FileInputStream openFileInput(@NonNull String filename) throws
    public @NonNull FileInputStream openFileInput(@NonNull String filename) throws
            FileNotFoundException {
        try {
            AndroidFuture<ParcelFileDescriptor> future = new AndroidFuture<>();
            assert mDetectorSessionStorageService != null;
            mDetectorSessionStorageService.openFile(filename, future);
            ParcelFileDescriptor pfd = future.get();
            if (pfd == null) {
                throw new FileNotFoundException(
                        "File does not exist. Unable to open " + filename + ".");
            }
            return new FileInputStream(pfd.getFileDescriptor());
        } catch (RemoteException | ExecutionException | InterruptedException e) {
            Log.w(TAG, "Cannot open file due to remote service failure");
+2 −2
Original line number Diff line number Diff line
@@ -447,12 +447,12 @@ public class VisualQueryDetector {
        public void onOpenFile(String filename, AndroidFuture future) throws RemoteException {
            Slog.v(TAG, "BinderCallback#onOpenFile " + filename);
            Binder.withCleanCallingIdentity(() -> mExecutor.execute(() -> {
                Slog.v(TAG, "onOpenFile: " + filename);
                Slog.v(TAG, "onOpenFile: " + filename + "under internal app storage.");
                File f = new File(mContext.getFilesDir(), filename);
                ParcelFileDescriptor pfd = null;
                try {
                    Slog.d(TAG, "opened a file with ParcelFileDescriptor.");
                    pfd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
                    Slog.d(TAG, "Successfully opened a file with ParcelFileDescriptor.");
                } catch (FileNotFoundException e) {
                    Slog.e(TAG, "Cannot open file. No ParcelFileDescriptor returned.");
                } finally {