libufdt: avoid to strdup() if possible

Memory allocation is slow in some platform. Uses stack to avoid
malloc/free if possible.

Also removed dto_strdup(). Doesn't need to implement it anymore.

Bug: 32969430
Test: ./tests/run_tests.sh
Change-Id: I68eae8fcf05e722089879ba3e95c55b136bb31ed
diff --git a/sysdeps/include/libufdt_sysdeps.h b/sysdeps/include/libufdt_sysdeps.h
index fe98d2a..1e1ea6d 100644
--- a/sysdeps/include/libufdt_sysdeps.h
+++ b/sysdeps/include/libufdt_sysdeps.h
@@ -56,8 +56,6 @@
 
 void dto_free(void *ptr);
 
-char *dto_strdup(const char *s);
-
 char *dto_strchr(const char *s, int c);
 
 unsigned long int dto_strtoul(const char *nptr, char **endptr, int base);
diff --git a/sysdeps/libufdt_sysdeps_posix.c b/sysdeps/libufdt_sysdeps_posix.c
index d33f6e1..91b4e47 100644
--- a/sysdeps/libufdt_sysdeps_posix.c
+++ b/sysdeps/libufdt_sysdeps_posix.c
@@ -41,8 +41,6 @@
 
 void dto_free(void *ptr) { free(ptr); }
 
-char *dto_strdup(const char *s) { return strdup(s); }
-
 char *dto_strchr(const char *s, int c) { return strchr(s, c); }
 
 unsigned long int dto_strtoul(const char *nptr, char **endptr, int base) {
diff --git a/sysdeps/libufdt_sysdeps_vendor.c b/sysdeps/libufdt_sysdeps_vendor.c
index 12e7695..235d995 100644
--- a/sysdeps/libufdt_sysdeps_vendor.c
+++ b/sysdeps/libufdt_sysdeps_vendor.c
@@ -184,8 +184,6 @@
 
 void dto_free(void *ptr) { free(ptr); }
 
-char *dto_strdup(const char *s) { return strdup(s); }
-
 char *dto_strchr(const char *s, int c) { return strchr(s, c); }
 
 unsigned long int dto_strtoul(const char *nptr, char **endptr, int base) {
diff --git a/ufdt_overlay.c b/ufdt_overlay.c
index 1c6b15e..7ffb8c1 100644
--- a/ufdt_overlay.c
+++ b/ufdt_overlay.c
@@ -131,12 +131,18 @@
   char *path, *prop_ptr, *offset_ptr, *end_ptr;
   int prop_offset, prop_len;
   const char *prop_data;
+  char path_buf[1024];
+  char *path_mem = NULL;
 
-  /*
-   * TODO(akaineko): Keep track of substring lengths so we don't have to
-   * dto_malloc a copy and split it up.
-   */
-  path = dto_strdup(fixup);
+  size_t fixup_len = strlen(fixup) + 1;
+  if (fixup_len > sizeof(path_buf)) {
+    path_mem = dto_malloc(fixup_len);
+    path = path_mem;
+  } else {
+    path = path_buf;
+  }
+  dto_memcpy(path, fixup, fixup_len);
+
   prop_ptr = dto_strchr(path, ':');
   if (prop_ptr == NULL) {
     dto_error("Missing property part in '%s'\n", path);
@@ -182,11 +188,11 @@
     goto fail;
   }
 
-  dto_free(path);
+  if (path_mem) dto_free(path_mem);
   return (char *)prop_data + prop_offset;
 
 fail:
-  dto_free(path);
+  if (path_mem) dto_free(path_mem);
   return NULL;
 }