diff --git a/lib/src/huami_legacy.dart b/lib/src/huami_legacy.dart index 72b200a..b7df2ec 100644 --- a/lib/src/huami_legacy.dart +++ b/lib/src/huami_legacy.dart @@ -97,7 +97,18 @@ const int kHuamiMaxFetchCount = 129600; if (count > kHuamiMaxFetchCount) return null; if (b.length < 15) return null; final tz = b[14] >= 0x80 ? b[14] - 0x100 : b[14]; - final wall = DateTime.utc(b[7] | (b[8] << 8), b[9], b[10], b[11], b[12], b[13]); + final year = b[7] | (b[8] << 8); + final wall = DateTime.utc(year, b[9], b[10], b[11], b[12], b[13]); + // an unclocked band sends zeros; month 13 / feb 31 would silently roll over. + if (year < 2000 || + year > 2100 || + wall.month != b[9] || + wall.day != b[10] || + wall.hour != b[11] || + wall.minute != b[12] || + wall.second != b[13]) { + return null; + } return ( count: count, startSec: wall.millisecondsSinceEpoch ~/ 1000 - tz * 15 * 60, diff --git a/test/huami_legacy_test.dart b/test/huami_legacy_test.dart index 199a1d7..2532123 100644 --- a/test/huami_legacy_test.dart +++ b/test/huami_legacy_test.dart @@ -34,6 +34,16 @@ void main() { expect(parseHuamiFetchStart([0x10, 0x01, 0x02]), isNull); }); + test('start response: implausible or rolled-over date is refused', () { + List f(int y, int mo, int d) => + [0x10, 0x01, 0x01, 60, 0, 0, 0, y & 0xff, y >> 8, mo, d, 0, 0, 0, 0]; + expect(parseHuamiFetchStart(f(0, 0, 0)), isNull); + expect(parseHuamiFetchStart(f(65535, 1, 1)), isNull); + expect(parseHuamiFetchStart(f(2026, 13, 1)), isNull); + expect(parseHuamiFetchStart(f(2026, 2, 31)), isNull); + expect(parseHuamiFetchStart(f(2026, 10, 3)), isNotNull); + }); + test('samples: kind inheritance, no-reading HR, minute timestamps', () { final buf = HuamiActivityBuffer() ..add([0, 0x09, 10, 0, 55, 0x00, 5, 0, 0xff])