Skip to contentSkip to conversation

coverity: Model g_memdup()

PATCH In review Markus Armbruster 2 participants 2 messages Nov 30, 2015 – Dec 1, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 coverity: Model g_memdup()

We model all the non-deprecated memory allocation functions from
https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html
except for g_memdup(), g_clear_pointer(), g_steal_pointer(). We don't use the latter two. Model the former.

Coverity now reports an OVERRUN
vl.c:2317: alloc_strlen: Allocating insufficient memory for the terminating null of the string. Correct, but we omit the terminating null intentionally there.

---
 scripts/coverity-model.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
scripts/coverity-model.c +17 -0
index 617f67d..e1d5f45 100644
@@ -236,6 +236,23 @@ void *g_try_realloc(void *ptr, size_t size)
     return g_try_realloc_n(ptr, 1, size);
 }
 
+/* Other memory allocation functions */
+
+void *g_memdup(const void *ptr, unsigned size)
+{
+    unsigned char *dup;
+    unsigned i;
+
+    if (!ptr) {
+        return NULL;
+    }
+
+    dup = g_malloc(size);
+    for (i = 0; i < size; i++)
+        dup[i] = ((unsigned char *)ptr)[i];
+    return dup;
+}
+
 /*
  * GLib string allocation functions
  */
Signature · 1 line
2.4.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 33 lines · Markus Armbruster

We model all the non-deprecated memory allocation functions from
https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html
except for g_memdup(), g_clear_pointer(), g_steal_pointer(). We don't use the latter two. Model the former.

Coverity now reports an OVERRUN
vl.c:2317: alloc_strlen: Allocating insufficient memory for the terminating null of the string. Correct, but we omit the terminating null intentionally there.

---
 scripts/coverity-model.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
scripts/coverity-model.c +10 -0
index 617f67d..e1d5f45 100644
@@ -236,6 +236,23 @@ void *g_try_realloc(void *ptr, size_t size)
     return g_try_realloc_n(ptr, 1, size);
 }
 
+/* Other memory allocation functions */
+
+void *g_memdup(const void *ptr, unsigned size)
+{
+    unsigned char *dup;
+    unsigned i;
+
+    if (!ptr) {
+        return NULL;
+    }

scripts/coverity-model.c void *g_try_realloc(void *ptr, size_t size) · line 258

Quoted 10 lines
scripts/coverity-model.c +7 -0
+
+    dup = g_malloc(size);
+    for (i = 0; i < size; i++)
+        dup[i] = ((unsigned char *)ptr)[i];
+    return dup;
+}
+
 /*
  * GLib string allocation functions
  */