{"record":{"id":"4bb77fdcdf7301a0","repo":"didi/DoKit","slug":"segment-of-is-illegal","errorCode":null,"errorMessage":"segment of <{}> is illegal","messagePattern":"segment of <(.+?)> is illegal","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/PathUtils.java","lineNumber":65,"sourceCode":"        return newPath;\n    }\n\n    private static String getLegalSegment(String segment) {\n        int st = -1, end = -1;\n        char[] charArray = segment.toCharArray();\n        for (int i = 0; i < charArray.length; i++) {\n            char c = charArray[i];\n            if (c != SEP) {\n                if (st == -1) {\n                    st = i;\n                }\n                end = i;\n            }\n        }\n        if (st >= 0 && end >= st) {\n            return segment.substring(st, end + 1);\n        }\n        throw new IllegalArgumentException(\"segment of <\" + segment + \"> is illegal\");\n    }\n\n    /**\n     * Return the path of /system.\n     *\n     * @return the path of /system\n     */\n    public static String getRootPath() {\n        return getAbsolutePath(Environment.getRootDirectory());\n    }\n\n    /**\n     * Return the path of /data.\n     *\n     * @return the path of /data\n     */\n    public static String getDataPath() {\n        return getAbsolutePath(Environment.getDataDirectory());","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/PathUtils.java#L47-L83","documentation":"PathUtils.getFirstSegment(segment) scans the string for a run of non-separator characters (SEP, i.e. '/') and returns it via substring(st, end + 1). If the string consists entirely of separators (or is empty), st stays -1 / end < st and the method throws IllegalArgumentException('segment of <...> is illegal'). It expects at least one non-separator character.","triggerScenarios":"Calling with \"\", \"/\", \"//\", or a whitespace-free separator-only string; often the result of splitting a path like \"/\" on '/' producing empty pieces, or of Environment paths resolving unexpectedly.","commonSituations":"Parsing user-entered or intent-supplied paths; unit tests passing the empty string; processing root-relative paths whose first segment after trimming separators is empty.","solutions":["Validate before calling: reject segments where segment == null || segment.replace(\"/\", \"\").isEmpty().","Normalize with PathUtils or java.io.File first and handle the root path (\"/\") as a special case instead of segmenting it.","If splitting, filter empty tokens before calling getFirstSegment."],"exampleFix":"// before\nString seg = PathUtils.getFirstSegment(\"/\"); // throws\n\n// after\nString path = \"/\";\nString seg = path.replace(\"/\", \"\").isEmpty() ? null : PathUtils.getFirstSegment(path);","handlingStrategy":"validation","validationCode":"boolean legal = segment != null && !segment.replace(String.valueOf('/'), \"\").isEmpty();\nString first = legal ? PathUtils.getFirstSegment(segment) : null;","typeGuard":null,"tryCatchPattern":"try { seg = PathUtils.getFirstSegment(s); } catch (IllegalArgumentException e) { seg = null; }","preventionTips":["Treat '/' and '' as root/empty special cases before segmenting.","Filter empty tokens when splitting paths on the separator."],"tags":["filesystem","path","validation"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}