{"record":{"id":"7f8d61dd549f9109","repo":"MuntashirAkon/AppManager","slug":"bad-mode-mode","errorCode":null,"errorMessage":"Bad mode: + mode","messagePattern":"Bad mode: \\+ mode","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/io/fs/VirtualFileSystem.java","lineNumber":1003,"sourceCode":"        if (!targetNode.isFile()) {\n            throw new IOException(path + \" is not a file.\");\n        }\n        return new FileOutputStream(getCachedFile(targetNode, true), append);\n    }\n\n    @NonNull\n    public FileChannel openChannel(String path, int mode) throws IOException {\n        boolean read = false;\n        boolean write = false;\n        if ((mode & MODE_READ_WRITE) != 0) {\n            read = true;\n            write = true;\n        } else if ((mode & MODE_READ_ONLY) != 0) {\n            read = true;\n        } else if ((mode & MODE_WRITE_ONLY) != 0) {\n            write = true;\n        } else {\n            throw new IllegalArgumentException(\"Bad mode: \" + mode);\n        }\n\n        if (read && write && !checkAccess(path, OsConstants.R_OK | OsConstants.W_OK)) {\n            throw new IOException(path + \" cannot be opened for both reading and writing.\");\n        } else if (read && !checkAccess(path, OsConstants.R_OK)) {\n            throw new IOException(path + \" cannot be opened for both reading.\");\n        } else if (write && !checkAccess(path, OsConstants.W_OK)) {\n            throw new IOException(path + \" cannot be opened for both writing.\");\n        }\n        Node<?> targetNode = getNode(path);\n        if (targetNode == null) {\n            throw new FileNotFoundException(path + \" does not exist.\");\n        }\n        if (!targetNode.isFile()) {\n            throw new IOException(path + \" is not a file.\");\n        }\n        return FileSystemManager.getLocal().openChannel(getCachedFile(targetNode, write), mode);\n    }","sourceCodeStart":985,"sourceCodeEnd":1021,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/io/fs/VirtualFileSystem.java#L985-L1021","documentation":"VirtualFileSystem.openChannel(path, mode) throws IllegalArgumentException(\"Bad mode: \" + mode) when the mode bitmask contains none of MODE_READ_WRITE, MODE_READ_ONLY, or MODE_WRITE_ONLY. The library only accepts these documented open-mode flags.","triggerScenarios":"Passing 0 as mode; passing an undefined/custom bit (e.g. java.nio FileChannel mode constants like READ/WRITE instead of the VFS's MODE_* constants); accidentally clearing all bits with a bad mask computation.","commonSituations":"Porting code from java.nio.channels.FileChannel (which uses StandardOpenOption) and passing wrong constants; typo in flag composition; mode built from a config value of 0 or a string parsed incorrectly.","solutions":["Pass one of the class's MODE_READ_WRITE / MODE_READ_ONLY / MODE_WRITE_ONLY constants","If translating from StandardOpenOption, map READ+WRITE->MODE_READ_WRITE, READ->MODE_READ_ONLY, WRITE/APPEND->MODE_WRITE_ONLY","Log/validate the mode value at the call site to catch computed masks that end up 0"],"exampleFix":"// before\nFileChannel ch = vfs.openChannel(path, 0);\n// after\nFileChannel ch = vfs.openChannel(path, VirtualFileSystem.MODE_READ_WRITE);","handlingStrategy":"validation","validationCode":"int mode = VirtualFileSystem.MODE_READ_ONLY; // one of MODE_READ_WRITE|MODE_READ_ONLY|MODE_WRITE_ONLY\nif (mode != VirtualFileSystem.MODE_READ_WRITE && mode != VirtualFileSystem.MODE_READ_ONLY && mode != VirtualFileSystem.MODE_WRITE_ONLY)\n    throw new IllegalArgumentException(\"Unsupported mode: \" + mode);","typeGuard":"boolean isValidMode(int mode) {\n    return mode == VirtualFileSystem.MODE_READ_WRITE\n        || mode == VirtualFileSystem.MODE_READ_ONLY\n        || mode == VirtualFileSystem.MODE_WRITE_ONLY;\n}","tryCatchPattern":"try {\n    FileChannel ch = vfs.openChannel(path, mode);\n} catch (IllegalArgumentException e) {\n    // fix mode constant at call site; log the bad value\n}","preventionTips":["Only use the class's MODE_* constants, never java.nio StandardOpenOption values","Centralize mode selection in one mapping helper","Never compute mode masks that can evaluate to 0"],"tags":["filesystem","virtual-fs","bad-argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}