Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit 26f4af5

Browse files
author
Mathieu Borderé
committed
configuration: Detect raft_id overflow
1 parent c61c701 commit 26f4af5

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/configuration.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ size_t configurationEncodedSize(const struct raft_configuration *c)
234234
return bytePad64(n);
235235
}
236236

237-
void configurationEncodeToBuf(const struct raft_configuration *c, void *buf)
237+
int configurationEncodeToBuf(const struct raft_configuration *c, void *buf)
238238
{
239239
void *cursor = buf;
240240
unsigned i;
@@ -247,12 +247,20 @@ void configurationEncodeToBuf(const struct raft_configuration *c, void *buf)
247247

248248
for (i = 0; i < c->n; i++) {
249249
struct raft_server *server = &c->servers[i];
250-
assert(server->address != NULL);
250+
if (server->address == NULL) {
251+
return RAFT_INVALID;
252+
}
253+
254+
if (server->id > UINT64_MAX) {
255+
return RAFT_BADID;
256+
}
251257
bytePut64Unaligned(&cursor, server->id); /* might not be aligned */
252258
bytePutString(&cursor, server->address);
253259
assert(server->role < 255);
254260
bytePut8(&cursor, (uint8_t)server->role);
255261
};
262+
263+
return 0;
256264
}
257265

258266
int configurationEncode(const struct raft_configuration *c,
@@ -270,9 +278,7 @@ int configurationEncode(const struct raft_configuration *c,
270278
return RAFT_NOMEM;
271279
}
272280

273-
configurationEncodeToBuf(c, buf->base);
274-
275-
return 0;
281+
return configurationEncodeToBuf(c, buf->base);
276282
}
277283

278284
int configurationDecode(const struct raft_buffer *buf,

src/configuration.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ size_t configurationEncodedSize(const struct raft_configuration *c);
4949

5050
/* Encode the given configuration object to the given pre-allocated buffer,
5151
* which is assumed to be at least configurationEncodedSize(c) bytes. */
52-
void configurationEncodeToBuf(const struct raft_configuration *c, void *buf);
52+
int configurationEncodeToBuf(const struct raft_configuration *c, void *buf);
5353

5454
/* Encode the given configuration object. The memory of the returned buffer is
5555
* allocated using raft_malloc(), and client code is responsible for releasing

src/uv_encoding.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ static void encodeAppendEntriesResult(
143143
bytePut64(&cursor, p->last_log_index);
144144
}
145145

146-
static void encodeInstallSnapshot(const struct raft_install_snapshot *p,
147-
void *buf)
146+
static int encodeInstallSnapshot(const struct raft_install_snapshot *p,
147+
void *buf)
148148
{
149+
int rv = 0;
149150
void *cursor;
150151
size_t conf_size = configurationEncodedSize(&p->conf);
151152

@@ -156,9 +157,13 @@ static void encodeInstallSnapshot(const struct raft_install_snapshot *p,
156157
bytePut64(&cursor, p->last_term); /* Term of last index. */
157158
bytePut64(&cursor, p->conf_index); /* Configuration index. */
158159
bytePut64(&cursor, conf_size); /* Configuration length. */
159-
configurationEncodeToBuf(&p->conf, cursor);
160+
rv = configurationEncodeToBuf(&p->conf, cursor);
161+
if (rv != 0) {
162+
return rv;
163+
}
160164
cursor = (uint8_t *)cursor + conf_size;
161165
bytePut64(&cursor, p->data.len); /* Snapshot data size. */
166+
return rv;
162167
}
163168

164169
static void encodeTimeoutNow(const struct raft_timeout_now *p, void *buf)

0 commit comments

Comments
 (0)