@@ -346,11 +346,25 @@ int wolfCLU_setup(int argc, char** argv, char action)
346346 while (ret == 0 ) {
347347 WOLFCLU_LOG (WOLFCLU_L0 ,
348348 "-in flag was not set, please enter a string or"
349- "file name to be encrypted: " );
350- ret = (fgets (inName , sizeof (inName ), stdin ) != NULL ) ? 1 : 0 ;
351- if (ret > 0 ) {
349+ " file name to be encrypted: " );
350+ if (fgets (inName , sizeof (inName ), stdin ) == NULL ) {
351+ /* Failed to read input, continue */
352+ continue ;
353+ }
354+ /* If no newline is present, the line was too long. */
355+ if (strchr (inName , '\n' ) == NULL ) {
356+ int ch ;
357+ do {
358+ ch = getchar ();
359+ } while (ch != '\n' && ch != EOF );
360+ } else {
352361 inName [strcspn (inName , "\n" )] = '\0' ;
353362 }
363+ /* Do not accept an empty string as valid input */
364+ if (inName [0 ] == '\0' ) {
365+ continue ;
366+ }
367+ ret = 1 ;
354368 }
355369 in = inName ;
356370 WOLFCLU_LOG (WOLFCLU_L0 , "Encrypting :\"%s\"" , inName );
@@ -400,12 +414,22 @@ int wolfCLU_setup(int argc, char** argv, char action)
400414 while (ret == 0 ) {
401415 WOLFCLU_LOG (WOLFCLU_L0 ,
402416 "Please enter a name for the output file: " );
403- ret = (fgets (outNameEnc , sizeof (outNameEnc ), stdin ) != NULL )
404- ? 1 : 0 ;
405- if (ret > 0 ) {
417+ if (fgets (outNameEnc , sizeof (outNameEnc ), stdin ) == NULL ) {
418+ continue ;
419+ }
420+ if (strchr (outNameEnc , '\n' ) == NULL ) {
421+ int ch ;
422+ do {
423+ ch = getchar ();
424+ } while (ch != '\n' && ch != EOF );
425+ } else {
406426 outNameEnc [strcspn (outNameEnc , "\n" )] = '\0' ;
407427 }
408- out = (ret > 0 ) ? outNameEnc : '\0' ;
428+ if (outNameEnc [0 ] == '\0' ) {
429+ continue ;
430+ }
431+ out = outNameEnc ;
432+ ret = 1 ;
409433 }
410434 }
411435 ret = wolfCLU_encrypt (alg , mode , pwdKey , key , keySize , in , out ,
@@ -426,12 +450,22 @@ int wolfCLU_setup(int argc, char** argv, char action)
426450 while (ret == 0 ) {
427451 WOLFCLU_LOG (WOLFCLU_L0 ,
428452 "Please enter a name for the output file: " );
429- ret = (fgets (outNameDec , sizeof (outNameDec ), stdin ) != NULL )
430- ? 1 : 0 ;
431- if (ret > 0 ) {
453+ if (fgets (outNameDec , sizeof (outNameDec ), stdin ) == NULL ) {
454+ continue ;
455+ }
456+ if (strchr (outNameDec , '\n' ) == NULL ) {
457+ int ch ;
458+ do {
459+ ch = getchar ();
460+ } while (ch != '\n' && ch != EOF );
461+ } else {
432462 outNameDec [strcspn (outNameDec , "\n" )] = '\0' ;
433463 }
434- out = (ret > 0 ) ? outNameDec : '\0' ;
464+ if (outNameDec [0 ] == '\0' ) {
465+ continue ;
466+ }
467+ out = outNameDec ;
468+ ret = 1 ;
435469 }
436470 }
437471 ret = wolfCLU_decrypt (alg , mode , pwdKey , key , keySize , in , out ,
0 commit comments