Skip to content

Commit 7bfe015

Browse files
committed
the values in the datastore are not 0-terminated, issue #14
1 parent 1557048 commit 7bfe015

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

ennodb.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ static dictionary *config;
1818
static int readonly = 0;
1919
static int cycle_log = 0;
2020

21+
#define _min(a, b) (((a) < (b)) ? (a) : (b))
22+
2123
static const char * get_prefix(const char *path) {
2224
const char * result = strrchr(path, '/');
2325
return result ? result+1 : 0;
@@ -124,26 +126,33 @@ static int list_keys(FCGX_Request *req, db_table *pl, const char *key) {
124126
int total = 0, result;
125127
char body[4096];
126128
char * b = body;
127-
size_t len = sizeof(body);
129+
size_t len = sizeof(body)-1;
128130

129131
do {
130132
result = cb_find_prefix(&pl->trie, key, strlen(key), matches, BATCH, total);
131133
if (result>1) {
132134
int i;
133135
for (i=0; i!=result; ++i) {
134-
const char *k, *v;
135136
size_t bytes;
136137
db_entry entry;
137138
cb_get_kv(matches[i], &entry, sizeof(db_entry));
138-
k = (const char *)matches[i];
139-
v = (const char *)entry.data;
140-
bytes = snprintf(b, len, "%s: %s\n", k, v);
139+
bytes = snprintf(b, len, "%s: ", (const char *)matches[i]);
140+
len -= bytes;
141+
b += bytes;
142+
bytes = _min(len, entry.size);
143+
memcpy(b, entry.data, bytes);
141144
len -= bytes;
142145
b += bytes;
146+
if (len>0) {
147+
b[0] = '\n';
148+
++b;
149+
--len;
150+
}
143151
}
144152
total += result;
145153
}
146154
} while (result==BATCH);
155+
b[0]=0;
147156
printf("found %d matches for prefix %s\n", total, key);
148157
if (total>0) {
149158
http_response(req->out, 200, "OK", body, strlen(body));

0 commit comments

Comments
 (0)