Hi!
I noticed that dict_cdb uses static buffer to read the value,
while all other maps use per-instance val_buf vstring or similar,
and even the fold_buf is per-instance. I wonder if something like
the below is actually necessary.
diff --git a/src/util/dict_cdb.c b/src/util/dict_cdb.c
index a9133cc3..63f5eaeb 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);
}
@@ -206,6 +209,7 @@ static DICT *dict_cdbq_open(const char *path, int
dict_flags)
#else
cdb_init(&(dict_cdbq->cdb), fd);
#endif
+ dict_cdbq->val_buf = 0;
dict_cdbq->dict.lookup = dict_cdbq_lookup;
dict_cdbq->dict.close = dict_cdbq_close;
dict_cdbq->dict.stat_fd = fd;
_______________________________________________
Postfix-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]