summaryrefslogtreecommitdiffstats
path: root/eurephiadm
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2010-07-08 13:50:55 +0200
committerDavid Sommerseth <dazo@users.sourceforge.net>2010-07-30 11:02:56 +0200
commit6e3665b47795dc26a1623db75d4753c80fa748b3 (patch)
tree7ad03bb2c7b1a058b02131099c81fe87f837c2e3 /eurephiadm
parentc148afb4f83604cb191c54240ef0056ec572a5be (diff)
downloadeurephia-6e3665b47795dc26a1623db75d4753c80fa748b3.tar.gz
eurephia-6e3665b47795dc26a1623db75d4753c80fa748b3.tar.xz
eurephia-6e3665b47795dc26a1623db75d4753c80fa748b3.zip
Do check the result of fgets()
If fgets() returns NULL, clear the buffer allocated for the console data.
Diffstat (limited to 'eurephiadm')
-rw-r--r--eurephiadm/get_console_input.c8
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");