use per-instance val_buf vstring buffer instead of
single static buf for all cdb instances, which might
be unsafe if more than one result is being in use at
the same time.

Signed-off-by: Michael Tokarev <m...@tls.msk.ru>
---
previous patch has been garbled by cut-n-paste.
Also move val_buf init a bit earlier.

 src/util/dict_cdb.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/util/dict_cdb.c b/src/util/dict_cdb.c
index a9133cc3..9b4648b7 100644
--- a/src/util/dict_cdb.c
+++ b/src/util/dict_cdb.c
@@ -84,6 +84,7 @@
 typedef struct {
     DICT    dict;                      /* generic members */
     struct cdb cdb;                    /* cdb structure */
+    VSTRING *val_buf;                  /* value result */
 } DICT_CDBQ;                           /* query interface */
 
 typedef struct {
@@ -100,8 +101,6 @@ static const char *dict_cdbq_lookup(DICT *dict, const char 
*name)
     DICT_CDBQ *dict_cdbq = (DICT_CDBQ *) dict;
     unsigned vlen;
     int     status = 0;
-    static char *buf;
-    static unsigned len;
     const char *result = 0;
 
     dict->error = 0;
@@ -142,19 +141,21 @@ static const char *dict_cdbq_lookup(DICT *dict, const 
char *name)
 
     if (status) {
        vlen = cdb_datalen(&dict_cdbq->cdb);
-       if (len < vlen) {
-           if (buf == 0)
-               buf = mymalloc(vlen + 1);
-           else
-               buf = myrealloc(buf, vlen + 1);
-           len = vlen;
+       if (!dict_cdbq->val_buf)
+           dict_cdbq->val_buf = vstring_alloc(vlen < 20 ? 20 : vlen);
+       else {
+           VSTRING_RESET(dict_cdbq->val_buf);
+           VSTRING_SPACE(dict_cdbq->val_buf, vlen);
        }
-       if (cdb_read(&dict_cdbq->cdb, buf, vlen,
-                    cdb_datapos(&dict_cdbq->cdb)) < 0)
+       if (cdb_read(&dict_cdbq->cdb, vstring_str(dict_cdbq->val_buf),
+                    vlen, cdb_datapos(&dict_cdbq->cdb)) < 0)
            msg_fatal("error reading %s: %m", dict->name);
-       buf[vlen] = '\0';
-       result = buf;
+       vstring_set_payload_size(dict_cdbq->val_buf, vlen);
+       VSTRING_TERMINATE(dict_cdbq->val_buf);
+
+       result = vstring_str(dict_cdbq->val_buf);
     }
+
     /* No locking so not release the lock.  */
 
     return (result);
@@ -170,6 +171,8 @@ static void dict_cdbq_close(DICT *dict)
     close(dict->stat_fd);
     if (dict->fold_buf)
        vstring_free(dict->fold_buf);
+    if (dict_cdbq->val_buf)
+       vstring_free(dict_cdbq->val_buf);
     dict_free(dict);
 }
 
@@ -200,6 +203,7 @@ static DICT *dict_cdbq_open(const char *path, int 
dict_flags)
 
     dict_cdbq = (DICT_CDBQ *) dict_alloc(DICT_TYPE_CDB,
                                         cdb_path, sizeof(*dict_cdbq));
+    dict_cdbq->val_buf = 0;
 #if defined(TINYCDB_VERSION)
     if (cdb_init(&(dict_cdbq->cdb), fd) != 0)
        msg_fatal("dict_cdbq_open: unable to init %s: %m", cdb_path);
-- 
2.39.5

_______________________________________________
Postfix-devel mailing list -- postfix-devel@postfix.org
To unsubscribe send an email to postfix-devel-le...@postfix.org

Reply via email to