Browse Source

Fix possible segmentations when istream==NULL

git-svn-id: https://klish.googlecode.com/svn/trunk@577 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 12 years ago
parent
commit
8d8fe690ca
2 changed files with 25 additions and 4 deletions
  1. 13 4
      tinyrl/tinyrl.c
  2. 12 0
      tinyrl/vt100/vt100.c

+ 13 - 4
tinyrl/tinyrl.c

@@ -63,9 +63,12 @@ static unsigned utf8_nsyms(tinyrl_t * this, const char *str, unsigned num)
 static void tty_set_raw_mode(tinyrl_t * this)
 {
 	struct termios new_termios;
-	int fd = fileno(tinyrl_vt100__get_istream(this->term));
+	int fd;
 	int status;
 
+	if (!tinyrl_vt100__get_istream(this->term))
+		return;
+	fd = fileno(tinyrl_vt100__get_istream(this->term));
 	status = tcgetattr(fd, &this->default_termios);
 	if (-1 != status) {
 		status = tcgetattr(fd, &new_termios);
@@ -84,8 +87,11 @@ static void tty_set_raw_mode(tinyrl_t * this)
 /*----------------------------------------------------------------------- */
 static void tty_restore_mode(const tinyrl_t * this)
 {
-	int fd = fileno(tinyrl_vt100__get_istream(this->term));
+	int fd;
 
+	if (!tinyrl_vt100__get_istream(this->term))
+		return;
+	fd = fileno(tinyrl_vt100__get_istream(this->term));
 	/* Do the mode switch */
 	(void)tcsetattr(fd, TCSADRAIN, &this->default_termios);
 }
@@ -743,7 +749,7 @@ static char *internal_readline(tinyrl_t * this,
 			tmp = lub_string_dup(str);
 			s = internal_insertline(this, tmp);
 		} else {
-			while ((sizeof(buffer) == len) &&
+			while (istream && (sizeof(buffer) == len) &&
 				(s = fgets(buffer, sizeof(buffer), istream))) {
 				s = internal_insertline(this, buffer);
 				len = strlen(buffer) + 1; /* account for the '\0' */
@@ -1268,7 +1274,10 @@ void tinyrl_disable_echo(tinyrl_t * this, char echo_char)
 void tinyrl__set_istream(tinyrl_t * this, FILE * istream)
 {
 	tinyrl_vt100__set_istream(this->term, istream);
-	this->isatty = isatty(fileno(istream)) ? BOOL_TRUE : BOOL_FALSE;
+	if (istream)
+		this->isatty = isatty(fileno(istream)) ? BOOL_TRUE : BOOL_FALSE;
+	else
+		this->isatty = BOOL_FALSE;
 }
 
 /*-------------------------------------------------------- */

+ 12 - 0
tinyrl/vt100/vt100.c

@@ -58,6 +58,10 @@ tinyrl_vt100_escape_t tinyrl_vt100_escape_decode(const tinyrl_vt100_t * this)
 	char sequence[10], *p = sequence;
 	int c;
 	unsigned i;
+
+	if (!this->istream)
+		return tinyrl_vt100_UNKNOWN;
+
 	/* before the while loop, set the input as non-blocking */
 	_tinyrl_vt100_setInputNonBlocking(this);
 
@@ -138,6 +142,8 @@ int tinyrl_vt100_getchar(const tinyrl_vt100_t *this)
 	int retval;
 	ssize_t res;
 
+	if (!this->istream)
+		return VT100_ERR;
 	istream_fd = fileno(this->istream);
 
 	/* Just wait for the input if no timeout */
@@ -186,6 +192,8 @@ int tinyrl_vt100_oflush(const tinyrl_vt100_t * this)
 /*-------------------------------------------------------- */
 int tinyrl_vt100_ierror(const tinyrl_vt100_t * this)
 {
+	if (!this->istream)
+		return 0;
 	return ferror(this->istream);
 }
 
@@ -200,12 +208,16 @@ int tinyrl_vt100_oerror(const tinyrl_vt100_t * this)
 /*-------------------------------------------------------- */
 int tinyrl_vt100_ieof(const tinyrl_vt100_t * this)
 {
+	if (!this->istream)
+		return 0;
 	return feof(this->istream);
 }
 
 /*-------------------------------------------------------- */
 int tinyrl_vt100_eof(const tinyrl_vt100_t * this)
 {
+	if (!this->istream)
+		return 0;
 	return feof(this->istream);
 }