[libvirt PATCH 07/21] util: Use glib memory functions in virLogFilterNew

Tim Wiederhake posted 21 patches 5 years, 5 months ago
There is a newer version of this series
[libvirt PATCH 07/21] util: Use glib memory functions in virLogFilterNew
Posted by Tim Wiederhake 5 years, 5 months ago
Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
---
 src/util/virlog.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/src/util/virlog.c b/src/util/virlog.c
index f6d0c6c050..de36da1a4a 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -1308,7 +1308,6 @@ virLogFilterNew(const char *match,
                 virLogPriority priority)
 {
     virLogFilterPtr ret = NULL;
-    char *mdup = NULL;
     size_t mlen = strlen(match);
 
     if (priority < VIR_LOG_DEBUG || priority > VIR_LOG_ERROR) {
@@ -1317,23 +1316,16 @@ virLogFilterNew(const char *match,
         return NULL;
     }
 
+    ret = g_new0(virLogFilter, 1);
+    ret->priority = priority;
+
     /* We must treat 'foo' as equiv to '*foo*' for g_pattern_match
-     * todo substring matches, so add 2 extra bytes
+     * substring matches, so add 2 extra bytes
      */
-    if (VIR_ALLOC_N_QUIET(mdup, mlen + 3) < 0)
-        return NULL;
-
-    mdup[0] = '*';
-    memcpy(mdup + 1, match, mlen);
-    mdup[mlen + 1] = '*';
-
-    if (VIR_ALLOC_QUIET(ret) < 0) {
-        VIR_FREE(mdup);
-        return NULL;
-    }
-
-    ret->match = mdup;
-    ret->priority = priority;
+    ret->match = g_new0(char, mlen + 3);
+    ret->match[0] = '*';
+    memcpy(ret->match + 1, match, mlen);
+    ret->match[mlen + 1] = '*';
 
     return ret;
 }
-- 
2.26.2

Re: [libvirt PATCH 07/21] util: Use glib memory functions in virLogFilterNew
Posted by Ján Tomko 5 years, 5 months ago
On a Friday in 2020, Tim Wiederhake wrote:
>Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
>---
> src/util/virlog.c | 24 ++++++++----------------
> 1 file changed, 8 insertions(+), 16 deletions(-)
>
>diff --git a/src/util/virlog.c b/src/util/virlog.c
>index f6d0c6c050..de36da1a4a 100644
>--- a/src/util/virlog.c
>+++ b/src/util/virlog.c
>@@ -1308,7 +1308,6 @@ virLogFilterNew(const char *match,
>                 virLogPriority priority)
> {
>     virLogFilterPtr ret = NULL;
>-    char *mdup = NULL;
>     size_t mlen = strlen(match);
>
>     if (priority < VIR_LOG_DEBUG || priority > VIR_LOG_ERROR) {
>@@ -1317,23 +1316,16 @@ virLogFilterNew(const char *match,
>         return NULL;
>     }
>
>+    ret = g_new0(virLogFilter, 1);
>+    ret->priority = priority;
>+

>     /* We must treat 'foo' as equiv to '*foo*' for g_pattern_match
>-     * todo substring matches, so add 2 extra bytes
>+     * substring matches, so add 2 extra bytes
>      */

Unrelated comment change.

>-    if (VIR_ALLOC_N_QUIET(mdup, mlen + 3) < 0)
>-        return NULL;
>-
>-    mdup[0] = '*';
>-    memcpy(mdup + 1, match, mlen);
>-    mdup[mlen + 1] = '*';
>-
>-    if (VIR_ALLOC_QUIET(ret) < 0) {
>-        VIR_FREE(mdup);
>-        return NULL;
>-    }
>-
>-    ret->match = mdup;
>-    ret->priority = priority;
>+    ret->match = g_new0(char, mlen + 3);
>+    ret->match[0] = '*';
>+    memcpy(ret->match + 1, match, mlen);
>+    ret->match[mlen + 1] = '*';
>
>     return ret;
> }

With the comment change removed:
Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano