This is the mail archive of the newlib@sources.redhat.com mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

ttyname: double the closedir, double the fun! [PATCH]


ttyname calls _closedir(dp) twice, and freeing the same pointer twice
may cause a crash.

Cheers,
Shaun

2005-07-05  Shaun Jackman  <sjackman@gmail.com>

	* newlib/libc/unix/ttyname.c (ttyname): Avoid calling _closedir
	twice for the same directory. _closedir calls free, and freeing
	the same pointer twice may cause a crash.

--- ./newlib/libc/unix/ttyname.c-	2000-08-24 15:32:38.000000000 -0700
+++ ./newlib/libc/unix/ttyname.c	2005-07-05 11:36:34.000000000 -0700
@@ -56,7 +56,6 @@
   struct dirent *dirp;
   DIR *dp;
   struct stat dsb;
-  char *rval;
 
   /* Must be a terminal. */
   if (tcgetattr (fd, &tty) < 0)
@@ -69,7 +68,7 @@
   if ((dp = _opendir (_PATH_DEV)) == NULL)
     return NULL;
 
-  for (rval = NULL; dirp = _readdir (dp);)
+  while ((dirp = _readdir (dp)) != NULL )
     {
       if (dirp->d_ino != sb.st_ino)
 	continue;
@@ -78,9 +77,8 @@
 	  sb.st_ino != dsb.st_ino)
 	continue;
       (void) _closedir (dp);
-      rval = buf;
-      break;
+      return buf;
     }
   (void) _closedir (dp);
-  return rval;
+  return NULL;
 }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]