{"record":{"id":"a36b804c13b236e1","repo":"MuntashirAkon/AppManager","slug":"bad-mode-mode-fileutils","errorCode":null,"errorMessage":"Bad mode: ${mode}","messagePattern":"Bad mode: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"libcore/io/src/main/java/io/github/muntashirakon/io/FileUtils.java","lineNumber":66,"sourceCode":"\n    static class Flag {\n        boolean read;\n        boolean write;\n        boolean create;\n        boolean truncate;\n        boolean append;\n    }\n\n    static int modeToPosix(int mode) {\n        int res;\n        if ((mode & MODE_READ_WRITE) == MODE_READ_WRITE) {\n            res = O_RDWR;\n        } else if ((mode & MODE_WRITE_ONLY) == MODE_WRITE_ONLY) {\n            res = O_WRONLY;\n        } else if ((mode & MODE_READ_ONLY) == MODE_READ_ONLY) {\n            res = O_RDONLY;\n        } else {\n            throw new IllegalArgumentException(\"Bad mode: \" + mode);\n        }\n        if ((mode & MODE_CREATE) == MODE_CREATE) {\n            res |= O_CREAT;\n        }\n        if ((mode & MODE_TRUNCATE) == MODE_TRUNCATE) {\n            res |= O_TRUNC;\n        }\n        if ((mode & MODE_APPEND) == MODE_APPEND) {\n            res |= O_APPEND;\n        }\n        return res;\n    }\n\n    @RequiresApi(api = 26)\n    static Set<OpenOption> modeToOptions(int mode) {\n        Set<OpenOption> set = new ArraySet<>();\n        if ((mode & MODE_READ_WRITE) == MODE_READ_WRITE) {\n            set.add(StandardOpenOption.READ);","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/io/src/main/java/io/github/muntashirakon/io/FileUtils.java#L48-L84","documentation":"FileUtils.modeToPosix(mode) maps FileSystemManager mode bits (MODE_READ_WRITE/MODE_WRITE_ONLY/MODE_READ_ONLY etc.) to OsConstants open() flags. If the mode bits contain no recognized access-mode combination, it throws IllegalArgumentException(\"Bad mode: \" + mode).","triggerScenarios":"Passing a mode int whose access bits are 0 or composed of flags not defined by FileSystemManager (e.g. passing 0, or raw O_* constants instead of the MODE_* constants).","commonSituations":"Passing Java NIO/POSIX constants instead of FileSystemManager.MODE_* constants; accidentally clearing access bits when combining flags; passing mode 0.","solutions":["Use the FileSystemManager.MODE_* constants: at least one of MODE_READ_ONLY, MODE_WRITE_ONLY, or MODE_READ_WRITE must be set.","Build the mode with bitwise OR of constants (e.g. MODE_CREATE | MODE_TRUNCATE | MODE_WRITE_ONLY).","Validate the mode before opening: (mode & (MODE_READ_ONLY|MODE_WRITE_ONLY|MODE_READ_WRITE)) != 0."],"exampleFix":"// before\nfs.openFile(f, 0);\n// after\nfs.openFile(f, FileSystemManager.MODE_WRITE_ONLY | FileSystemManager.MODE_CREATE);","handlingStrategy":"validation","validationCode":"static boolean validAccessMode(int mode) {\n    int acc = FileSystemManager.MODE_READ_ONLY | FileSystemManager.MODE_WRITE_ONLY | FileSystemManager.MODE_READ_WRITE;\n    return (mode & acc) != 0;\n}","typeGuard":null,"tryCatchPattern":"try {\n    fs.openFile(f, mode);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Bad mode\")) mode |= FileSystemManager.MODE_READ_ONLY;\n}","preventionTips":["Only compose modes from FileSystemManager.MODE_* constants","Assert an access bit is present before opening","Do not pass POSIX/NIO constants as mode ints"],"tags":["io","file-mode","argument-validation"],"backgroundTag":"invalid-flag-value","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}