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=17dd898818b423c1ec1c2f1b80de4eda4a727012 commit 17dd898818b423c1ec1c2f1b80de4eda4a727012 (HEAD -> main) Author: Guillem Jover <[email protected]> AuthorDate: Fri May 17 23:35:26 2024 +0200 libdpkg: Make varbuf_detach() always return a string When we have added no content into a varbuf, we avoid allocating memory, but when calling varbuf_detach() the caller expects a valid string no matter what, and having to care about it returning NULL is error-prone. Instead, when we have no buffer, we return an allocated empty string. --- lib/dpkg/t/t-varbuf.c | 27 +++++++++++++++++++++++---- lib/dpkg/varbuf.c | 3 +++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/dpkg/t/t-varbuf.c b/lib/dpkg/t/t-varbuf.c index 2bd08f74c..1ab1cf206 100644 --- a/lib/dpkg/t/t-varbuf.c +++ b/lib/dpkg/t/t-varbuf.c @@ -568,22 +568,41 @@ test_varbuf_detach(void) char *str; varbuf_init(&vb, 0); + test_pass(vb.used == 0); + test_pass(vb.size == 0); + test_pass(vb.buf == NULL); + str = varbuf_detach(&vb); + test_str(str, ==, ""); + test_pass(vb.used == 0); + test_pass(vb.size == 0); + test_pass(vb.buf == NULL); + free(str); - varbuf_add_buf(&vb, "1234567890", 10); - + varbuf_init(&vb, 0); + varbuf_add_buf(&vb, NULL, 0); + test_pass(vb.used == 0); + test_pass(vb.size == 0); + test_pass(vb.buf == NULL); str = varbuf_detach(&vb); + test_str(str, ==, ""); + test_pass(vb.used == 0); + test_pass(vb.size == 0); + test_pass(vb.buf == NULL); + free(str); + varbuf_init(&vb, 0); + varbuf_add_buf(&vb, "1234567890", 10); + str = varbuf_detach(&vb); test_mem(str, ==, "1234567890", 10); test_pass(vb.used == 0); test_pass(vb.size == 0); test_pass(vb.buf == NULL); - free(str); } TEST_ENTRY(test) { - test_plan(191); + test_plan(205); test_varbuf_init(); test_varbuf_prealloc(); diff --git a/lib/dpkg/varbuf.c b/lib/dpkg/varbuf.c index 37902e711..5829a6f78 100644 --- a/lib/dpkg/varbuf.c +++ b/lib/dpkg/varbuf.c @@ -314,6 +314,9 @@ varbuf_detach(struct varbuf *v) v->size = 0; v->used = 0; + if (buf == NULL) + buf = m_strdup(""); + return buf; } -- Dpkg.Org's dpkg

