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=7f320dfdfd802bab9e35099845dd1560d141b3d9

commit 7f320dfdfd802bab9e35099845dd1560d141b3d9
Author: Guillem Jover <[email protected]>
AuthorDate: Wed Jul 24 04:52:30 2024 +0200

    libdpkg: Add new varbuf_array() and C++ operator[] methods
    
    We want to be able to replace current raw pointer usage, which accesses
    those pointers as arrays. We add these methods so that we can switch the
    code to use varbuf, while at the same time, making it safer to use, as
    the new code will have built-in range checks.
---
 lib/dpkg/libdpkg.map |  1 +
 lib/dpkg/varbuf.c    | 10 ++++++++++
 lib/dpkg/varbuf.h    | 16 ++++++++++++++++
 3 files changed, 27 insertions(+)

diff --git a/lib/dpkg/libdpkg.map b/lib/dpkg/libdpkg.map
index 21754c5ed..78f65636c 100644
--- a/lib/dpkg/libdpkg.map
+++ b/lib/dpkg/libdpkg.map
@@ -119,6 +119,7 @@ LIBDPKG_PRIVATE {
        varbuf_grow;
        varbuf_trunc;
        varbuf_str;
+       varbuf_array;
        varbuf_set_varbuf;
        varbuf_set_buf;
        varbuf_set_vfmt;
diff --git a/lib/dpkg/varbuf.c b/lib/dpkg/varbuf.c
index 8e4589c0f..73fbfa35c 100644
--- a/lib/dpkg/varbuf.c
+++ b/lib/dpkg/varbuf.c
@@ -115,6 +115,16 @@ varbuf_str(struct varbuf *v)
        return v->buf;
 }
 
+char *
+varbuf_array(const struct varbuf *v, size_t index)
+{
+       if (index > v->used)
+               internerr("varbuf array access (%zu) > used (%zu)",
+                         index, v->used);
+
+       return v->buf + index;
+}
+
 void
 varbuf_set_buf(struct varbuf *v, const void *buf, size_t size)
 {
diff --git a/lib/dpkg/varbuf.h b/lib/dpkg/varbuf.h
index 5b0e259f0..87714d93e 100644
--- a/lib/dpkg/varbuf.h
+++ b/lib/dpkg/varbuf.h
@@ -107,6 +107,9 @@ struct varbuf {
        varbuf &operator+=(int c);
        varbuf &operator+=(const char *str);
 
+       const char &operator[](int i) const;
+       char &operator[](int i);
+
        size_t len() const;
        const char *str();
 #endif
@@ -127,6 +130,7 @@ void varbuf_destroy(struct varbuf *v);
 void varbuf_free(struct varbuf *v);
 
 const char *varbuf_str(struct varbuf *v);
+char *varbuf_array(const struct varbuf *v, size_t index);
 
 void varbuf_set_varbuf(struct varbuf *v, struct varbuf *other);
 void varbuf_set_buf(struct varbuf *v, const void *buf, size_t size);
@@ -422,6 +426,18 @@ varbuf::operator+=(const char *str)
        return *this;
 }
 
+inline const char &
+varbuf::operator[](int i) const
+{
+       return *varbuf_array(this, i);
+}
+
+inline char &
+varbuf::operator[](int i)
+{
+       return *varbuf_array(this, i);
+}
+
 inline size_t
 varbuf::len() const
 {

-- 
Dpkg.Org's dpkg

Reply via email to