From d78de6ab99a7e47da4f1841b80ab21ad5705f1a3 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 9 Jul 2026 16:27:11 +0100 Subject: [PATCH] fix(gecko-upgrade): don't crash on a counter with empty sample_groups --- src/profile-logic/gecko-profile-versioning.ts | 8 ++++++- src/test/unit/profile-upgrading.test.ts | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/profile-logic/gecko-profile-versioning.ts b/src/profile-logic/gecko-profile-versioning.ts index 38923e9c45..23001ac1c2 100644 --- a/src/profile-logic/gecko-profile-versioning.ts +++ b/src/profile-logic/gecko-profile-versioning.ts @@ -1463,7 +1463,13 @@ const _upgraders: { // upgraded counters; ignore them. continue; } - counter.samples = counter.sample_groups[0].samples; + // A counter that collected no data has an empty sample_groups array + // (see the version 18 upgrader), so fall back to an empty samples + // table when there is no group to unwrap. + counter.samples = + counter.sample_groups.length > 0 + ? counter.sample_groups[0].samples + : { schema: { time: 0, count: 1, number: 2 }, data: [] }; delete counter.sample_groups; } } diff --git a/src/test/unit/profile-upgrading.test.ts b/src/test/unit/profile-upgrading.test.ts index 6345faf12b..50a7d6ceaa 100644 --- a/src/test/unit/profile-upgrading.test.ts +++ b/src/test/unit/profile-upgrading.test.ts @@ -71,6 +71,30 @@ describe('upgrading gecko profiles', function () { // - upgrading tracing markers without interval information testProfileUpgrading(require('../fixtures/upgrades/gecko-2.json')); }); + + it('handles a counter with no collected data', function () { + // The version 18 upgrader turns a counter whose sample_groups object has no + // samples into one with an empty sample_groups array. The version 29 + // upgrader must not crash when unwrapping that empty array. + const profile: any = { + meta: { version: 17 }, + processes: [], + threads: [], + counters: [ + { + name: 'power', + category: 'power', + description: 'Power counter', + sample_groups: { id: 0 }, + }, + ], + }; + upgradeGeckoProfileToCurrentVersion(profile); + expect(profile.meta.version).toEqual(GECKO_PROFILE_VERSION); + const [counter] = profile.counters; + expect(counter.sample_groups).toBe(undefined); + expect(counter.samples.data).toEqual([]); + }); }); describe('upgrading processed profiles', function () {