This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch main
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=0e589d3fcd3e94b70dd37d73fc21121ed2035609

commit 0e589d3fcd3e94b70dd37d73fc21121ed2035609
Author: Guillem Jover <[email protected]>
AuthorDate: Sun May 19 11:21:10 2024 +0200

    libdpkg: Make prefix and suffix varbuf checks cope with unallocated varbufs
    
    If the varbufs have not been allocated, we should not be passing them to
    functions that expect their arguments not to be NULL.
    
    Changelog: silent
---
 lib/dpkg/t/t-varbuf.c |  8 +++++++-
 lib/dpkg/varbuf.c     | 10 ++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/lib/dpkg/t/t-varbuf.c b/lib/dpkg/t/t-varbuf.c
index 1b9a23bd6..2bd08f74c 100644
--- a/lib/dpkg/t/t-varbuf.c
+++ b/lib/dpkg/t/t-varbuf.c
@@ -410,9 +410,15 @@ test_varbuf_has(void)
        struct varbuf vb_prefix = VARBUF_OBJECT;
        struct varbuf vb_suffix = VARBUF_OBJECT;
 
+       test_pass(varbuf_has_prefix(&vb, &vb_prefix));
+       test_pass(varbuf_has_suffix(&vb, &vb_suffix));
+
        varbuf_set_str(&vb_prefix, "prefix");
        varbuf_set_str(&vb_suffix, "suffix");
 
+       test_fail(varbuf_has_prefix(&vb, &vb_prefix));
+       test_fail(varbuf_has_suffix(&vb, &vb_suffix));
+
        varbuf_set_str(&vb, "prefix and some text");
        test_pass(varbuf_has_prefix(&vb, &vb_prefix));
        test_fail(varbuf_has_prefix(&vb, &vb_suffix));
@@ -577,7 +583,7 @@ test_varbuf_detach(void)
 
 TEST_ENTRY(test)
 {
-       test_plan(187);
+       test_plan(191);
 
        test_varbuf_init();
        test_varbuf_prealloc();
diff --git a/lib/dpkg/varbuf.c b/lib/dpkg/varbuf.c
index 02653e2f4..37902e711 100644
--- a/lib/dpkg/varbuf.c
+++ b/lib/dpkg/varbuf.c
@@ -182,6 +182,11 @@ varbuf_has_prefix(struct varbuf *v, struct varbuf *prefix)
        if (prefix->used > v->used)
                return false;
 
+       if (prefix->used == 0)
+               return true;
+       if (v->used == 0)
+               return false;
+
        return strncmp(v->buf, prefix->buf, prefix->used) == 0;
 }
 
@@ -193,6 +198,11 @@ varbuf_has_suffix(struct varbuf *v, struct varbuf *suffix)
        if (suffix->used > v->used)
                return false;
 
+       if (suffix->used == 0)
+               return true;
+       if (v->used == 0)
+               return false;
+
        slice = v->buf + v->used - suffix->used;
 
        return strcmp(slice, suffix->buf) == 0;

-- 
Dpkg.Org's dpkg

Reply via email to