Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 82a11ba

Browse files
committed
Merge pull request #323 from tcahuzax/klocwork_fix_3.1.0
Fix errors found by Klocwork on the PF 3.1.0 - Wrong instance lifetime - Using uninitialized variable - Avoiding using memcpy - Removing strncpy call
2 parents 73ce970 + 563960d commit 82a11ba

File tree

4 files changed

+49
-26
lines changed

4 files changed

+49
-26
lines changed

test/functional-tests/Basic.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,34 @@ SCENARIO_METHOD(ParameterFramework, "No Logger", "[log]")
6262
}
6363
}
6464

65-
SCENARIO_METHOD(WarningPF, "Logger should receive info and warnings", "[log]")
65+
SCENARIO("Logger should receive info and warnings", "[log]")
6666
{
67-
GIVEN ("Config files that emit warnings") {
68-
GIVEN ("A logger that stores logs") {
69-
StoreLogger logger{};
70-
WHEN ("The record logger is set") {
71-
setLogger(&logger);
72-
THEN ("Start should succeed") {
73-
REQUIRE_NOTHROW(start());
74-
AND_THEN ("The logger should have stored info and warning log") {
75-
using Logs = StoreLogger::Logs;
76-
using Level = StoreLogger::Log::Level;
77-
CHECK(logger.filter(Level::warning) != Logs{});
78-
CHECK(logger.getLogs() != Logs{});
79-
}
80-
}
81-
AND_WHEN ("A nullptr logger is set") {
82-
setLogger(nullptr);
67+
GIVEN ("A logger that stores logs") {
68+
/* Instantiating logger first to ensure that its lifetime is longer than the pfw's one,
69+
* because the pfw references the logger. */
70+
StoreLogger logger{};
71+
GIVEN ("A parameter framework") {
72+
WarningPF pfw;
73+
GIVEN ("Config files that emit warnings") {
74+
WHEN ("The record logger is set") {
75+
pfw.setLogger(&logger);
8376
THEN ("Start should succeed") {
84-
REQUIRE_NOTHROW(start());
85-
AND_THEN (
86-
"The record logger should NOT have stored any info or warning log") {
87-
CHECK(logger.getLogs() == StoreLogger::Logs{});
77+
REQUIRE_NOTHROW(pfw.start());
78+
AND_THEN ("The logger should have stored info and warning log") {
79+
using Logs = StoreLogger::Logs;
80+
using Level = StoreLogger::Log::Level;
81+
CHECK(logger.filter(Level::warning) != Logs{});
82+
CHECK(logger.getLogs() != Logs{});
83+
}
84+
}
85+
AND_WHEN ("A nullptr logger is set") {
86+
pfw.setLogger(nullptr);
87+
THEN ("Start should succeed") {
88+
REQUIRE_NOTHROW(pfw.start());
89+
AND_THEN ("The record logger should NOT have stored any info or "
90+
"warning log") {
91+
CHECK(logger.getLogs() == StoreLogger::Logs{});
92+
}
8893
}
8994
}
9095
}

test/functional-tests/FloatingPoint.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ SCENARIO_METHOD(FloatsPF, "Floating points", "[floating points]")
9999
GIVEN ("A valid value " + vec.title) {
100100
CHECK_NOTHROW(setParameter(path, vec.payload));
101101
string getValueBack;
102-
CHECK_NOTHROW(getParameter(path, getValueBack));
102+
REQUIRE_NOTHROW(getParameter(path, getValueBack));
103103
CHECK(getValueBack == vec.payload);
104104
}
105105
}
@@ -138,7 +138,7 @@ SCENARIO_METHOD(FloatsPF, "Floating points", "[floating points]")
138138
GIVEN ("A valid value " + vec.title) {
139139
CHECK_NOTHROW(setParameter(path, vec.payload));
140140
string getValueBack;
141-
CHECK_NOTHROW(getParameter(path, getValueBack));
141+
REQUIRE_NOTHROW(getParameter(path, getValueBack));
142142
CHECK(getValueBack == vec.payload);
143143
}
144144
}
@@ -161,7 +161,7 @@ SCENARIO_METHOD(FloatsPF, "Floating points", "[floating points]")
161161
GIVEN ("A valid value " + vec.title) {
162162
CHECK_NOTHROW(handle.setAsDouble(vec.payload));
163163
double getValueBack;
164-
CHECK_NOTHROW(handle.getAsDouble(getValueBack));
164+
REQUIRE_NOTHROW(handle.getAsDouble(getValueBack));
165165
CHECK(getValueBack == vec.payload);
166166
}
167167
}

test/test-subsystem/TESTSubsystemBinary.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <stdlib.h>
3535
#include <assert.h>
3636
#include <algorithm>
37+
#include <AlwaysAssert.hpp>
3738
#include <Iterator.hpp>
3839
#include "TESTSubsystemBinary.h"
3940

@@ -53,7 +54,10 @@ std::string CTESTSubsystemBinary::toString(const void *pvValue, size_t size) con
5354

5455
assert(size <= sizeof(uiValue));
5556

56-
memcpy(&uiValue, pvValue, size);
57+
auto first = MAKE_ARRAY_ITERATOR(static_cast<const uint8_t *>(pvValue), size);
58+
auto destination = MAKE_ARRAY_ITERATOR(reinterpret_cast<uint8_t *>(&uiValue), sizeof(uiValue));
59+
60+
std::copy_n(first, size, destination);
5761

5862
strStream << "0x" << std::hex << uiValue;
5963

test/test-subsystem/TESTSubsystemString.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030
#include <string.h>
31+
#include <iterator>
32+
#include <algorithm>
33+
#include <stdexcept>
34+
#include <Iterator.hpp>
3135
#include "TESTSubsystemString.h"
3236

3337
#define base CTESTSubsystemObject
@@ -46,5 +50,15 @@ std::string CTESTSubsystemString::toString(const void *pvValue, size_t /*size*/)
4650

4751
void CTESTSubsystemString::fromString(const std::string &strValue, void *pvValue, size_t size)
4852
{
49-
strncpy((char *)pvValue, strValue.c_str(), size);
53+
std::size_t requiredBufferSize = strValue.length() + 1; /* Adding one for null character */
54+
if (size < requiredBufferSize) {
55+
throw std::logic_error("Buffer is to small: " + std::to_string(size) + " Minimum size: " +
56+
std::to_string(requiredBufferSize));
57+
}
58+
59+
auto destination = MAKE_ARRAY_ITERATOR(static_cast<char *>(pvValue), size);
60+
auto last = std::copy(begin(strValue), end(strValue), destination);
61+
62+
/* Adding null character */
63+
*last = 0;
5064
}

0 commit comments

Comments
 (0)