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

[Bug] Potential NullPointerException in AlarmSettings #633

Closed
LightningRS opened this issue Oct 11, 2022 · 1 comment · Fixed by #636
Closed

[Bug] Potential NullPointerException in AlarmSettings #633

LightningRS opened this issue Oct 11, 2022 · 1 comment · Fixed by #636
Labels

Comments

@LightningRS
Copy link

Description:
We inspected the latest release version of Suntimes (0.14.8 from F-droid) using an Android application testing tool under development, and found a potential NullPointerException bug.

In the public function com.forrestguice.suntimeswidget.alarmclock.AlarmSettings.setDefaultRingtone(), line 303 and line 309 in AlarmSettings.java, an Android API RingtoneManager.getActualDefaultRingtoneUri() has been called, and the returned Uri object is stored in the variable uri.

    uri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM);
    ...
    uri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION);

However, according to Android SDK 25, this API call may return null.

    public static Uri getActualDefaultRingtoneUri(Context context, int type) {
        String setting = getSettingForType(type);
        if (setting == null) return null;  // might return null
        final String uriString = Settings.System.getStringForUser(context.getContentResolver(),
                setting, context.getUserId());
        return uriString != null ? Uri.parse(uriString) : null;  // might return null
    }

At line 316, uri.toString() method is called without checking whether uri is null, which cause NullPointerException.

        prefs.putString(key_uri, uri.toString()); // Potential NPE

Suggested solution:

We suggest changing the code on line 316 to the following form to avoid the problem.

        prefs.putString(key_uri, uri != null ? uri.toString() : null); // Avoid NPE

Stacktrace:

E AndroidRuntime: java.lang.RuntimeException: An error occurred while executing doInBackground()
E AndroidRuntime: 	at android.os.AsyncTask$3.done(AsyncTask.java:325)
E AndroidRuntime: 	at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
E AndroidRuntime: 	at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
E AndroidRuntime: 	at java.util.concurrent.FutureTask.run(FutureTask.java:242)
E AndroidRuntime: 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
E AndroidRuntime: 	at java.lang.Thread.run(Thread.java:761)
E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
E AndroidRuntime: 	at com.forrestguice.suntimeswidget.alarmclock.AlarmSettings.setDefaultRingtone(AlarmSettings.java:316)
E AndroidRuntime: 	at com.forrestguice.suntimeswidget.alarmclock.AlarmSettings$CacheDefaultRingtoneTask.doInBackground(AlarmSettings.java:345)
E AndroidRuntime: 	at com.forrestguice.suntimeswidget.alarmclock.AlarmSettings$CacheDefaultRingtoneTask.doInBackground(AlarmSettings.java:332)
E AndroidRuntime: 	at android.os.AsyncTask$2.call(AsyncTask.java:305)
E AndroidRuntime: 	at java.util.concurrent.FutureTask.run(FutureTask.java:237)

Version Info:

  • App Version: 0.14.8
  • Android Version: Android 7.1.2
  • Device Model: Emulator
@forrestguice
Copy link
Owner

Thanks for bringing this to my attention.
I've applied the nullcheck recommended in #634 - it should be fixed as part of v0.14.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants