ClassCastException when reading non String value from Java #1139

pachoo opened this Issue Oct 17, 2013 · 1 comment


None yet

2 participants

pachoo commented Oct 17, 2013

When JRuby is setting up the global environment it sets the JAVA_ENV variable to the value of Java's When there is a non String value the OSEnvironment code below will throw a ClassCastException.

    private static Map getAsMapOfRubyStrings(Ruby runtime, Set<Map.Entry<Object, Object>> entrySet) {
        Map envs = new HashMap();
        Encoding encoding = runtime.getEncodingService().getLocaleEncoding();

        // On Windows, entrySet doesn't have corresponding keys for these
        if (Platform.IS_WINDOWS) {
            // these may be null when in a restricted environment (JRUBY-6514)
            String home = SafePropertyAccessor.getProperty("user.home");
            String user = SafePropertyAccessor.getProperty("");
            addRubyKeyValuePair(runtime, envs, "HOME", home == null ? "/" : home, encoding);
            addRubyKeyValuePair(runtime, envs, "USER", user == null ? "" : user, encoding);

        for (Map.Entry<Object, Object> entry : entrySet) {
            // This will throw a ClassCastException if the value is not a String
            String value = (String)entry.getValue();
            String key = (String)entry.getKey();

            addRubyKeyValuePair(runtime, envs, key, value, encoding);

        return envs;

Though Properties should be all String values, since they are subclassed from java.util.Hashtable the put() method can add a non String object. From the Properties documentation:
"Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail. Similarly, the call to the propertyNames or list method will fail if it is called on a "compromised" Properties object that contains a non-String key."

headius commented Oct 21, 2013

Wow, who is actually doing this? I think in our case the best we can do would be to skip such values, because I don't see any reason you'd ever want to do this. Want to come up with a patch?

@enebo enebo added a commit that closed this issue Nov 12, 2013
@enebo enebo Fixes #1139. ClassCastException when reading non String value from Ja…
@enebo enebo closed this in 288e68e Nov 12, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment