[MLton-commit] r4657

Matthew Fluet MLton@mlton.org
Sun, 18 Jun 2006 16:49:06 -0700


Avoid using functions OpenBSD deems too dangerous for mere mortals.

----------------------------------------------------------------------

D   mlton/branches/on-20050822-x86_64-branch/runtime/gc/util.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/gc/world.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/darwin.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/diskBack.unix.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/displayMem.linux.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/freebsd.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/setenv.putenv.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/solaris.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/util/read_write.h
U   mlton/branches/on-20050822-x86_64-branch/runtime/util/to-string.c

----------------------------------------------------------------------

Deleted: mlton/branches/on-20050822-x86_64-branch/runtime/gc/util.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc/util.c	2006-06-17 19:28:18 UTC (rev 4656)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/util.c	2006-06-18 23:49:04 UTC (rev 4657)
@@ -1,100 +0,0 @@
-/* Copyright (C) 2004-2005 Henry Cejtin, Matthew Fluet, Suresh
- *    Jagannathan, and Stephen Weeks.
- *
- * MLton is released under a BSD-style license.
- * See the file MLton-LICENSE for details.
- */
-
-const char* boolToString (bool b) {
-  return b ? "TRUE" : "FALSE";
-}
-
-#define BUF_SIZE 81
-char* intmaxToCommaString (intmax_t n) {
-  static char buf1[BUF_SIZE];
-  static char buf2[BUF_SIZE];
-  static char buf3[BUF_SIZE];
-  static char buf4[BUF_SIZE];
-  static char buf5[BUF_SIZE];
-  static char *bufs[] = {buf1, buf2, buf3, buf4, buf5};
-  static int bufIndex = 0;
-  static char *buf;
-  int i;
-  
-  buf = bufs[bufIndex++];
-  bufIndex %= 5;
-        
-  i = BUF_SIZE - 1;
-  buf[i--] = '\000';
-        
-  if (0 == n)
-    buf[i--] = '0';
-  else if (INTMAX_MIN == n) {
-    /* must treat INTMAX_MIN specially, because I negate stuff later */
-    switch (sizeof(intmax_t)) {
-    case 1:
-      strcpy (buf + 1, "-128");
-      break;
-    case 2:
-      strcpy (buf + 1, "-32,768");
-      break;
-    case 4:
-      strcpy (buf + 1, "-2,147,483,648");
-      break;
-    case 8:
-      strcpy (buf + 1, "-9,223,372,036,854,775,808");
-      break;
-    case 16:
-      strcpy (buf + 1, "-170,141,183,460,469,231,731,687,303,715,884,105,728");
-      break;
-    default:
-      die ("intmaxToCommaString: sizeof(intmax_t) = %zu", sizeof(intmax_t));
-      break;
-    }
-    i = 0;
-  } else {
-    intmax_t m;
-        
-    if (n > 0) 
-      m = n; 
-    else 
-      m = -n;
-        
-    while (m > 0) {
-      buf[i--] = m % 10 + '0';
-      m = m / 10;
-      if (i % 4 == 0 and m > 0) buf[i--] = ',';
-    }
-    if (n < 0) buf[i--] = '-';
-  }
-  return buf + i + 1;
-}
-
-char* uintmaxToCommaString (uintmax_t n) {
-  static char buf1[BUF_SIZE];
-  static char buf2[BUF_SIZE];
-  static char buf3[BUF_SIZE];
-  static char buf4[BUF_SIZE];
-  static char buf5[BUF_SIZE];
-  static char *bufs[] = {buf1, buf2, buf3, buf4, buf5};
-  static int bufIndex = 0;
-  static char *buf;
-  int i;
-  
-  buf = bufs[bufIndex++];
-  bufIndex %= 5;
-  
-  i = BUF_SIZE - 1;
-  buf[i--] = '\000';
-  if (0 == n)
-    buf[i--] = '0';
-  else {
-    while (n > 0) {
-      buf[i--] = n % 10 + '0';
-      n = n / 10;
-      if (i % 4 == 0 and n > 0) buf[i--] = ',';
-    }
-  }
-  return buf + i + 1;
-}
-#undef BUF_SIZE

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/gc/world.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc/world.c	2006-06-17 19:28:18 UTC (rev 4656)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/world.c	2006-06-18 23:49:04 UTC (rev 4657)
@@ -59,10 +59,10 @@
     fprintf (stderr, "saveWorldToFILE\n");
   /* Compact the heap. */
   performGC (s, 0, 0, TRUE, TRUE);
-  sprintf (buf,
-           "Heap file created by MLton.\nheap.start = "FMTPTR"\nbytesLive = %zu\n",
-           (uintptr_t)s->heap.start, 
-           s->lastMajorStatistics.bytesLive);
+  snprintf (buf, cardof(buf),
+            "Heap file created by MLton.\nheap.start = "FMTPTR"\nbytesLive = %zu\n",
+            (uintptr_t)s->heap.start, 
+            s->lastMajorStatistics.bytesLive);
   len = strlen(buf) + 1; /* +1 to get the '\000' */
   
   if (fwrite (buf, 1, len, f) != len) return -1;

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/darwin.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/darwin.c	2006-06-17 19:28:18 UTC (rev 4656)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/darwin.c	2006-06-18 23:49:04 UTC (rev 4657)
@@ -26,7 +26,7 @@
 void GC_displayMem (void) {
         static char buffer[256];
 
-        sprintf (buffer, "/usr/bin/vmmap -w -interleaved %d\n", (int)getpid ());
+        snprintf (buffer, cardof(buffer), "/usr/bin/vmmap -w -interleaved %d\n", (int)getpid ());
         (void)system (buffer);
 }
 

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/diskBack.unix.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/diskBack.unix.c	2006-06-17 19:28:18 UTC (rev 4656)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/diskBack.unix.c	2006-06-18 23:49:04 UTC (rev 4657)
@@ -4,6 +4,7 @@
   char *template;
   const char *tmpDir;
   const char *tag = "/TempFileXXXXXXXXXX";
+  size_t tmpDirLen, tagLen;
   mode_t m;
 
   tmpDir = getenv ("TMP");
@@ -12,9 +13,11 @@
     if (NULL == tmpDir)
       tmpDir = "/var/tmp";
   }
-  template = malloc_safe (strlen(tmpDir) + strlen(tag) + 1);
-  strcpy (template, tmpDir);
-  strcat (template, tag);
+  tmpDirLen = strlen(tmpDir);
+  tagLen = strlen(tag);
+  template = malloc_safe (tmpDirLen + tagLen + 1);
+  strncpy (template, tmpDir, tmpDirLen + 1);
+  strncpy (template + tmpDirLen, tag, tagLen + 1);
   m = umask(077);
   fd = mkstemp_safe (template);
   f = fdopen_safe (fd, "w+");

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/displayMem.linux.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/displayMem.linux.c	2006-06-17 19:28:18 UTC (rev 4656)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/displayMem.linux.c	2006-06-18 23:49:04 UTC (rev 4657)
@@ -1,6 +1,6 @@
 void GC_displayMem (void) {
         static char buffer[256];
 
-        sprintf (buffer, "/bin/cat /proc/%d/maps\n", (int)(getpid ()));
+        snprintf (buffer, cardof(buffer), "/bin/cat /proc/%d/maps\n", (int)(getpid ()));
         system (buffer);
 }

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/freebsd.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/freebsd.c	2006-06-17 19:28:18 UTC (rev 4656)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/freebsd.c	2006-06-18 23:49:04 UTC (rev 4657)
@@ -11,7 +11,7 @@
 void GC_displayMem () {
         static char buffer[256];
 
-        sprintf (buffer, "/bin/cat /proc/%d/map\n", (int)getpid ());
+        snprintf (buffer, cardof(buffer), "/bin/cat /proc/%d/map\n", (int)getpid ());
         (void)system (buffer);
 }
 

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/setenv.putenv.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/setenv.putenv.c	2006-06-17 19:28:18 UTC (rev 4656)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/setenv.putenv.c	2006-06-18 23:49:04 UTC (rev 4657)
@@ -2,12 +2,14 @@
  * it, since the specification of putenv is that it uses the memory for its arg.
  */
 int setenv (const char *name, const char *value, int overwrite) {
+        size_t len;
         char *b;
 
         if (!overwrite && getenv (name))
                 return 0;
 
-        b = malloc (strlen (name) + strlen (value) + 2 /* = and \000 */);
-        sprintf (b, "%s=%s", name, value);
+        len = strlen (name) + strlen (value) + 2 /* = and \000 */;
+        b = malloc (len);
+        snprintf (b, len, "%s=%s", name, value);
         return putenv (b);
 }

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/solaris.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/solaris.c	2006-06-17 19:28:18 UTC (rev 4656)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/solaris.c	2006-06-18 23:49:04 UTC (rev 4657)
@@ -66,7 +66,7 @@
 
 void GC_displayMem () {
         static char buffer[256];
-        sprintf (buffer, "pmap %d\n", (int)(getpid ()));
+        snprintf (buffer, cardof(buffer), "pmap %d\n", (int)(getpid ()));
         system (buffer);
 }
 

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/util/read_write.h
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/util/read_write.h	2006-06-17 19:28:18 UTC (rev 4656)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/util/read_write.h	2006-06-18 23:49:04 UTC (rev 4657)
@@ -54,21 +54,21 @@
 static inline void writeUint32U (FILE *f, uint32_t u) {
   static char buf[BUF_SIZE];
 
-  sprintf (buf, "%"PRIu32, u);
+  snprintf (buf, BUF_SIZE, "%"PRIu32, u);
   writeString (f, buf);
 }
 
 static inline void writeUintmaxU (FILE *f, uintmax_t u) {
   static char buf[BUF_SIZE];
 
-  sprintf (buf, "%"PRIuMAX, u);
+  snprintf (buf, BUF_SIZE, "%"PRIuMAX, u);
   writeString (f, buf);
 }
 
 static inline void writeUint32X (FILE *f, uint32_t u) {
   static char buf[BUF_SIZE];
   
-  sprintf (buf, "0x%08"PRIx32, u);
+  snprintf (buf, BUF_SIZE, "0x%08"PRIx32, u);
   writeString (f, buf);
 }
 
@@ -76,11 +76,11 @@
   static char buf[BUF_SIZE];
 
   if (sizeof(uintmax_t) == 4) {
-    sprintf (buf, "0x%08"PRIxMAX, u);
+    snprintf (buf, BUF_SIZE, "0x%08"PRIxMAX, u);
   } else if (sizeof(uintmax_t) == 8) {
-    sprintf (buf, "0x%016"PRIxMAX, u);
+    snprintf (buf, BUF_SIZE, "0x%016"PRIxMAX, u);
   } else {
-    sprintf (buf, "0x%"PRIxMAX, u);
+    snprintf (buf, BUF_SIZE, "0x%"PRIxMAX, u);
   }
   writeString (f, buf);
 }

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/util/to-string.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/util/to-string.c	2006-06-17 19:28:18 UTC (rev 4656)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/util/to-string.c	2006-06-18 23:49:04 UTC (rev 4657)
@@ -32,27 +32,29 @@
   if (0 == n)
     buf[i--] = '0';
   else if (INTMAX_MIN == n) {
+    const char* s;
     /* must treat INTMAX_MIN specially, because I negate stuff later */
     switch (sizeof(intmax_t)) {
     case 1:
-      strcpy (buf + 1, "-128");
+      s = "-128";
       break;
     case 2:
-      strcpy (buf + 1, "-32,768");
+      s = "-32,768";
       break;
     case 4:
-      strcpy (buf + 1, "-2,147,483,648");
+      s = "-2,147,483,648";
       break;
     case 8:
-      strcpy (buf + 1, "-9,223,372,036,854,775,808");
+      s = "-9,223,372,036,854,775,808";
       break;
     case 16:
-      strcpy (buf + 1, "-170,141,183,460,469,231,731,687,303,715,884,105,728");
+      s = "-170,141,183,460,469,231,731,687,303,715,884,105,728";
       break;
     default:
       die ("intmaxToCommaString: sizeof(intmax_t) = %zu", sizeof(intmax_t));
       break;
     }
+    strncpy (buf + 1, s, strlen(s) + 1);
     i = 0;
   } else {
     intmax_t m;
@@ -122,7 +124,7 @@
     amount /= factor;
     suffixIndex++;
   }
-  sprintf (buf, "%zu%s", amount, suffixs[suffixIndex]);
+  snprintf (buf, BUF_SIZE, "%zu%s", amount, suffixs[suffixIndex]);
   return buf;
 }
 #undef BUF_SIZE