Skip to content

Commit 791e64c

Browse files
committed
Merge branch 'fix-clang-tidy-bump-ci' into bump_CI_images
2 parents 311adc7 + 6b7ee04 commit 791e64c

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

src/tools/spake2p/Cmd_GenVerifier.cpp

+41-42
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
#include "spake2p.h"
2727

28-
#include <errno.h>
29-
#include <stdio.h>
28+
#include <cstring>
29+
#include <fstream>
30+
#include <iomanip>
31+
#include <iostream>
3032

3133
#include <CHIPVersion.h>
3234
#include <crypto/CHIPCryptoPAL.h>
@@ -157,30 +159,29 @@ uint8_t gSalt[BASE64_MAX_DECODED_LEN(BASE64_ENCODED_LEN(kSpake2p_Max_PBKDF_Salt_
157159
uint8_t gSaltDecodedLen = 0;
158160
uint8_t gSaltLen = 0;
159161
const char * gOutFileName = nullptr;
160-
FILE * gPinCodeFile = nullptr;
162+
std::ifstream gPinCodeFile;
161163

162164
static uint32_t GetNextPinCode()
163165
{
164-
if (!gPinCodeFile)
166+
if (!gPinCodeFile.is_open())
165167
{
166168
return chip::kSetupPINCodeUndefinedValue;
167169
}
168-
char * pinCodeStr = nullptr;
169-
size_t readSize = 8;
170-
uint32_t pinCode = chip::kSetupPINCodeUndefinedValue;
171-
if (getline(&pinCodeStr, &readSize, gPinCodeFile) != -1)
170+
std::string pinCodeStr;
171+
uint32_t pinCode = chip::kSetupPINCodeUndefinedValue;
172+
std::getline(gPinCodeFile, pinCodeStr);
173+
if (!gPinCodeFile.fail())
172174
{
173-
if (readSize > 8)
175+
if (pinCodeStr.length() > 8)
174176
{
175-
pinCodeStr[8] = 0;
177+
pinCodeStr = pinCodeStr.substr(0, 8);
176178
}
177-
pinCode = static_cast<uint32_t>(atoi(pinCodeStr));
179+
pinCode = static_cast<uint32_t>(atoi(pinCodeStr.c_str()));
178180
if (!chip::SetupPayload::IsValidSetupPIN(pinCode))
179181
{
180-
fprintf(stderr, "The line %s in PIN codes file is invalid, using a random PIN code.\n", pinCodeStr);
182+
std::cerr << "The line " << pinCodeStr << " in PIN codes file is invalid, using a random PIN code.\n";
181183
pinCode = chip::kSetupPINCodeUndefinedValue;
182184
}
183-
free(pinCodeStr);
184185
}
185186
return pinCode;
186187
}
@@ -206,8 +207,8 @@ bool HandleOption(const char * progName, OptionSet * optSet, int id, const char
206207
break;
207208

208209
case 'f':
209-
gPinCodeFile = fopen(arg, "r");
210-
if (!gPinCodeFile)
210+
gPinCodeFile.open(arg, std::ios::in);
211+
if (gPinCodeFile.fail())
211212
{
212213
PrintArgError("%s: Failed to open the PIN code file: %s\n", progName, arg);
213214
return false;
@@ -235,7 +236,7 @@ bool HandleOption(const char * progName, OptionSet * optSet, int id, const char
235236
case 's':
236237
if (strlen(arg) > BASE64_ENCODED_LEN(kSpake2p_Max_PBKDF_Salt_Length))
237238
{
238-
fprintf(stderr, "%s: Salt parameter too long: %s\n", progName, arg);
239+
std::cerr << progName << ": Salt parameter too long: " << arg << "\n";
239240
return false;
240241
}
241242

@@ -245,13 +246,13 @@ bool HandleOption(const char * progName, OptionSet * optSet, int id, const char
245246
// Now double-check if the length is correct.
246247
if (gSaltDecodedLen > kSpake2p_Max_PBKDF_Salt_Length)
247248
{
248-
fprintf(stderr, "%s: Salt parameter too long: %s\n", progName, arg);
249+
std::cerr << progName << ": Salt parameter too long: " << arg << "\n";
249250
return false;
250251
}
251252

252253
if (gSaltDecodedLen < kSpake2p_Min_PBKDF_Salt_Length)
253254
{
254-
fprintf(stderr, "%s: Salt parameter too short: %s\n", progName, arg);
255+
std::cerr << progName << ": Salt parameter too short: " << arg << "\n";
255256
return false;
256257
}
257258

@@ -273,31 +274,31 @@ bool HandleOption(const char * progName, OptionSet * optSet, int id, const char
273274

274275
bool Cmd_GenVerifier(int argc, char * argv[])
275276
{
276-
FILE * outFile = nullptr;
277-
277+
std::ofstream outFile;
278+
std::ostream * outStream = &outFile;
278279
if (argc == 1)
279280
{
280281
gHelpOptions.PrintBriefUsage(stderr);
281282
return true;
282283
}
283284

284285
bool res = ParseArgs(CMD_NAME, argc, argv, gCmdOptionSets);
285-
VerifyOrReturnError(res, false);
286+
VerifyOrReturnValue(res, false);
286287

287288
if (gIterationCount == 0)
288289
{
289-
fprintf(stderr, "Please specify the iteration-count parameter.\n");
290+
std::cerr << "Please specify the iteration-count parameter.\n";
290291
return false;
291292
}
292293

293294
if (gSaltDecodedLen == 0 && gSaltLen == 0)
294295
{
295-
fprintf(stderr, "Please specify at least one of the 'salt' or 'salt-len' parameters.\n");
296+
std::cerr << "Please specify at least one of the 'salt' or 'salt-len' parameters.\n";
296297
return false;
297298
}
298299
if (gSaltDecodedLen != 0 && gSaltLen != 0 && gSaltDecodedLen != gSaltLen)
299300
{
300-
fprintf(stderr, "The specified 'salt-len' doesn't match the length of 'salt' parameter.\n");
301+
std::cerr << "The specified 'salt-len' doesn't match the length of 'salt' parameter.\n";
301302
return false;
302303
}
303304
if (gSaltLen == 0)
@@ -307,28 +308,27 @@ bool Cmd_GenVerifier(int argc, char * argv[])
307308

308309
if (gOutFileName == nullptr)
309310
{
310-
fprintf(stderr, "Please specify the output file name, or - for stdout.\n");
311+
std::cerr << "Please specify the output file name, or - for stdout.\n";
311312
return false;
312313
}
313314

314315
if (strcmp(gOutFileName, "-") != 0)
315316
{
316-
outFile = fopen(gOutFileName, "w+b");
317-
if (outFile == nullptr)
317+
outFile.open(gOutFileName, std::ios::binary | std::ios::trunc);
318+
if (!outFile.is_open())
318319
{
319-
fprintf(stderr, "Unable to create file %s\n%s\n", gOutFileName, strerror(errno));
320+
std::cerr << "Unable to create file " << gOutFileName << "\n" << strerror(errno) << "\n";
320321
return false;
321322
}
322323
}
323324
else
324325
{
325-
outFile = stdout;
326+
outStream = &std::cout;
326327
}
327-
328-
if (fprintf(outFile, "Index,PIN Code,Iteration Count,Salt,Verifier\n") < 0 || ferror(outFile))
328+
(*outStream) << "Index,PIN Code,Iteration Count,Salt,Verifier\n";
329+
if (outStream->fail())
329330
{
330-
fprintf(stderr, "Error writing to output file: %s\n", strerror(errno));
331-
return false;
331+
std::cerr << "Error writing to output file: " << strerror(errno) << "\n";
332332
}
333333

334334
for (uint32_t i = 0; i < gCount; i++)
@@ -339,7 +339,7 @@ bool Cmd_GenVerifier(int argc, char * argv[])
339339
CHIP_ERROR err = chip::Crypto::DRBG_get_bytes(salt, gSaltLen);
340340
if (err != CHIP_NO_ERROR)
341341
{
342-
fprintf(stderr, "DRBG_get_bytes() failed.\n");
342+
std::cerr << "DRBG_get_bytes() failed.\n";
343343
return false;
344344
}
345345
}
@@ -353,7 +353,7 @@ bool Cmd_GenVerifier(int argc, char * argv[])
353353
(gPinCode == chip::kSetupPINCodeUndefinedValue), gPinCode);
354354
if (err != CHIP_NO_ERROR)
355355
{
356-
fprintf(stderr, "GeneratePASEVerifier() failed.\n");
356+
std::cerr << "GeneratePASEVerifier() failed.\n";
357357
return false;
358358
}
359359

@@ -362,7 +362,7 @@ bool Cmd_GenVerifier(int argc, char * argv[])
362362
err = verifier.Serialize(serializedVerifierSpan);
363363
if (err != CHIP_NO_ERROR)
364364
{
365-
fprintf(stderr, "Spake2pVerifier::Serialize() failed.\n");
365+
std::cerr << "Spake2pVerifier::Serialize() failed.\n";
366366
return false;
367367
}
368368

@@ -374,9 +374,11 @@ bool Cmd_GenVerifier(int argc, char * argv[])
374374
uint32_t verifierB64Len = chip::Base64Encode32(serializedVerifier, kSpake2p_VerifierSerialized_Length, verifierB64);
375375
verifierB64[verifierB64Len] = '\0';
376376

377-
if (fprintf(outFile, "%d,%08d,%d,%s,%s\n", i, gPinCode, gIterationCount, saltB64, verifierB64) < 0 || ferror(outFile))
377+
(*outStream) << i << "," << std::setfill('0') << std::setw(8) << gPinCode << "," << gIterationCount << "," << saltB64 << ","
378+
<< verifierB64 << "\n";
379+
if (outStream->fail())
378380
{
379-
fprintf(stderr, "Error writing to output file: %s\n", strerror(errno));
381+
std::cerr << "Error writing to output file: " << strerror(errno) << "\n";
380382
return false;
381383
}
382384

@@ -386,9 +388,6 @@ bool Cmd_GenVerifier(int argc, char * argv[])
386388
gSaltDecodedLen = 0;
387389
}
388390

389-
if (gPinCodeFile)
390-
{
391-
fclose(gPinCodeFile);
392-
}
391+
gPinCodeFile.close();
393392
return true;
394393
}

0 commit comments

Comments
 (0)