Skip to content

Commit 7b0d922

Browse files
committed
Fix some scan-build warnings
Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent 7ec4188 commit 7b0d922

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

demos/gcm-file/gcm_filehandle.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ int gcm_filehandle( int cipher,
116116
if (direction == GCM_DECRYPT) {
117117
tot_data -= taglen;
118118
}
119-
rewind(in);
119+
fseek(in, 0, SEEK_SET);
120120
do {
121121
x = MIN(tot_data, LTC_FILE_READ_BUFSIZE);
122122
x = fread(buf, 1, x, in);
@@ -140,6 +140,10 @@ int gcm_filehandle( int cipher,
140140
}
141141

142142
if (direction == GCM_DECRYPT) {
143+
if (feof(in) || ferror(in)) {
144+
err = CRYPT_ERROR;
145+
goto LBL_CLEANBUF;
146+
}
143147
x = fread(buf, 1, taglen, in);
144148
if (x != taglen) {
145149
err = CRYPT_ERROR;
@@ -170,7 +174,7 @@ int gcm_filehandle( int cipher,
170174
#endif
171175
if(*res == 0) {
172176
x = ftell(out);
173-
rewind(out);
177+
fseek(in, 0, SEEK_SET);
174178
while((size_t)ftell(out) < x) {
175179
fwrite(buf, 1, LTC_FILE_READ_BUFSIZE, out);
176180
}

demos/tv_gen.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
/* SPDX-License-Identifier: Unlicense */
33
#include "tomcrypt_private.h"
44

5+
#define OPEN_FILE(f, o) do { \
6+
o = fopen(f, "w"); \
7+
if (o == NULL) { \
8+
perror("can't open " f); \
9+
return; \
10+
} \
11+
} while(0)
12+
513
static void hash_gen(void)
614
{
715
unsigned char md[MAXBLOCKSIZE], *buf;
816
unsigned long outlen, x, y, z;
917
FILE *out;
1018
int err;
1119

12-
out = fopen("hash_tv.txt", "w");
13-
if (out == NULL) {
14-
perror("can't open hash_tv.txt");
15-
return;
16-
}
20+
OPEN_FILE("hash_tv.txt", out);
1721

1822
fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n");
1923
for (x = 0; hash_descriptor[x].name != NULL; x++) {
@@ -52,7 +56,7 @@ static void cipher_gen(void)
5256
FILE *out;
5357
symmetric_key skey;
5458

55-
out = fopen("cipher_tv.txt", "w");
59+
OPEN_FILE("cipher_tv.txt", out);
5660

5761
fprintf(out,
5862
"Cipher Test Vectors\n\nThese are test encryptions with key of nn bytes '00 01 02 03 .. (nn-1)' and original PT of the same style.\n"
@@ -126,7 +130,7 @@ static void hmac_gen(void)
126130
FILE *out;
127131
unsigned long len;
128132

129-
out = fopen("hmac_tv.txt", "w");
133+
OPEN_FILE("hmac_tv.txt", out);
130134

131135
fprintf(out,
132136
"HMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed. The initial key is\n"
@@ -179,7 +183,7 @@ static void omac_gen(void)
179183
FILE *out;
180184
unsigned long len;
181185

182-
out = fopen("omac_tv.txt", "w");
186+
OPEN_FILE("omac_tv.txt", out);
183187

184188
fprintf(out,
185189
"OMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n"
@@ -240,7 +244,7 @@ static void pmac_gen(void)
240244
FILE *out;
241245
unsigned long len;
242246

243-
out = fopen("pmac_tv.txt", "w");
247+
OPEN_FILE("pmac_tv.txt", out);
244248

245249
fprintf(out,
246250
"PMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are PMAC'ed. The initial key is\n"
@@ -302,7 +306,7 @@ static void eax_gen(void)
302306
plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
303307
unsigned long len;
304308

305-
out = fopen("eax_tv.txt", "w");
309+
OPEN_FILE("eax_tv.txt", out);
306310
fprintf(out, "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n"
307311
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
308312
"step repeated sufficiently.\n\n");
@@ -368,7 +372,7 @@ static void ocb_gen(void)
368372
plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
369373
unsigned long len;
370374

371-
out = fopen("ocb_tv.txt", "w");
375+
OPEN_FILE("ocb_tv.txt", out);
372376
fprintf(out, "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
373377
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
374378
"step repeated sufficiently. The nonce is fixed throughout.\n\n");
@@ -437,7 +441,7 @@ static void ocb3_gen(void)
437441
plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
438442
unsigned long len;
439443

440-
out = fopen("ocb3_tv.txt", "w");
444+
OPEN_FILE("ocb3_tv.txt", out);
441445
fprintf(out, "OCB3 Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
442446
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
443447
"step repeated sufficiently. The nonce is fixed throughout. AAD is fixed to 3 bytes (ASCII) 'AAD'.\n\n");
@@ -509,7 +513,7 @@ static void ccm_gen(void)
509513
unsigned long len;
510514
const unsigned int taglen[] = {4, 6, 8, 10, 12, 14, 16};
511515

512-
out = fopen("ccm_tv.txt", "w");
516+
OPEN_FILE("ccm_tv.txt", out);
513517
fprintf(out, "CCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
514518
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
515519
"step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
@@ -579,7 +583,7 @@ static void gcm_gen(void)
579583
unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
580584
unsigned long len;
581585

582-
out = fopen("gcm_tv.txt", "w");
586+
OPEN_FILE("gcm_tv.txt", out);
583587
fprintf(out, "GCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
584588
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
585589
"step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
@@ -641,7 +645,7 @@ static void base64_gen(void)
641645
char dst[256];
642646
unsigned long x, len;
643647

644-
out = fopen("base64_tv.txt", "w");
648+
OPEN_FILE("base64_tv.txt", out);
645649
fprintf(out, "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n");
646650
for (x = 0; x <= 32; x++) {
647651
for (ch = 0; ch < x; ch++) {
@@ -666,7 +670,7 @@ static void ecc_gen(void)
666670
ecc_point *G, *R;
667671
int x;
668672

669-
out = fopen("ecc_tv.txt", "w");
673+
OPEN_FILE("ecc_tv.txt", out);
670674
fprintf(out, "ecc vectors. These are for kG for k=1,3,9,27,...,3**n until k > order of the curve outputs are <k,x,y> triplets\n\n");
671675
G = ltc_ecc_new_point();
672676
R = ltc_ecc_new_point();
@@ -713,7 +717,7 @@ static void lrw_gen(void)
713717
tweak[x] = key[x] = iv[x] = x;
714718
}
715719

716-
out = fopen("lrw_tv.txt", "w");
720+
OPEN_FILE("lrw_tv.txt", out);
717721
for (x = 16; x < (int)(sizeof(buf)); x += 16) {
718722
if ((err = lrw_start(find_cipher("aes"), iv, key, 16, tweak, 0, &lrw)) != CRYPT_OK) {
719723
fprintf(stderr, "Error starting LRW-AES: %s\n", error_to_string(err));

tests/common.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ static DIR *s_opendir(const char *path, char *mypath, unsigned long l)
103103
static int s_read_and_process(FILE *f, unsigned long sz, void *ctx, dir_iter_cb process)
104104
{
105105
int err = CRYPT_OK;
106-
void* buf = XMALLOC(sz + 1);
106+
void* buf;
107+
if (f == NULL)
108+
return CRYPT_FILE_NOTFOUND;
109+
buf = XMALLOC(sz + 1);
107110
if (buf == NULL)
108111
return CRYPT_MEM;
109112
if (fread(buf, 1, sz, f) != sz) {

0 commit comments

Comments
 (0)