Skip to content

Commit 9b1c8c5

Browse files
committed
addressed copilot comments
1 parent 165e7d5 commit 9b1c8c5

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

src/crypto/clu_crypto_setup.c

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,24 @@ int wolfCLU_setup(int argc, char** argv, char action)
348348
"-in flag was not set, please enter a string or"
349349
" file name to be encrypted: ");
350350
if (fgets(inName, sizeof(inName), stdin) == NULL) {
351-
/* Failed to read input, continue */
352-
continue;
351+
/* EOF or read error: cannot prompt further */
352+
wolfCLU_LogError("failed to read input file name");
353+
wolfCLU_freeBins(pwdKey, iv, key, NULL, NULL);
354+
if (mode != NULL)
355+
XFREE(mode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
356+
return WOLFCLU_FATAL_ERROR;
353357
}
354-
/* If no newline is present, the line was too long. */
358+
/* If no newline is present, the line was too long: flush and
359+
* re-prompt rather than proceeding with a truncated filename. */
355360
if (strchr(inName, '\n') == NULL) {
356361
int ch;
357362
do {
358363
ch = getchar();
359364
} while (ch != '\n' && ch != EOF);
360-
} else {
361-
inName[strcspn(inName, "\n")] = '\0';
365+
wolfCLU_LogError("input too long, please try again");
366+
continue;
362367
}
368+
inName[strcspn(inName, "\n")] = '\0';
363369
/* Do not accept an empty string as valid input */
364370
if (inName[0] == '\0') {
365371
continue;
@@ -415,16 +421,22 @@ int wolfCLU_setup(int argc, char** argv, char action)
415421
WOLFCLU_LOG(WOLFCLU_L0,
416422
"Please enter a name for the output file: ");
417423
if (fgets(outNameEnc, sizeof(outNameEnc), stdin) == NULL) {
418-
continue;
424+
/* EOF or read error: cannot prompt further */
425+
wolfCLU_LogError("failed to read output file name");
426+
wolfCLU_freeBins(pwdKey, iv, key, NULL, NULL);
427+
if (mode != NULL)
428+
XFREE(mode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
429+
return WOLFCLU_FATAL_ERROR;
419430
}
420431
if (strchr(outNameEnc, '\n') == NULL) {
421432
int ch;
422433
do {
423434
ch = getchar();
424435
} while (ch != '\n' && ch != EOF);
425-
} else {
426-
outNameEnc[strcspn(outNameEnc, "\n")] = '\0';
436+
wolfCLU_LogError("input too long, please try again");
437+
continue;
427438
}
439+
outNameEnc[strcspn(outNameEnc, "\n")] = '\0';
428440
if (outNameEnc[0] == '\0') {
429441
continue;
430442
}
@@ -451,16 +463,22 @@ int wolfCLU_setup(int argc, char** argv, char action)
451463
WOLFCLU_LOG(WOLFCLU_L0,
452464
"Please enter a name for the output file: ");
453465
if (fgets(outNameDec, sizeof(outNameDec), stdin) == NULL) {
454-
continue;
466+
/* EOF or read error: cannot prompt further */
467+
wolfCLU_LogError("failed to read output file name");
468+
wolfCLU_freeBins(pwdKey, iv, key, NULL, NULL);
469+
if (mode != NULL)
470+
XFREE(mode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
471+
return WOLFCLU_FATAL_ERROR;
455472
}
456473
if (strchr(outNameDec, '\n') == NULL) {
457474
int ch;
458475
do {
459476
ch = getchar();
460477
} while (ch != '\n' && ch != EOF);
461-
} else {
462-
outNameDec[strcspn(outNameDec, "\n")] = '\0';
478+
wolfCLU_LogError("input too long, please try again");
479+
continue;
463480
}
481+
outNameDec[strcspn(outNameDec, "\n")] = '\0';
464482
if (outNameDec[0] == '\0') {
465483
continue;
466484
}

tests/encrypt/enc-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ rm -f test-enc.der
187187

188188
# Regression tests for stack buffer overflow fix (scanf -> fgets)
189189

190-
# Test: -in not provided, filename supplied via stdin (inName path, L344)
190+
# Test: -in not provided, filename supplied via stdin to exercise the inName Path
191191
printf "certs/crl.der\n" | ./wolfssl enc -aes-128-cbc -out test-stdin-in.enc -k "testpass" > /dev/null 2>&1
192192
if [ $? != 0 ]; then
193193
echo "Failed: enc with stdin input (no -in flag)"

0 commit comments

Comments
 (0)