Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException when reading non String value from Java System.properties #1139

Closed
pachoo opened this issue Oct 17, 2013 · 1 comment
Closed

Comments

@pachoo
Copy link

@pachoo 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 System.properties. 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("user.name");
            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
Copy link
Member

@headius 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.