[RFC,v0,04/10] device_tree: get_prop(): memdup returned properties

Submitted by Peter A. G. Crosthwaite on Sept. 17, 2012, 9:02 a.m.

Details

Message ID ad2950cac0fff79c86bbbaa2c5c8deb729bfce27.1347871922.git.peter.crosthwaite@petalogix.com
State New
Headers show

Commit Message

Peter A. G. Crosthwaite Sept. 17, 2012, 9:02 a.m.
getprop currently return a pointer into the device tree itself. These pointers
will be corrupted if the device tree is written to in anyway. To make getprop()
safe, duplicate the property so that the value doesnt change over the lifetime
of the returned pointer.

Signed-off-by: Peter A. G. Crosthwaite <peter.crosthwaite@petalogix.com>
---
 device_tree.c |   11 +++++++----
 device_tree.h |    2 +-
 2 files changed, 8 insertions(+), 5 deletions(-)

Patch hide | download patch | download mbox

diff --git a/device_tree.c b/device_tree.c
index 7edbbff..3e22286 100644
--- a/device_tree.c
+++ b/device_tree.c
@@ -178,7 +178,7 @@  int qemu_devtree_setprop_string(void *fdt, const char *node_path,
     return r;
 }
 
-const void *qemu_devtree_getprop(void *fdt, const char *node_path,
+void *qemu_devtree_getprop(void *fdt, const char *node_path,
                                  const char *property, int *lenp,
                                  bool inherit, Error **errp)
 {
@@ -200,7 +200,7 @@  const void *qemu_devtree_getprop(void *fdt, const char *node_path,
         error_set(errp, QERR_UNDEFINED_ERROR);
         return NULL;
     }
-    return r;
+    return g_memdup(r, *lenp);
 }
 
 uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
@@ -208,7 +208,8 @@  uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
                                    bool inherit, Error **errp)
 {
     int len;
-    const uint32_t *p = qemu_devtree_getprop(fdt, node_path, property, &len,
+    uint32_t ret;
+    uint32_t *p = qemu_devtree_getprop(fdt, node_path, property, &len,
                                                                 inherit, errp);
     if (errp && *errp) {
         return 0;
@@ -221,7 +222,9 @@  uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
         error_set(errp, QERR_UNDEFINED_ERROR);
         return 0;
     }
-    return be32_to_cpu(p[offset]);
+    ret = be32_to_cpu(p[offset]);
+    g_free(p);
+    return ret;
 }
 
 uint32_t qemu_devtree_get_phandle(void *fdt, const char *path)
diff --git a/device_tree.h b/device_tree.h
index c3f3b28..2bc188a 100644
--- a/device_tree.h
+++ b/device_tree.h
@@ -31,7 +31,7 @@  int qemu_devtree_setprop_string(void *fdt, const char *node_path,
 int qemu_devtree_setprop_phandle(void *fdt, const char *node_path,
                                  const char *property,
                                  const char *target_node_path);
-const void *qemu_devtree_getprop(void *fdt, const char *node_path,
+void *qemu_devtree_getprop(void *fdt, const char *node_path,
                                  const char *property, int *lenp,
                                  bool inherit, Error **errp);
 uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,