From ead4080e5b1259cdc9bf66bf10529439fd187e4b Mon Sep 17 00:00:00 2001 From: Aaron Jense Date: Wed, 30 Oct 2019 10:59:46 -0600 Subject: [PATCH] Error checking for fgets to remove unused return warnings --- crypto/3des/3des-file-encrypt.c | 11 +++++++---- crypto/aes/aes-file-encrypt.c | 16 ++++++++++------ crypto/camellia/camellia-encrypt.c | 12 ++++++++---- tls/client-tcp.c | 5 ++++- tls/client-tls-cacb.c | 5 ++++- tls/client-tls-callback.c | 5 ++++- tls/client-tls-ecdhe.c | 5 ++++- tls/client-tls-nonblocking.c | 5 ++++- tls/client-tls-resume.c | 10 ++++++++-- tls/client-tls.c | 5 ++++- 10 files changed, 57 insertions(+), 22 deletions(-) diff --git a/crypto/3des/3des-file-encrypt.c b/crypto/3des/3des-file-encrypt.c index e589b15c..33f9062c 100644 --- a/crypto/3des/3des-file-encrypt.c +++ b/crypto/3des/3des-file-encrypt.c @@ -250,18 +250,21 @@ int NoEcho(char* key, int size) nflags.c_lflag |= ECHONL; if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) { - printf("Error\n"); + printf("Error: tcsetattr failed to disable terminal echo\n"); return -1060; } printf("Unique Password: "); - fgets(key, size, stdin); + if (fgets(key, size, stdin) == NULL) { + printf("Error: fgets failed to retrieve secure key input\n"); + return -1070; + } key[strlen(key) - 1] = 0; /* restore terminal */ if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) { - printf("Error\n"); - return -1070; + printf("Error: tcsetattr failed to enable terminal echo\n"); + return -1080; } return 0; } diff --git a/crypto/aes/aes-file-encrypt.c b/crypto/aes/aes-file-encrypt.c index 52d891de..dd04030a 100644 --- a/crypto/aes/aes-file-encrypt.c +++ b/crypto/aes/aes-file-encrypt.c @@ -46,7 +46,7 @@ int GenerateKey(RNG* rng, byte* key, int size, byte* salt, int pad) /* stretches key */ ret = wc_PBKDF2(key, key, strlen((const char*)key), salt, SALT_SIZE, 4096, - size, WC_SHA256); + size, WC_SHA256); if (ret != 0) return -1030; @@ -182,7 +182,7 @@ int AesDecrypt(Aes* aes, byte* key, int size, FILE* inFile, FILE* outFile) /* replicates old key if keys match */ ret = wc_PBKDF2(key, key, strlen((const char*)key), salt, SALT_SIZE, 4096, - size, WC_SHA256); + size, WC_SHA256); if (ret != 0) return -1050; @@ -249,18 +249,22 @@ int NoEcho(char* key, int size) nflags.c_lflag |= ECHONL; if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) { - printf("Error\n"); + printf("Error: tcsetattr failed to disable terminal echo\n"); return -1060; } printf("Unique Password: "); - fgets(key, size, stdin); + if (fgets(key, size, stdin) == NULL) { + printf("Error: fgets failed to retrieve secure key input\n"); + return -1070; + } + key[strlen(key) - 1] = 0; /* restore terminal */ if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) { - printf("Error\n"); - return -1070; + printf("Error: tcsetattr failed to enable terminal echo\n"); + return -1080; } return 0; } diff --git a/crypto/camellia/camellia-encrypt.c b/crypto/camellia/camellia-encrypt.c index 534063f3..ba54ca16 100644 --- a/crypto/camellia/camellia-encrypt.c +++ b/crypto/camellia/camellia-encrypt.c @@ -246,18 +246,22 @@ int NoEcho(char* key, int size) nflags.c_lflag |= ECHONL; if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) { - printf("Error\n"); + printf("Error: tcsetattr failed to disable terminal echo\n"); return -1060; } printf("Unique Password: "); - fgets(key, size, stdin); + if (fgets(key, size, stdin) == NULL) { + printf("Error: fgets failed to retrieve secure key input\n"); + return -1070; + } + key[strlen(key) - 1] = 0; /* restore terminal */ if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) { - printf("Error\n"); - return -1070; + printf("Error: tcsetattr failed to enable terminal echo\n"); + return -1080; } return 0; } diff --git a/tls/client-tcp.c b/tls/client-tcp.c index 3b71ffde..75393bae 100644 --- a/tls/client-tcp.c +++ b/tls/client-tcp.c @@ -88,7 +88,10 @@ int main(int argc, char** argv) /* Get a message for the server from stdin */ printf("Message for server: "); memset(buff, 0, sizeof(buff)); - fgets(buff, sizeof(buff), stdin); + if (fgets(buff, sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + return -1; + } len = strnlen(buff, sizeof(buff)); /* Send the message to the server */ diff --git a/tls/client-tls-cacb.c b/tls/client-tls-cacb.c index 6e4d76b9..69f1eb7f 100644 --- a/tls/client-tls-cacb.c +++ b/tls/client-tls-cacb.c @@ -43,7 +43,10 @@ int ClientGreet(int sock, WOLFSSL* ssl) int ret = 0; /* variable for error checking */ printf("Message for server:\t"); - fgets(sendBuff, MAXDATASIZE, stdin); + if (fgets(sendBuff, MAXDATASIZE, stdin) == NULL) { + printf("Input error: No message for server"); + return EXIT_FAILURE; + } if (wolfSSL_write(ssl, sendBuff, strlen(sendBuff)) != strlen(sendBuff)) { /* the message is not able to send, or error trying */ diff --git a/tls/client-tls-callback.c b/tls/client-tls-callback.c index c6a16c09..d632ab8b 100644 --- a/tls/client-tls-callback.c +++ b/tls/client-tls-callback.c @@ -240,7 +240,10 @@ int main(int argc, char** argv) /* Get a message for the server from stdin */ printf("Message for server: "); memset(buff, 0, sizeof(buff)); - fgets(buff, sizeof(buff), stdin); + if (fgets(buff, sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + return -1; + } len = strnlen(buff, sizeof(buff)); /* Send the message to the server */ diff --git a/tls/client-tls-ecdhe.c b/tls/client-tls-ecdhe.c index 1e51b7ff..dcf36fd0 100644 --- a/tls/client-tls-ecdhe.c +++ b/tls/client-tls-ecdhe.c @@ -161,7 +161,10 @@ int main(int argc, char** argv) /* Get a message for the server from stdin */ printf("Message for server: "); memset(buff, 0, sizeof(buff)); - fgets(buff, sizeof(buff), stdin); + if (fgets(buff, sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + return -1; + } len = strnlen(buff, sizeof(buff)); /* Send the message to the server */ diff --git a/tls/client-tls-nonblocking.c b/tls/client-tls-nonblocking.c index 585bcf54..f1e95c06 100644 --- a/tls/client-tls-nonblocking.c +++ b/tls/client-tls-nonblocking.c @@ -150,7 +150,10 @@ int main(int argc, char** argv) /* Get a message for the server from stdin */ printf("Message for server: "); memset(buff, 0, sizeof(buff)); - fgets(buff, sizeof(buff), stdin); + if (fgets(buff, sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + return -1; + } len = strnlen(buff, sizeof(buff)); /* Send the message to the server */ diff --git a/tls/client-tls-resume.c b/tls/client-tls-resume.c index 259aa4c5..d25baeca 100644 --- a/tls/client-tls-resume.c +++ b/tls/client-tls-resume.c @@ -139,7 +139,10 @@ int main(int argc, char** argv) /* Get a message for the server from stdin */ printf("Message for server: "); memset(buff, 0, sizeof(buff)); - fgets(buff, sizeof(buff), stdin); + if (fgets(buff, sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to to get message for server\n"); + return -1; + } len = strnlen(buff, sizeof(buff)); /* Send the message to the server */ @@ -230,7 +233,10 @@ int main(int argc, char** argv) /* Get a message for the server from stdin */ printf("Message for server: "); memset(buff, 0, sizeof(buff)); - fgets(buff, sizeof(buff), stdin); + if (fgets(buff, sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + return -1; + } len = strnlen(buff, sizeof(buff)); /* Send the message to the server */ diff --git a/tls/client-tls.c b/tls/client-tls.c index a72dfada..a8aa4de0 100644 --- a/tls/client-tls.c +++ b/tls/client-tls.c @@ -136,7 +136,10 @@ int main(int argc, char** argv) /* Get a message for the server from stdin */ printf("Message for server: "); memset(buff, 0, sizeof(buff)); - fgets(buff, sizeof(buff), stdin); + if (fgets(buff, sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + return -1; + } len = strnlen(buff, sizeof(buff)); /* Send the message to the server */