about summary refs log tree commit diff
path: root/sysdeps/posix
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2002-01-31 19:43:44 +0000
committerUlrich Drepper <drepper@redhat.com>2002-01-31 19:43:44 +0000
commita204ea3607d148c7b83dec5b54496762a99699d8 (patch)
tree142c0d7a0f56df9d09330742585760dad2047ccd /sysdeps/posix
parentd6b5d570a3255d8dc80e07c3674594574cd98fe7 (diff)
downloadglibc-a204ea3607d148c7b83dec5b54496762a99699d8.tar.gz
glibc-a204ea3607d148c7b83dec5b54496762a99699d8.tar.xz
glibc-a204ea3607d148c7b83dec5b54496762a99699d8.zip
Update.
2002-01-31  Ulrich Drepper  <drepper@redhat.com>

	* sysdeps/posix/readv.c: Don't use alloca if the memory requirements
	are too high.

2002-01-31  Andreas Schwab  <schwab@suse.de>

	* sysdeps/posix/readv.c: Check for ssize_t overflow.

2002-01-31  Andreas Schwab  <schwab@suse.de>

	* sysdeps/generic/dl-sysdep.c (_dl_sysdep_start): Fix leftover
	reference to _dl_pagesize.
Diffstat (limited to 'sysdeps/posix')
-rw-r--r--sysdeps/posix/readv.c42
1 files changed, 36 insertions, 6 deletions
diff --git a/sysdeps/posix/readv.c b/sysdeps/posix/readv.c
index 6349e242eb..89fe1af7d3 100644
--- a/sysdeps/posix/readv.c
+++ b/sysdeps/posix/readv.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1992, 1996, 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1992, 1996, 1997, 2002 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -19,6 +19,9 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <sys/param.h>
 #include <sys/uio.h>
 
 /* Read data from file descriptor FD, and put the result in the
@@ -33,17 +36,41 @@ __readv (fd, vector, count)
      int count;
 {
   char *buffer;
+  char *buffer_start;
   size_t bytes;
   int bytes_read;
   int i;
+  bool use_malloc = false;
 
   /* Find the total number of bytes to be read.  */
   bytes = 0;
   for (i = 0; i < count; ++i)
-    bytes += vector[i].iov_len;
+    {
+      /* Check for ssize_t overflow.  */
+      if (SSIZE_MAX - bytes < vector[i].iov_len)
+	{
+	  errno = EINVAL;
+	  return -1;
+	}
+      bytes += vector[i].iov_len;
+    }
+
+  /* Allocate a temporary buffer to hold the data.  We should normally
+     use alloca since it's faster and does not require synchronization
+     with other threads.  But we cannot if the amount of memory
+     required is too large.  Use 512k as the limit.  */
+  if (bytes < 512 * 1024)
+    buffer = (char *) __alloca (bytes);
+  else
+    {
+      buffer = (char *) malloc (bytes);
+      if (buffer == NULL)
+	/* XXX I don't know whether it is acceptable to try reading
+	   the data in chunks.  Probably not so we just fail here.  */
+	return -1;
 
-  /* Allocate a temporary buffer to hold the data.  */
-  buffer = (char *) __alloca (bytes);
+      use_malloc = true;
+    }
 
   /* Read the data.  */
   bytes_read = __read (fd, buffer, bytes);
@@ -52,10 +79,10 @@ __readv (fd, vector, count)
 
   /* Copy the data from BUFFER into the memory specified by VECTOR.  */
   bytes = bytes_read;
+  buffer_start = buffer;
   for (i = 0; i < count; ++i)
     {
-#define	min(a, b)	((a) > (b) ? (b) : (a))
-      size_t copy = min (vector[i].iov_len, bytes);
+      size_t copy = MIN (vector[i].iov_len, bytes);
 
       (void) memcpy ((void *) vector[i].iov_base, (void *) buffer, copy);
 
@@ -65,6 +92,9 @@ __readv (fd, vector, count)
 	break;
     }
 
+  if (use_malloc)
+    free (buffer_start);
+
   return bytes_read;
 }
 #ifndef __readv