commit:     c1a7c3366024334c97303cad6e1e54e30285beda
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Mon Dec 30 09:16:03 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Mon Dec 30 09:16:03 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=c1a7c336

libq/tree: ensure valid pointers in case of realloc

in tree_read_file_binpkg_xpak_cb pointers could get stale after realloc,
if so, reposition them in the newly allocated block

Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 libq/tree.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/libq/tree.c b/libq/tree.c
index f0c8ddb..d9eec76 100644
--- a/libq/tree.c
+++ b/libq/tree.c
@@ -876,9 +876,19 @@ tree_read_file_binpkg_xpak_cb(
                data_len--;
 
        if (len - pos < (size_t)data_len) {
+               char *old_data = m->_data;
                len += (((data_len + 1) / BUFSIZ) + 1) * BUFSIZ;
                m->_data = xrealloc(m->_data, len);
                m->_md5_ = (char *)len;
+
+               /* re-position existing keys */
+               if (old_data != NULL && m->_data != old_data) {
+                       char **newdata = (char **)m;
+                       int elems = sizeof(tree_pkg_meta) / sizeof(char *);
+                       while (elems-- > 0)
+                               if (newdata[elems] != NULL)
+                                       newdata[elems] = m->_data + 
(newdata[elems] - old_data);
+               }
        }
 
        *key = m->_data + pos;

Reply via email to