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

Commit 0c91451b authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Catch more invalid file modes.

Bug: 117440225
Test: atest android.os.FileUtilsTest
Change-Id: Id5e8c0869182b6391994dd9266a455dd3152e653
parent 47bf6668
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -1198,6 +1198,19 @@ public class FileUtils {

    /** {@hide} */
    public static int translateModeStringToPosix(String mode) {
        // Sanity check for invalid chars
        for (int i = 0; i < mode.length(); i++) {
            switch (mode.charAt(i)) {
                case 'r':
                case 'w':
                case 't':
                case 'a':
                    break;
                default:
                    throw new IllegalArgumentException("Bad mode: " + mode);
            }
        }

        int res = 0;
        if (mode.startsWith("rw")) {
            res |= O_RDWR | O_CREAT;
+15 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.content.Context;
import android.os.FileUtils.MemoryPipe;
@@ -510,6 +511,20 @@ public class FileUtilsTest {
                MODE_WRITE_ONLY | MODE_CREATE | MODE_APPEND);
    }

    @Test
    public void testTranslateMode_Invalid() throws Exception {
        try {
            translateModeStringToPosix("rwx");
            fail();
        } catch (IllegalArgumentException expected) {
        }
        try {
            translateModeStringToPosix("");
            fail();
        } catch (IllegalArgumentException expected) {
        }
    }

    private static void assertTranslate(String string, int posix, int pfd) {
        assertEquals(posix, translateModeStringToPosix(string));
        assertEquals(string, translateModePosixToString(posix));