OSDN Git Service

2009-03-16 Matthias Klose <doko@ubuntu.com>
[pf3gnuchains/gcc-fork.git] / libmudflap / mf-hooks2.c
index 5a001ad..1da2261 100644 (file)
@@ -26,8 +26,8 @@ for more details.
 
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING.  If not, write to the Free
-Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.  */
+Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301, USA.  */
 
 
 #include "config.h"
@@ -41,7 +41,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #if !defined(__FreeBSD__) && !defined(__APPLE__)
 #define _POSIX_SOURCE
 #endif /* Some BSDs break <sys/socket.h> if this is defined. */
-#define _GNU_SOURCE 
+#define _GNU_SOURCE
 #define _XOPEN_SOURCE
 #define _BSD_TYPES
 #define __EXTENSIONS__
@@ -192,7 +192,7 @@ WRAPPER2(char *, strcpy, char *dest, const char *src)
 
   size_t n = strlen (src);
   TRACE ("%s\n", __PRETTY_FUNCTION__);
-  MF_VALIDATE_EXTENT(src, CLAMPADD(n, 1), __MF_CHECK_READ, "strcpy src"); 
+  MF_VALIDATE_EXTENT(src, CLAMPADD(n, 1), __MF_CHECK_READ, "strcpy src");
   MF_VALIDATE_EXTENT(dest, CLAMPADD(n, 1), __MF_CHECK_WRITE, "strcpy dest");
   return strcpy (dest, src);
 }
@@ -216,7 +216,7 @@ WRAPPER2(char *, strcat, char *dest, const char *src)
   size_t src_sz;
   TRACE ("%s\n", __PRETTY_FUNCTION__);
   dest_sz = strlen (dest);
-  src_sz = strlen (src);  
+  src_sz = strlen (src);
   MF_VALIDATE_EXTENT(src, CLAMPADD(src_sz, 1), __MF_CHECK_READ, "strcat src");
   MF_VALIDATE_EXTENT(dest, CLAMPADD(dest_sz, CLAMPADD(src_sz, 1)),
                     __MF_CHECK_WRITE, "strcat dest");
@@ -228,15 +228,15 @@ WRAPPER2(char *, strncat, char *dest, const char *src, size_t n)
 {
 
   /* nb: validating the extents (s,n) might be a mistake for two reasons.
-     
-  (1) the string s might be shorter than n chars, and n is just a 
+
+  (1) the string s might be shorter than n chars, and n is just a
   poor choice by the programmer. this is not a "true" error in the
   sense that the call to strncat would still be ok.
-  
+
   (2) we could try to compensate for case (1) by calling strlen(s) and
   using that as a bound for the extent to verify, but strlen might fall off
   the end of a non-terminated string, leading to a false positive.
-  
+
   so we will call strnlen(s,n) and use that as a bound.
 
   if strnlen returns a length beyond the end of the registered extent
@@ -265,7 +265,7 @@ WRAPPER2(int, strcmp, const char *s1, const char *s2)
   size_t s2_sz;
   TRACE ("%s\n", __PRETTY_FUNCTION__);
   s1_sz = strlen (s1);
-  s2_sz = strlen (s2);  
+  s2_sz = strlen (s2);
   MF_VALIDATE_EXTENT(s1, CLAMPADD(s1_sz, 1), __MF_CHECK_READ, "strcmp 1st arg");
   MF_VALIDATE_EXTENT(s2, CLAMPADD(s2_sz, 1), __MF_CHECK_WRITE, "strcmp 2nd arg");
   return strcmp (s1, s2);
@@ -278,7 +278,7 @@ WRAPPER2(int, strcasecmp, const char *s1, const char *s2)
   size_t s2_sz;
   TRACE ("%s\n", __PRETTY_FUNCTION__);
   s1_sz = strlen (s1);
-  s2_sz = strlen (s2);  
+  s2_sz = strlen (s2);
   MF_VALIDATE_EXTENT(s1, CLAMPADD(s1_sz, 1), __MF_CHECK_READ, "strcasecmp 1st arg");
   MF_VALIDATE_EXTENT(s2, CLAMPADD(s2_sz, 1), __MF_CHECK_READ, "strcasecmp 2nd arg");
   return strcasecmp (s1, s2);
@@ -318,7 +318,7 @@ WRAPPER2(char *, strdup, const char *s)
   size_t n = strlen (s);
   TRACE ("%s\n", __PRETTY_FUNCTION__);
   MF_VALIDATE_EXTENT(s, CLAMPADD(n,1), __MF_CHECK_READ, "strdup region");
-  result = (char *)CALL_REAL(malloc, 
+  result = (char *)CALL_REAL(malloc,
                             CLAMPADD(CLAMPADD(n,1),
                                      CLAMPADD(__mf_opts.crumple_zone,
                                               __mf_opts.crumple_zone)));
@@ -343,11 +343,11 @@ WRAPPER2(char *, strndup, const char *s, size_t n)
   MF_VALIDATE_EXTENT(s, sz, __MF_CHECK_READ, "strndup region"); /* nb: strNdup */
 
   /* note: strndup still adds a \0, even with the N limit! */
-  result = (char *)CALL_REAL(malloc, 
+  result = (char *)CALL_REAL(malloc,
                             CLAMPADD(CLAMPADD(n,1),
                                      CLAMPADD(__mf_opts.crumple_zone,
                                               __mf_opts.crumple_zone)));
-  
+
   if (UNLIKELY(! result)) return result;
 
   result += __mf_opts.crumple_zone;
@@ -393,7 +393,7 @@ WRAPPER2(char *, strstr, const char *haystack, const char *needle)
 
 
 #ifdef HAVE_MEMMEM
-WRAPPER2(void *, memmem, 
+WRAPPER2(void *, memmem,
        const void *haystack, size_t haystacklen,
        const void *needle, size_t needlelen)
 {
@@ -544,8 +544,14 @@ WRAPPER2(struct tm*, gmtime, const time_t *timep)
 /* The following indicate if the result of the corresponding function
  * should be explicitly un/registered by the wrapper
 */
+
+#ifdef __FreeBSD__
+#define MF_REGISTER_fopen              __MF_TYPE_STATIC
+#else
 #undef  MF_REGISTER_fopen
+#endif
 #define MF_RESULT_SIZE_fopen           (sizeof (FILE))
+
 #undef  MF_REGISTER_opendir
 #define MF_RESULT_SIZE_opendir         0       /* (sizeof (DIR)) */
 #undef  MF_REGISTER_readdir
@@ -1394,7 +1400,8 @@ WRAPPER2(int, getsockopt, int s, int level, int optname, void *optval,
 WRAPPER2(int, accept, int s, struct  sockaddr *addr, socklen_t *addrlen)
 {
   TRACE ("%s\n", __PRETTY_FUNCTION__);
-  MF_VALIDATE_EXTENT (addr, (size_t)*addrlen, __MF_CHECK_WRITE, "accept addr");
+  if (addr != NULL)
+    MF_VALIDATE_EXTENT (addr, (size_t)*addrlen, __MF_CHECK_WRITE, "accept addr");
   return accept (s, addr, addrlen);
 }