Skip to content

Commit 0d27f42

Browse files
authored
Fix deregistering error formatter (project-chip#37254)
* Fix deregistering error formatter * Don't dereference address pointed by lfp in for loop expression as it may be a nullptr. * Add simple test that registers and deregisters single error formatter. * Deregister all formatters at the end of CheckRegisterDeregisterErrorFormatter. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no> * Fix error formatter tests * Add function to deregister CHIP layer error formatter. * Deregister CHIP error formatter at the end of tests to not interfere with other tests. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no> --------- Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no>
1 parent 3700a5a commit 0d27f42

File tree

7 files changed

+44
-3
lines changed

7 files changed

+44
-3
lines changed

src/lib/core/CHIPError.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@
2222

2323
namespace chip {
2424

25+
static ErrorFormatter sCHIPErrorFormatter = { FormatCHIPError, nullptr };
26+
2527
/**
2628
* Register a text error formatter for CHIP core errors.
2729
*/
2830
void RegisterCHIPLayerErrorFormatter()
2931
{
30-
static ErrorFormatter sCHIPErrorFormatter = { FormatCHIPError, nullptr };
31-
3232
RegisterErrorFormatter(&sCHIPErrorFormatter);
3333
}
3434

35+
/**
36+
* Deregister a text error formatter for CHIP core errors.
37+
*/
38+
void DeregisterCHIPLayerErrorFormatter()
39+
{
40+
DeregisterErrorFormatter(&sCHIPErrorFormatter);
41+
}
42+
3543
/**
3644
* Given a CHIP error, returns a human-readable NULL-terminated C string
3745
* describing the error.

src/lib/core/CHIPError.h

+1
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,7 @@ using CHIP_ERROR = ::chip::ChipError;
18211821
namespace chip {
18221822

18231823
extern void RegisterCHIPLayerErrorFormatter();
1824+
extern void DeregisterCHIPLayerErrorFormatter();
18241825
extern bool FormatCHIPError(char * buf, uint16_t bufSize, CHIP_ERROR err);
18251826

18261827
} // namespace chip

src/lib/core/ErrorStr.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,17 @@ DLL_EXPORT void RegisterErrorFormatter(ErrorFormatter * errFormatter)
142142
DLL_EXPORT void DeregisterErrorFormatter(ErrorFormatter * errFormatter)
143143
{
144144
// Remove the formatter if present
145-
for (ErrorFormatter ** lfp = &sErrorFormatterList; *lfp != nullptr; lfp = &(*lfp)->Next)
145+
for (ErrorFormatter ** lfp = &sErrorFormatterList; *lfp != nullptr;)
146146
{
147147
// Remove the formatter from the global list, if found.
148148
if (*lfp == errFormatter)
149149
{
150150
*lfp = errFormatter->Next;
151151
}
152+
else
153+
{
154+
lfp = &(*lfp)->Next;
155+
}
152156
}
153157
}
154158

src/lib/core/tests/TestCHIPErrorStr.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ TEST(TestCHIPErrorStr, CheckCoreErrorStr)
207207
// ErrorStr with static char array.
208208
CheckCoreErrorStrHelper(ErrorStr(err, /*withSourceLocation=*/true), err);
209209
}
210+
211+
// Deregister the layer error formatter
212+
DeregisterCHIPLayerErrorFormatter();
210213
}
211214

212215
TEST(TestCHIPErrorStr, CheckCoreErrorStrStorage)
@@ -222,6 +225,9 @@ TEST(TestCHIPErrorStr, CheckCoreErrorStrStorage)
222225
ErrorStrStorage storage;
223226
CheckCoreErrorStrHelper(ErrorStr(err, /*withSourceLocation=*/true, storage), err);
224227
}
228+
229+
// Deregister the layer error formatter
230+
DeregisterCHIPLayerErrorFormatter();
225231
}
226232

227233
void CheckCoreErrorStrWithoutSourceLocationHelper(const char * errStr, CHIP_ERROR err)
@@ -258,6 +264,9 @@ TEST(TestCHIPErrorStr, CheckCoreErrorStrWithoutSourceLocation)
258264
// ErrorStr with static char array.
259265
CheckCoreErrorStrWithoutSourceLocationHelper(ErrorStr(err, /*withSourceLocation=*/false), err);
260266
}
267+
268+
// Deregister the layer error formatter
269+
DeregisterCHIPLayerErrorFormatter();
261270
}
262271

263272
TEST(TestCHIPErrorStr, CheckCoreErrorStrStorageWithoutSourceLocation)
@@ -273,4 +282,7 @@ TEST(TestCHIPErrorStr, CheckCoreErrorStrStorageWithoutSourceLocation)
273282
ErrorStrStorage storage;
274283
CheckCoreErrorStrWithoutSourceLocationHelper(ErrorStr(err, /*withSourceLocation=*/false, storage), err);
275284
}
285+
286+
// Deregister the layer error formatter
287+
DeregisterCHIPLayerErrorFormatter();
276288
}

src/lib/support/tests/TestErrorStr.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ static bool trueFormat(char * buf, uint16_t bufSize, CHIP_ERROR err)
6767
return true; // means I handled it
6868
}
6969

70+
TEST(TestErrorStr, CheckRegisterDeregisterSingleErrorFormatter)
71+
{
72+
static ErrorFormatter falseFormatter = { falseFormat, nullptr };
73+
74+
RegisterErrorFormatter(&falseFormatter);
75+
DeregisterErrorFormatter(&falseFormatter);
76+
}
77+
7078
TEST(TestErrorStr, CheckRegisterDeregisterErrorFormatter)
7179
{
7280
static ErrorFormatter falseFormatter = { falseFormat, nullptr };
@@ -114,6 +122,8 @@ TEST(TestErrorStr, CheckRegisterDeregisterErrorFormatter)
114122

115123
// verify this doesn't crash
116124
DeregisterErrorFormatter(&trueFormatter);
125+
DeregisterErrorFormatter(&falseFormatter);
126+
DeregisterErrorFormatter(&falseFormatter2);
117127
}
118128

119129
TEST(TestErrorStr, CheckNoError)

src/system/tests/TestSystemErrorStr.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,7 @@ TEST(TestSystemErrorStr, CheckSystemErrorStr)
6969
EXPECT_NE(strchr(errStr, ':'), nullptr);
7070
#endif // !CHIP_CONFIG_SHORT_ERROR_STR
7171
}
72+
73+
// Deregister the layer error formatter
74+
DeregisterCHIPLayerErrorFormatter();
7275
}

src/system/tests/TestSystemPacketBuffer.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ class TestSystemPacketBuffer : public ::testing::Test
100100
{
101101
chip::DeviceLayer::PlatformMgr().Shutdown();
102102
chip::Platform::MemoryShutdown();
103+
104+
// Deregister the layer error formatter
105+
DeregisterCHIPLayerErrorFormatter();
103106
}
104107

105108
void SetUp()

0 commit comments

Comments
 (0)