diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2010-07-08 13:50:55 +0200 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2010-07-08 13:50:55 +0200 |
commit | 961b3a85ca6d2ca65360034f5c2b34d276507d6a (patch) | |
tree | 4e8f7f1374a0188dc0430eea92dd8b07db2e7033 | |
parent | 7ae14aca46d6299d2ed49640e5eb942a207b3a68 (diff) | |
download | eurephia-961b3a85ca6d2ca65360034f5c2b34d276507d6a.tar.gz eurephia-961b3a85ca6d2ca65360034f5c2b34d276507d6a.tar.xz eurephia-961b3a85ca6d2ca65360034f5c2b34d276507d6a.zip |
Do check the result of fgets()
If fgets() returns NULL, clear the buffer allocated for the console data.
-rw-r--r-- | eurephiadm/get_console_input.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/eurephiadm/get_console_input.c b/eurephiadm/get_console_input.c index e291d79..ab407f7 100644 --- a/eurephiadm/get_console_input.c +++ b/eurephiadm/get_console_input.c @@ -46,6 +46,7 @@ */ int get_console_input(char *buf, size_t len, const char *prompt, int hidden) { struct termios term_orig, term_noecho; + char *res = NULL; char *ptr; // Print prompt @@ -65,7 +66,7 @@ int get_console_input(char *buf, size_t len, const char *prompt, int hidden) { } // Read user input from stdin - fgets(buf, len, stdin); + res = fgets(buf, len, stdin); if( hidden == 1 ) { // Restore terminal to saved state @@ -73,13 +74,16 @@ int get_console_input(char *buf, size_t len, const char *prompt, int hidden) { } // Remove trailing spaces - if( buf != NULL ) { + if( res != NULL && buf != NULL ) { ptr = buf + strlen(buf) - 1; while( (ptr > buf) && ((*ptr == 0x20) || (*ptr == '\n') || (*ptr == '\r')) ) { *ptr = 0; ptr--; } ptr++; + } else { + // If nothing is read, make sure result buffer is cleared + memset(buf, 0, len); } if( hidden ) { fprintf(stdout, "\n"); |