apsun / RemotePreferences

A drop-in solution for inter-app access to SharedPreferences.
MIT License
138 stars 32 forks source link

No exception thrown when Key is NULL #15

Closed hahagu closed 3 years ago

hahagu commented 3 years ago

If RemotePreferences is called upon a nulled object reference, such as:

SharedPreferences rpo = new RemotePreferences(context, "authority", "pref_name", true);
String prefKey;
String prefValue;

// <... code to set prefKey as some key that fails ...>
// At this point, prefKey = null

try {
    prefValue = rpo.getString(prefKey, "");
}
catch (RemotePreferenceAccessException e) {
    Log.e("FAIL");
}

Log.d(prefValue);

This above code fails silently. The fail message does not show up, and the last line, Log.d(prefValue); does not even reach, but without any errors on the logcat.

apsun commented 3 years ago

From the readme:

Also note that your preference keys cannot be null or "" (empty string).

So if you are in fact trying to use it with a null key, all bets are off. Essentially, this is undefined behavior. That said, we have checks for this. Are you sure it is failing silently? Passing null as a key should throw IllegalArgumentException. It does not throw RemotePreferenceAccessException because it is an incorrect usage of the API, not an expected failure condition of RPC.

hahagu commented 3 years ago

Hmm.. I did not check if it throws the IllegalArgumentException. I guess there should be additional checks in place to catch null exception in my own code.

Thanks for pointing me to the right direction.