Reviewers: Søren Gjesse, alexbl,

Message:
FreeBSD does not support strndup as reported by Alex (V8 issue 202).
Use the custom implementation also used on macos and win32.

Description:
Handle strndup in freebsd in the same way it is handled on other
platforms that do not support it directly.

Please review this at http://codereview.chromium.org/18585

SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/

Affected files:
   M     src/platform-freebsd.cc
   M     src/platform-macos.cc
   M     src/platform-win32.cc


Index: src/platform-freebsd.cc
===================================================================
--- src/platform-freebsd.cc     (revision 1141)
+++ src/platform-freebsd.cc     (working copy)
@@ -195,7 +195,19 @@


  char* OS::StrNDup(const char* str, size_t n) {
-  return strndup(str, n);
+  // Stupid implementation of strndup since freebsd isn't born with
+  // one.
+  size_t len = strlen(str);
+  if (len <= n) {
+    return StrDup(str);
+  }
+  char* result = new char[n+1];
+  size_t i;
+  for (i = 0; i <= n; i++) {
+    result[i] = str[i];
+  }
+  result[i] = '\0';
+  return result;
  }


Index: src/platform-win32.cc
===================================================================
--- src/platform-win32.cc       (revision 1141)
+++ src/platform-win32.cc       (working copy)
@@ -704,12 +704,14 @@
    // Stupid implementation of strndup since windows isn't born with
    // one.
    size_t len = strlen(str);
-  if (len <= n)
+  if (len <= n) {
      return StrDup(str);
+  }
    char* result = new char[n+1];
    size_t i;
-  for (i = 0; i <= n; i++)
+  for (i = 0; i <= n; i++) {
      result[i] = str[i];
+  }
    result[i] = '\0';
    return result;
  }
Index: src/platform-macos.cc
===================================================================
--- src/platform-macos.cc       (revision 1141)
+++ src/platform-macos.cc       (working copy)
@@ -199,12 +199,14 @@
    // Stupid implementation of strndup since macos isn't born with
    // one.
    size_t len = strlen(str);
-  if (len <= n)
+  if (len <= n) {
      return StrDup(str);
+  }
    char* result = new char[n+1];
    size_t i;
-  for (i = 0; i <= n; i++)
+  for (i = 0; i <= n; i++) {
      result[i] = str[i];
+  }
    result[i] = '\0';
    return result;
  }



--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to