Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 96b62382 authored by Narayan Kamath's avatar Narayan Kamath
Browse files

Validate TimeZone in TextClock.setTimeZone

TimeZone.getTimeZone appears to perform less stringent checks
than tz.ToZoneId, which leads to a crash in the widget when we do eventually call toZoneId upon a time tick.

An example of such an invalid timezone is "GMT+20:00".

TextClock is a remotable view, so this leads to crashes in the Launcher process when it's part of a widget.

Bug: 372811751
Change-Id: I3f0bf19830639387926f8ab2e685a7dd0d991958
Test: atest TextClockTest
Flag: EXEMPT Localized bug fix with limited impact
parent ced83430
Loading
Loading
Loading
Loading
+19 −3
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import android.view.inspector.InspectableProperty;
import com.android.internal.R;
import com.android.internal.util.Preconditions;

import java.time.DateTimeException;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
@@ -291,11 +292,26 @@ public class TextClock extends TextView {
    }

    private void createTime(String timeZone) {
        if (timeZone != null) {
            mTime = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
        TimeZone tz = null;
        if (timeZone == null) {
            tz = TimeZone.getDefault();
            // Note that mTimeZone should always be null if timeZone is.
        } else {
            mTime = Calendar.getInstance();
            tz = TimeZone.getTimeZone(timeZone);
            try {
                // Try converting this TZ to a zoneId to make sure it's valid. This
                // performs a different set of checks than TimeZone.getTimeZone so
                // we can avoid exceptions later when we do need this conversion.
                tz.toZoneId();
            } catch (DateTimeException ex) {
                // If we're here, the user supplied timezone is invalid, so reset
                // mTimeZone to something sane.
                tz = TimeZone.getDefault();
                mTimeZone = tz.getID();
            }
        }

        mTime = Calendar.getInstance(tz);
    }

    /**