[git commit nptl] do not save/restore errno around free() calls

Denys Vlasenko vda.linux at googlemail.com
Fri Sep 18 14:07:31 UTC 2009


commit: http://git.uclibc.org/uClibc/commit/?id=7bd16c28e51c92f58f9415ea05dda039df706d5a
branch: http://git.uclibc.org/uClibc/commit/?id=refs/heads/nptl

In any non-buggy program free() does not fail.
And when it fails in a buggy program, the failure
is usually fatal (heap corruption and segfault).

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
Signed-off-by: Austin Foxley <austinf at cetoncorp.com>
---
 libc/inet/if_index.c       |   15 ++++-----------
 libc/inet/ifaddrs.c        |    5 +----
 libc/misc/dirent/opendir.c |    9 +++++----
 librt/shm.c                |   16 ++++++----------
 4 files changed, 16 insertions(+), 29 deletions(-)

diff --git a/libc/inet/if_index.c b/libc/inet/if_index.c
index 968cd37..1c59ee2 100644
--- a/libc/inet/if_index.c
+++ b/libc/inet/if_index.c
@@ -38,14 +38,6 @@
 
 #include "netlinkaccess.h"
 
-/* Experimentally off - libc_hidden_proto(strncpy) */
-/* Experimentally off - libc_hidden_proto(strdup) */
-/* libc_hidden_proto(ioctl) */
-/* libc_hidden_proto(close) */
-#if __ASSUME_NETLINK_SUPPORT
-/* Experimentally off - libc_hidden_proto(strndup) */
-#endif
-
 extern int __opensock(void) attribute_hidden;
 
 /* libc_hidden_proto(if_nametoindex) */
@@ -65,14 +57,15 @@ if_nametoindex(const char* ifname)
   strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
   if (ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
     {
-      int saved_errno = errno;
+      // close never fails here, fd is just a unconnected socket
+      //int saved_errno = errno;
 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
       close_not_cancel_no_status(fd);
 #else
       close(fd);
 #endif
-      if (saved_errno == EINVAL)
-	__set_errno(ENOSYS);
+      //if (saved_errno == EINVAL)
+      //  __set_errno(ENOSYS);
       return 0;
     }
 
diff --git a/libc/inet/ifaddrs.c b/libc/inet/ifaddrs.c
index 1d54d51..77ca7ce 100644
--- a/libc/inet/ifaddrs.c
+++ b/libc/inet/ifaddrs.c
@@ -81,7 +81,6 @@ void
 __netlink_free_handle (struct netlink_handle *h)
 {
   struct netlink_res *ptr;
-  int saved_errno = errno;
 
   ptr = h->nlm_list;
   while (ptr != NULL)
@@ -89,11 +88,9 @@ __netlink_free_handle (struct netlink_handle *h)
       struct netlink_res *tmpptr;
 
       tmpptr = ptr->next;
-      free (ptr);
+      free (ptr); /* doesn't affect errno */
       ptr = tmpptr;
     }
-
-  __set_errno (saved_errno);
 }
 
 
diff --git a/libc/misc/dirent/opendir.c b/libc/misc/dirent/opendir.c
index 29e28b1..cbbdec9 100644
--- a/libc/misc/dirent/opendir.c
+++ b/libc/misc/dirent/opendir.c
@@ -96,20 +96,21 @@ DIR *opendir(const char *name)
 #endif
 	if (fd < 0)
 		return NULL;
-
 	/* Note: we should check to make sure that between the stat() and open()
 	 * call, 'name' didnt change on us, but that's only if O_DIRECTORY isnt
 	 * defined and since Linux has supported it for like ever, i'm not going
 	 * to worry about it right now (if ever). */
+
 	if (fstat(fd, &statbuf) < 0) {
-		int saved_errno;
-		saved_errno = errno;
+		// this close() never fails
+		//int saved_errno;
+		//saved_errno = errno;
 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
 		close_not_cancel_no_status(fd);
 #else
 		close(fd);
 #endif
-		__set_errno(saved_errno);
+		//__set_errno(saved_errno);
 		return NULL;
 	}
 
diff --git a/librt/shm.c b/librt/shm.c
index f0a9740..dd21324 100644
--- a/librt/shm.c
+++ b/librt/shm.c
@@ -25,8 +25,8 @@
  * Returns a malloc'ed buffer containing the OS specific path
  * to the shm filename or NULL upon failure.
  */
-static __attribute_noinline__ char* get_shm_name(const char*name) __nonnull((1));
-static char* get_shm_name(const char*name)
+static __attribute_noinline__ char* get_shm_name(const char *name) __nonnull((1));
+static char* get_shm_name(const char *name)
 {
 	char *path;
 	int i;
@@ -57,7 +57,7 @@ static char* get_shm_name(const char*name)
 
 int shm_open(const char *name, int oflag, mode_t mode)
 {
-	int fd, old_errno;
+	int fd;
 	char *shm_name = get_shm_name(name);
 
 	/* Stripped multiple '/' from start; may have set errno properly */
@@ -83,23 +83,19 @@ int shm_open(const char *name, int oflag, mode_t mode)
 		//}
 	}
 #endif
-	old_errno = errno;
-	free(shm_name);
-	errno = old_errno;
+	free(shm_name); /* doesn't affect errno */
 	return fd;
 }
 
 int shm_unlink(const char *name)
 {
 	char *shm_name = get_shm_name(name);
-	int ret, old_errno;
+	int ret;
 
 	/* Stripped multiple '/' from start; may have set errno properly */
 	if (shm_name == NULL)
 		return -1;
 	ret = unlink(shm_name);
-	old_errno = errno;
-	free(shm_name);
-	errno = old_errno;
+	free(shm_name); /* doesn't affect errno */
 	return ret;
 }
-- 
1.6.3.3



More information about the uClibc-cvs mailing list