From c563762bf4496e8bd415dd8accd06a3e1723b7ee Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 6 Dec 2017 17:32:05 +0100 Subject: Avoid number conversion issues Use unsigned types where we mean unsigned and signed types where we mean signed. Check the integer range before casting to a smaller type. Change-Id: Ia2150282e9763855c495bd5547e2bc176d8d93be Reviewed-by: Milian Wolff --- app/perftracingdata.h | 18 +++++++++--------- app/perfunwind.cpp | 5 ++++- app/perfunwind.h | 2 +- tests/auto/elfmap/tst_elfmap.cpp | 22 +++++++++++----------- tests/auto/perfdata/tst_perfdata.cpp | 3 ++- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/app/perftracingdata.h b/app/perftracingdata.h index 4427f8d..3c83daf 100644 --- a/app/perftracingdata.h +++ b/app/perftracingdata.h @@ -24,16 +24,16 @@ #include #include -enum FormatFlags +enum FormatFlags: quint32 { - FIELD_IS_ARRAY = 1, - FIELD_IS_POINTER = 2, - FIELD_IS_SIGNED = 4, - FIELD_IS_STRING = 8, - FIELD_IS_DYNAMIC = 16, - FIELD_IS_LONG = 32, - FIELD_IS_FLAG = 64, - FIELD_IS_SYMBOLIC = 128, + FIELD_IS_ARRAY = 1 << 0, + FIELD_IS_POINTER = 1 << 1, + FIELD_IS_SIGNED = 1 << 2, + FIELD_IS_STRING = 1 << 3, + FIELD_IS_DYNAMIC = 1 << 4, + FIELD_IS_LONG = 1 << 5, + FIELD_IS_FLAG = 1 << 6, + FIELD_IS_SYMBOLIC = 1 << 7, }; struct FormatField diff --git a/app/perfunwind.cpp b/app/perfunwind.cpp index 0830f92..849b220 100644 --- a/app/perfunwind.cpp +++ b/app/perfunwind.cpp @@ -565,7 +565,10 @@ void PerfUnwind::analyze(const PerfRecordSample &sample) const auto &attribute = m_attributes.at(attributesId); if (attribute.type() == PerfEventAttributes::TYPE_TRACEPOINT) { type = TracePointSample; - eventFormatId = attribute.config(); + if (attribute.config() > std::numeric_limits::max()) + qWarning() << "Excessively large event format ID" << attribute.config(); + else + eventFormatId = static_cast(attribute.config()); } } diff --git a/app/perfunwind.h b/app/perfunwind.h index 36b8f22..a18d6a0 100644 --- a/app/perfunwind.h +++ b/app/perfunwind.h @@ -92,7 +92,7 @@ public: UnwindInfo() : frames(0), unwind(nullptr), sample(nullptr), maxFrames(64), firstGuessedFrame(-1), isInterworking(false) {} - QHash> stackValues; + QHash> stackValues; QVector frames; PerfUnwind *unwind; const PerfRecordSample *sample; diff --git a/tests/auto/elfmap/tst_elfmap.cpp b/tests/auto/elfmap/tst_elfmap.cpp index 58ce323..aac377b 100644 --- a/tests/auto/elfmap/tst_elfmap.cpp +++ b/tests/auto/elfmap/tst_elfmap.cpp @@ -169,7 +169,7 @@ private slots: void benchRegisterElfDisjunct() { - QFETCH(int, numElfMaps); + QFETCH(uint, numElfMaps); const quint64 ADDR_STEP = 1024; const quint64 MAX_ADDR = ADDR_STEP * numElfMaps; const quint64 LEN = 1024; @@ -183,16 +183,16 @@ private slots: void benchRegisterElfDisjunct_data() { - QTest::addColumn("numElfMaps"); - QTest::newRow("10") << 10; - QTest::newRow("100") << 100; - QTest::newRow("1000") << 1000; - QTest::newRow("2000") << 2000; + QTest::addColumn("numElfMaps"); + QTest::newRow("10") << 10u; + QTest::newRow("100") << 100u; + QTest::newRow("1000") << 1000u; + QTest::newRow("2000") << 2000u; } void benchRegisterElfOverlapping() { - QFETCH(int, numElfMaps); + QFETCH(uint, numElfMaps); const quint64 ADDR_STEP = 1024; const quint64 MAX_ADDR = ADDR_STEP * numElfMaps; quint64 len = MAX_ADDR; @@ -212,7 +212,7 @@ private slots: void benchRegisterElfExpanding() { - QFETCH(int, numElfMaps); + QFETCH(uint, numElfMaps); const quint64 ADDR = 0; const quint64 LEN_STEP = 1024; const quint64 MAX_LEN = LEN_STEP * numElfMaps; @@ -231,7 +231,7 @@ private slots: void benchFindElfDisjunct() { - QFETCH(int, numElfMaps); + QFETCH(uint, numElfMaps); PerfElfMap map; @@ -258,7 +258,7 @@ private slots: void benchFindElfOverlapping() { - QFETCH(int, numElfMaps); + QFETCH(uint, numElfMaps); PerfElfMap map; @@ -285,7 +285,7 @@ private slots: void benchFindElfExpanding() { - QFETCH(int, numElfMaps); + QFETCH(uint, numElfMaps); PerfElfMap map; diff --git a/tests/auto/perfdata/tst_perfdata.cpp b/tests/auto/perfdata/tst_perfdata.cpp index e9fc85f..bf4b72c 100644 --- a/tests/auto/perfdata/tst_perfdata.cpp +++ b/tests/auto/perfdata/tst_perfdata.cpp @@ -164,8 +164,9 @@ void TestPerfData::testTracingData() const PerfParserTestClient::AttributeEvent attribute = client.attribute(sample.attributeId); QCOMPARE(attribute.type, 2u); + QVERIFY(attribute.config <= std::numeric_limits::max()); const PerfParserTestClient::TracePointFormatEvent format - = client.tracePointFormat(attribute.config); + = client.tracePointFormat(static_cast(attribute.config)); QCOMPARE(client.string(format.system), QByteArray("probe_untitled1")); QCOMPARE(client.string(format.name), QByteArray("main")); QCOMPARE(format.flags, 0u); -- cgit v1.2.3