Skip to content

Commit 670d43a

Browse files
authored
Merge pull request #186 from JacobBarthelmeh/scan_build
resolve scan-build report
2 parents 76da2d9 + a194e44 commit 670d43a

File tree

8 files changed

+129
-37
lines changed

8 files changed

+129
-37
lines changed

src/certgen/clu_certgen_ed25519.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ int make_self_signed_ed25519_certificate(char* keyPath, char* certOut)
5757
keyFileSz = (int)XFTELL(keyFile);
5858
keyBuf = (byte*)XMALLOC(keyFileSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
5959
if (keyBuf == NULL) {
60+
XFCLOSE(keyFile);
6061
return MEMORY_E;
6162
}
6263
if (XFSEEK(keyFile, 0, SEEK_SET) != 0 || (int)XFREAD(keyBuf, 1, keyFileSz, keyFile) != keyFileSz) {

src/client/client.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ static WC_INLINE void clu_tcp_connect(SOCKET_T* sockfd, const char* ip,
354354
clu_build_addr(NULL, &ipv6, ip, port, udp, sctp);
355355
clu_tcp_socket(sockfd, udp, sctp, isIpv6);
356356
if (!udp) {
357+
if (*sockfd < 0)
358+
err_sys_with_errno("tcp bad socket");
359+
357360
if (connect(*sockfd, (const struct sockaddr*)&ipv6, sizeof(ipv6))
358361
!= 0)
359362
err_sys_with_errno("ipv6 tcp connect failed");
@@ -367,6 +370,9 @@ static WC_INLINE void clu_tcp_connect(SOCKET_T* sockfd, const char* ip,
367370
clu_tcp_socket(sockfd, udp, sctp, isIpv6);
368371

369372
if (!udp) {
373+
if (*sockfd < 0)
374+
err_sys_with_errno("tcp bad socket");
375+
370376
if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr))
371377
!= 0)
372378
err_sys_with_errno("tcp connect failed");
@@ -3355,8 +3361,8 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
33553361

33563362
#if defined(WOLFSSL_TRUST_PEER_CERT) && !defined(NO_FILESYSTEM)
33573363
if (trustCert) {
3358-
if ((ret = wolfSSL_CTX_trust_peer_cert(ctx, trustCert,
3359-
WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) {
3364+
if (wolfSSL_CTX_trust_peer_cert(ctx, trustCert,
3365+
WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
33603366
wolfSSL_CTX_free(ctx); ctx = NULL;
33613367
err_sys("can't load trusted peer cert file");
33623368
}

src/crypto/clu_decrypt.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,26 @@ int wolfCLU_decrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
145145
*/
146146
while (length > 0 && ret == 0) {
147147
/* Read in 1kB */
148-
if (ret == 0 &&
149-
(ret = (int)XFREAD(input, 1, MAX_LEN, inFile)) != MAX_LEN) {
148+
if (ret == 0) {
150149
if (feof(inFile)) {
151-
tempMax = ret;
152-
ret = 0; /* success */
150+
wolfCLU_LogError("Unexpected end of the file.");
151+
ret = FREAD_ERROR;
153152
}
154-
else {
155-
wolfCLU_LogError("Input file does not exist.");
153+
else if (ferror(inFile)) {
154+
wolfCLU_LogError("File stream in error state");
156155
ret = FREAD_ERROR;
157156
}
157+
else {
158+
ret = (int)XFREAD(input, 1, MAX_LEN, inFile);
159+
if ((ret > 0 && ret != MAX_LEN) || feof(inFile)) {
160+
tempMax = ret;
161+
ret = 0; /* success */
162+
}
163+
else {
164+
wolfCLU_LogError("Input file does not exist.");
165+
ret = FREAD_ERROR;
166+
}
167+
}
158168
}
159169

160170
#ifdef HAVE_CAMELLIA

src/crypto/clu_encrypt.c

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,15 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
7676

7777
/* open the file to write */
7878
tempInFile = XFOPEN(in, "wb");
79-
XFWRITE(userInputBuffer, 1, inputLength, tempInFile);
80-
XFCLOSE(tempInFile);
79+
if (tempInFile == NULL) {
80+
wolfCLU_LogError("unable to open file %s", in);
81+
XFREE(userInputBuffer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
82+
return BAD_FUNC_ARG;
83+
}
84+
else {
85+
XFWRITE(userInputBuffer, 1, inputLength, tempInFile);
86+
XFCLOSE(tempInFile);
87+
}
8188

8289
/* free buffer */
8390
XFREE(userInputBuffer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
@@ -121,13 +128,15 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
121128
ret = wc_RNG_GenerateBlock(&rng, iv, block);
122129

123130
if (ret != 0) {
131+
XFCLOSE(inFile);
124132
return ret;
125133
}
126134

127135
/* stretches pwdKey to fit size based on wolfCLU_getAlgo() */
128136
ret = wolfCLU_genKey_PWDBASED(&rng, pwdKey, size, salt, padCounter);
129137
if (ret != WOLFCLU_SUCCESS) {
130138
wolfCLU_LogError("failed to set pwdKey.");
139+
XFCLOSE(inFile);
131140
return ret;
132141
}
133142
/* move the generated pwdKey to "key" for encrypting */
@@ -140,6 +149,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
140149
outFile = XFOPEN(out, "wb");
141150
if (outFile == NULL) {
142151
wolfCLU_LogError("unable to open output file %s", out);
152+
XFCLOSE(inFile);
143153
return WOLFCLU_FATAL_ERROR;
144154
}
145155
XFWRITE(salt, 1, SALT_SIZE, outFile);
@@ -148,21 +158,29 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
148158

149159
/* MALLOC 1kB buffers */
150160
input = (byte*) XMALLOC(MAX_LEN, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
151-
if (input == NULL)
161+
if (input == NULL) {
162+
XFCLOSE(inFile);
152163
return MEMORY_E;
164+
}
153165
output = (byte*) XMALLOC(MAX_LEN, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
154166
if (output == NULL) {
167+
XFCLOSE(inFile);
155168
wolfCLU_freeBins(input, NULL, NULL, NULL, NULL);
156169
return MEMORY_E;
157170
}
158171

159172
/* loop, encrypt 1kB at a time till length <= 0 */
160173
while (length > 0) {
161174
/* Read in 1kB to input[] */
162-
if (inputHex == 1)
163-
ret = (int) fread(inputString, 1, MAX_LEN, inFile);
164-
else
165-
ret = (int) fread(input, 1, MAX_LEN, inFile);
175+
if (feof(inFile)) {
176+
ret = 0;
177+
}
178+
else {
179+
if (inputHex == 1)
180+
ret = (int) fread(inputString, 1, MAX_LEN, inFile);
181+
else
182+
ret = (int) fread(input, 1, MAX_LEN, inFile);
183+
}
166184

167185
if (ret != MAX_LEN) {
168186
/* check for end of file */
@@ -178,6 +196,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
178196
if (hexRet != WOLFCLU_SUCCESS) {
179197
wolfCLU_LogError("failed during conversion of input,"
180198
" ret = %d", hexRet);
199+
XFCLOSE(inFile);
181200
return hexRet;
182201
}
183202
}/* end hex or ascii */
@@ -191,6 +210,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
191210
}
192211
else { /* otherwise we got a file read error */
193212
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
213+
XFCLOSE(inFile);
194214
return FREAD_ERROR;
195215
}/* End feof check */
196216
}/* End fread check */
@@ -200,6 +220,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
200220
alg == WOLFCLU_CAMELLIA256CBC) {
201221
ret = wc_CamelliaSetKey(&camellia, key, block, iv);
202222
if (ret != 0) {
223+
XFCLOSE(inFile);
203224
wolfCLU_LogError("CamelliaSetKey failed.");
204225
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
205226
return ret;
@@ -208,6 +229,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
208229
wc_CamelliaCbcEncrypt(&camellia, output, input, tempMax);
209230
}
210231
else {
232+
XFCLOSE(inFile);
211233
wolfCLU_LogError("Incompatible mode while using Camellia.");
212234
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
213235
return FATAL_ERROR;
@@ -233,14 +255,25 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
233255

234256
/* Open the outFile in append mode */
235257
outFile = XFOPEN(out, "ab");
258+
if (outFile == NULL) {
259+
XFCLOSE(inFile);
260+
wolfCLU_LogError("failed to open file.");
261+
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
262+
return FWRITE_ERROR;
263+
}
264+
236265
ret = (int)XFWRITE(output, 1, tempMax, outFile);
237266

238267
if (ferror(outFile)) {
268+
XFCLOSE(outFile);
269+
XFCLOSE(inFile);
239270
wolfCLU_LogError("failed to write to file.");
240271
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
241272
return FWRITE_ERROR;
242273
}
243274
if (ret > MAX_LEN) {
275+
XFCLOSE(outFile);
276+
XFCLOSE(inFile);
244277
wolfCLU_LogError("Wrote too much to file.");
245278
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
246279
return FWRITE_ERROR;

src/genkey/clu_genkey.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ int wolfCLU_genKey_ED25519(WC_RNG* rng, char* fOutNm, int directive, int format)
8989
ret = WC_KEY_SIZE_E;
9090

9191
/* export keys to buffers */
92-
ret = wc_ed25519_export_key(&edKeyOut, privKeyBuf, &privKeySz,
92+
if (ret == 0)
93+
ret = wc_ed25519_export_key(&edKeyOut, privKeyBuf, &privKeySz,
9394
pubKeyBuf, &pubKeySz);
9495
}
9596

src/sign-verify/clu_sign.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,24 @@ int wolfCLU_sign_data_rsa(byte* data, char* out, word32 dataSz, char* privKey,
263263
if (ret >= 0) {
264264
XFILE s;
265265
s = XFOPEN(out, "wb");
266-
XFWRITE(outBuf, 1, outBufSz, s);
267-
XFCLOSE(s);
266+
if (s == NULL) {
267+
wolfCLU_LogError("Failed to open output file");
268+
ret = BAD_FUNC_ARG;
269+
}
270+
else {
271+
XFWRITE(outBuf, 1, outBufSz, s);
272+
XFCLOSE(s);
273+
}
268274
}
269275
else {
270276
wolfCLU_LogError("Failed to sign data with RSA private key.\nRET: %d", ret);
271277
}
272278
}
273279

274280
/* cleanup allocated resources */
275-
XFCLOSE(privKeyFile);
281+
if (privKeyFile != NULL) {
282+
XFCLOSE(privKeyFile);
283+
}
276284

277285
if (keyBuf!= NULL) {
278286
XFREE(keyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
@@ -388,16 +396,24 @@ int wolfCLU_sign_data_ecc(byte* data, char* out, word32 fSz, char* privKey,
388396
if (ret >= 0) {
389397
XFILE s;
390398
s = XFOPEN(out, "wb");
391-
XFWRITE(outBuf, 1, outLen, s);
392-
XFCLOSE(s);
399+
if (s == NULL) {
400+
wolfCLU_LogError("Failed to open file");
401+
ret = BAD_FUNC_ARG;
402+
}
403+
else {
404+
XFWRITE(outBuf, 1, outLen, s);
405+
XFCLOSE(s);
406+
}
393407
}
394408
else {
395409
wolfCLU_LogError("Failed to sign data with Ecc private key.\nRET: %d", ret);
396410
}
397411
}
398412

399413
/* cleanup allocated resources */
400-
XFCLOSE(privKeyFile);
414+
if (privKeyFile != NULL) {
415+
XFCLOSE(privKeyFile);
416+
}
401417

402418
if (keyBuf!= NULL) {
403419
XFREE(keyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
@@ -530,16 +546,24 @@ int wolfCLU_sign_data_ed25519 (byte* data, char* out, word32 fSz, char* privKey,
530546
if (ret >= 0) {
531547
XFILE s;
532548
s = XFOPEN(out, "wb");
533-
XFWRITE(outBuf, 1, outBufSz, s);
534-
XFCLOSE(s);
549+
if (s == NULL) {
550+
wolfCLU_LogError("Failed to open file");
551+
ret = BAD_FUNC_ARG;
552+
}
553+
else {
554+
XFWRITE(outBuf, 1, outBufSz, s);
555+
XFCLOSE(s);
556+
}
535557
}
536558
else {
537559
wolfCLU_LogError("Failed to sign data with ED25519 private key.\nRET: %d", ret);
538560
}
539561
}
540562

541563
/* cleanup allocated resources */
542-
XFCLOSE(privKeyFile);
564+
if (privKeyFile != NULL) {
565+
XFCLOSE(privKeyFile);
566+
}
543567

544568
if (keyBuf!= NULL) {
545569
XFREE(keyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);

src/sign-verify/clu_verify.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ int wolfCLU_verify_signature_rsa(byte* sig, char* out, int sigSz, char* keyPath,
450450
}
451451

452452
/* Cleanup allocated resources */
453-
XFCLOSE(keyPathFile);
453+
if (keyPathFile != NULL) {
454+
XFCLOSE(keyPathFile);
455+
}
454456

455457
if (outBuf != NULL) {
456458
XFREE(outBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
@@ -578,7 +580,9 @@ int wolfCLU_verify_signature_ecc(byte* sig, int sigSz, byte* hash, int hashSz,
578580
}
579581

580582
/* cleanup allocated resources */
581-
XFCLOSE(keyPathFile);
583+
if (keyPathFile != NULL) {
584+
XFCLOSE(keyPathFile);
585+
}
582586

583587
if (outBuf != NULL) {
584588
XFREE(outBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
@@ -711,7 +715,9 @@ int wolfCLU_verify_signature_ed25519(byte* sig, int sigSz,
711715
}
712716

713717
/* cleanup allocated resources */
714-
XFCLOSE(keyPathFile);
718+
if (keyPathFile != NULL) {
719+
XFCLOSE(keyPathFile);
720+
}
715721

716722
if (keyBuf != NULL) {
717723
XFREE(keyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);

src/tools/clu_base64.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,17 @@ int wolfCLU_Base64Setup(int argc, char** argv)
163163
/* Try other types if PRIVATEKEY_TYPE fails */
164164
ret = wc_PemToDer(input, (long)inputSz, CERT_TYPE,
165165
&der, NULL, NULL, NULL);
166-
if (ret < 0) {
167-
ret = wc_PemToDer(input, (long)inputSz, CERTREQ_TYPE,
166+
}
167+
168+
if (ret < 0) {
169+
ret = wc_PemToDer(input, (long)inputSz, PKCS7_TYPE,
168170
&der, NULL, NULL, NULL);
169-
if (ret < 0) {
170-
wolfCLU_LogError("PEM to DER conversion failed: %d",
171-
ret);
172-
ret = WOLFCLU_FATAL_ERROR;
173-
}
174-
}
171+
}
172+
173+
/* If all PEM to DER attempts failed then set error */
174+
if (ret < 0) {
175+
wolfCLU_LogError("PEM to DER conversion failed: %d", ret);
176+
ret = WOLFCLU_FATAL_ERROR;
175177
}
176178

177179
if (ret == 0) {
@@ -247,6 +249,9 @@ int wolfCLU_Base64Setup(int argc, char** argv)
247249
wolfCLU_LogError("Base64 encode failed: %d", ret);
248250
ret = WOLFCLU_FATAL_ERROR;
249251
}
252+
else {
253+
ret = WOLFCLU_SUCCESS;
254+
}
250255
}
251256
}
252257

@@ -257,6 +262,9 @@ int wolfCLU_Base64Setup(int argc, char** argv)
257262
wolfCLU_LogError("Failed to write output data: %d", ret);
258263
ret = WOLFCLU_FATAL_ERROR;
259264
}
265+
else {
266+
ret = WOLFCLU_SUCCESS;
267+
}
260268
}
261269
else if (ret == WOLFCLU_SUCCESS) {
262270
/* Write to stdout */
@@ -268,6 +276,9 @@ int wolfCLU_Base64Setup(int argc, char** argv)
268276
wolfCLU_LogError("Failed to write to stdout: %d", ret);
269277
ret = WOLFCLU_FATAL_ERROR;
270278
}
279+
else {
280+
ret = WOLFCLU_SUCCESS;
281+
}
271282
}
272283
else {
273284
wolfCLU_LogError("Failed to create stdout BIO");
@@ -294,7 +305,7 @@ int wolfCLU_Base64Setup(int argc, char** argv)
294305
wolfSSL_BIO_free(bioOut);
295306
}
296307

297-
return WOLFCLU_SUCCESS;
308+
return ret;
298309
#else
299310
(void)argc;
300311
(void)argv;

0 commit comments

Comments
 (0)