From e676f7c6b0f2be66271849dc81192f2603db58a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Sat, 31 Aug 2024 21:29:47 +0100 Subject: [PATCH] Add safeguard against invalid year of birth values --- .../gadgetbridge/GBApplication.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 2ac909614..923289d60 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -1728,9 +1728,18 @@ public class GBApplication extends Application { if (oldVersion < 38) { // Migrate year of birth to date of birth - final String yearOfBirth = sharedPrefs.getString("activity_user_year_of_birth", null); - if (StringUtils.isNotBlank(yearOfBirth)) { - editor.putString("activity_user_date_of_birth", String.format(Locale.ROOT, "%s-01-01", yearOfBirth.trim())); + try { + final String yearOfBirth = sharedPrefs.getString("activity_user_year_of_birth", null); + if (StringUtils.isNotBlank(yearOfBirth)) { + final int yearOfBirthValue = Integer.parseInt(yearOfBirth); + if (yearOfBirthValue > 1800 && yearOfBirthValue < 3000) { + editor.putString("activity_user_date_of_birth", String.format(Locale.ROOT, "%s-01-01", yearOfBirth.trim())); + } else { + Log.e(TAG, "Year of birth out of range, not migrating - " + yearOfBirth); + } + } + } catch (final Exception e) { + Log.e(TAG, "Failed to migrate year of birth to date of birth in version 38", e); } }