Skip to content

Commit 4c6a612

Browse files
authored
Fix codeql errors (project-chip#34248)
* Fix error with not checking results of snprintf * Fix codeql errors
1 parent d856745 commit 4c6a612

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

examples/common/tracing/decoder/logging/ToCertificateString.cpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const char * ToCertificate(const chip::ByteSpan & source, chip::MutableCharSpan
3535
{
3636
// Reset the buffer
3737
memset(destination.data(), '\0', destination.size());
38-
38+
int snprintf_len = 0;
3939
if (source.size() == 0)
4040
{
4141
return destination.data();
@@ -70,7 +70,8 @@ const char * ToCertificate(const chip::ByteSpan & source, chip::MutableCharSpan
7070
ChipLogError(DataManagement, "Certificate size is greater than 400 bytes");
7171
}
7272

73-
snprintf(destination.data(), destination.size(), "%s", str.Get());
73+
snprintf_len = snprintf(destination.data(), destination.size(), "%s", str.Get());
74+
VerifyOrExit(snprintf_len >= 0, ChipLogError(DataManagement, "Failed to write certificate"););
7475
}
7576
else
7677
{
@@ -83,15 +84,23 @@ const char * ToCertificate(const chip::ByteSpan & source, chip::MutableCharSpan
8384
size_t inIndex = 0;
8485
size_t outIndex = strlen(header) + 1;
8586

86-
snprintf(destination.data(), destination.size(), "%s\n", header);
87+
snprintf_len = snprintf(destination.data(), destination.size(), "%s\n", header);
88+
VerifyOrExit(snprintf_len >= 0, ChipLogError(DataManagement, "Failed to write header"););
8789
for (; inIndex < base64DataLen; inIndex += 64)
8890
{
89-
auto charsPrinted = snprintf(&destination.data()[outIndex], destination.size() - outIndex, "%.64s\n", &str[inIndex]);
90-
outIndex += static_cast<size_t>(charsPrinted);
91+
snprintf_len = snprintf(&destination.data()[outIndex], destination.size() - outIndex, "%.64s\n", &str[inIndex]);
92+
VerifyOrExit(snprintf_len >= 0, ChipLogError(DataManagement, "Failed to write certificate"););
93+
94+
outIndex += static_cast<size_t>(snprintf_len);
9195
}
92-
snprintf(&destination.data()[outIndex], destination.size() - outIndex, "%s", footer);
96+
snprintf_len = snprintf(&destination.data()[outIndex], destination.size() - outIndex, "%s", footer);
97+
VerifyOrExit(snprintf_len >= 0, ChipLogError(DataManagement, "Failed to write footer"););
98+
}
99+
exit:
100+
if (snprintf_len < 0)
101+
{
102+
memset(destination.data(), '\0', destination.size());
93103
}
94-
95104
return destination.data();
96105
}
97106

src/app/WriteClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class WriteClient : public Messaging::ExchangeDelegate
178178
ReturnErrorOnFailure(EncodeSingleAttributeDataIB(path, DataModel::List<uint8_t>()));
179179

180180
path.mListOp = ConcreteDataAttributePath::ListOperation::AppendItem;
181-
for (ListIndex i = 0; i < value.size(); i++)
181+
for (size_t i = 0; i < value.size(); i++)
182182
{
183183
ReturnErrorOnFailure(EncodeSingleAttributeDataIB(path, value.data()[i]));
184184
}

src/app/tests/TestBufferedReadCallback.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,11 @@ void DataSeriesValidator::OnAttributeData(const ConcreteDataAttributePath & aPat
235235

236236
auto iter = value.begin();
237237

238-
uint8_t index = 0;
238+
uint32_t index = 0;
239239
while (iter.Next() && index < expectedListLength)
240240
{
241241
auto & iterValue = iter.GetValue();
242-
EXPECT_EQ(iterValue, (index));
242+
EXPECT_EQ(iterValue, (index % 256));
243243
index++;
244244
}
245245

src/lib/shell/commands/Config.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static CHIP_ERROR ConfigSetSetupDiscriminator(char * argv)
142142
}
143143
else
144144
{
145-
streamer_printf(sout, "Setup discriminator setting failed with code: %d\r\n", error);
145+
streamer_printf(sout, "Setup discriminator setting failed with code: %d\r\n", error.AsInteger());
146146
}
147147

148148
return error;

src/lib/shell/commands/Dns.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,18 @@ class DnsShellResolverDelegate : public Dnssd::DiscoverNodeDelegate, public Addr
122122
auto retryInterval = nodeData.GetMrpRetryIntervalIdle();
123123

124124
if (retryInterval.has_value())
125-
streamer_printf(streamer_get(), " MRP retry interval (idle): %" PRIu32 "ms\r\n", *retryInterval);
125+
streamer_printf(streamer_get(), " MRP retry interval (idle): %" PRIu32 "ms\r\n", retryInterval->count());
126126

127127
retryInterval = nodeData.GetMrpRetryIntervalActive();
128128

129129
if (retryInterval.has_value())
130-
streamer_printf(streamer_get(), " MRP retry interval (active): %" PRIu32 "ms\r\n", *retryInterval);
130+
streamer_printf(streamer_get(), " MRP retry interval (active): %" PRIu32 "ms\r\n", retryInterval->count());
131131

132132
auto activeThreshold = nodeData.GetMrpRetryActiveThreshold();
133133

134134
if (activeThreshold.has_value())
135135
{
136-
streamer_printf(streamer_get(), " MRP retry active threshold time: %" PRIu32 "ms\r\n", *activeThreshold);
136+
streamer_printf(streamer_get(), " MRP retry active threshold time: %" PRIu32 "ms\r\n", activeThreshold->count());
137137
}
138138

139139
streamer_printf(streamer_get(), " Supports TCP Client: %s\r\n", nodeData.supportsTcpClient ? "yes" : "no");

0 commit comments

Comments
 (0)