{"record":{"id":"5b90513596a094bb","repo":"asLody/VirtualApp","slug":"invalid-userid-5b9051","errorCode":null,"errorMessage":"Invalid userId ","messagePattern":"Invalid userId ","errorType":"exception","errorClass":"java.lang.IllegalStateException","httpStatus":null,"severity":"error","filePath":"VirtualApp/lib/src/main/java/com/lody/virtual/server/vs/VirtualStorageService.java","lineNumber":91,"sourceCode":"            config.enable = enable;\n            mLayer.save();\n        }\n\n    }\n\n    @Override\n    public boolean isVirtualStorageEnable(String packageName, int userId) {\n        checkUserId(userId);\n        synchronized (mConfigs) {\n            VSConfig config = getOrCreateVSConfigLocked(packageName, userId);\n            return config.enable;\n        }\n\n    }\n\n    private void checkUserId(int userId) {\n        if (!VUserManagerService.get().exists(userId)) {\n            throw new IllegalStateException(\"Invalid userId \" + userId);\n        }\n    }\n}\n","sourceCodeStart":73,"sourceCodeEnd":95,"githubUrl":"https://github.com/asLody/VirtualApp/blob/666fefcb5d3f39cc944001c3457c38ffd6544c87/VirtualApp/lib/src/main/java/com/lody/virtual/server/vs/VirtualStorageService.java#L73-L95","documentation":"VirtualStorageService manages per-user virtual storage paths inside VirtualApp. checkUserId validates that the given userId corresponds to an existing virtual user (per VUserManagerService.exists). If the user does not exist, it throws IllegalStateException(\"Invalid userId ...\"), and this guard runs at the start of setVirtualStorage, getVirtualStorage, setVirtualStorageState, and isVirtualStorageEnable.","triggerScenarios":"Calling any of those four APIs with a userId that was never created via VUserManagerService (e.g. userId 0 assumptions that don't hold, a user that was removed, or a hardcoded/hard-typed ID from config).","commonSituations":"Calling virtual-storage APIs before any virtual users have been created during first-run initialization; using a userId saved before the user was deleted; off-by-one or 1-based userId values passed where 0-based were expected.","solutions":["Create the virtual user first (VUserManagerService.createUser) before touching its virtual storage","Validate the userId with VUserManagerService.get().exists(userId) before the call and fail fast with your own message","Ensure the userId passed around your app is the same convention VA uses (int index as returned by user creation/listing APIs)","Catch the IllegalStateException and re-initialize user + storage if the user is missing"],"exampleFix":"// before\nstorage.setVirtualStorage(userId, path);\n// after\nif (!VUserManagerService.get().exists(userId)) {\n    throw new IllegalArgumentException(\"user \" + userId + \" must be created first\");\n}\nstorage.setVirtualStorage(userId, path);","handlingStrategy":"validation","validationCode":"// Java: guard before any VirtualStorageService call\nVUserManagerService ums = VUserManagerService.get();\nif (ums == null || !ums.exists(userId)) {\n    throw new IllegalArgumentException(\"virtual user \" + userId + \" does not exist\");\n}","typeGuard":"boolean validUser(int userId) {\n    VUserManagerService ums = VUserManagerService.get();\n    return ums != null && userId >= 0 && ums.exists(userId);\n}","tryCatchPattern":null,"preventionTips":["Create the virtual user before configuring its storage","Keep userIds as returned by VA's user APIs; never hardcode them","Handle user-deletion events by invalidating cached userIds","Run virtual-storage setup after user initialization completes"],"tags":["android","virtualapp","user-management","state-error","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"666fefcb5d3f39cc944001c3457c38ffd6544c87","analyzedAt":"2026-09-09T11:09:01.694Z","contentChangedAt":"2026-09-09T11:09:01.694Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}