Skip to content

Commit

Permalink
[atlesn] Replace callocs() with explicit initialization of unused fie…
Browse files Browse the repository at this point in the history
…lds or areas
  • Loading branch information
atlesn committed Mar 23, 2024
1 parent 07b5f3e commit 515b97b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,15 @@ int convertToLeader(struct raft *r)
r->barrier.type = RAFT_BARRIER;
r->barrier.term = r->current_term;
r->barrier.buf.len = 8;
r->barrier.buf.base = raft_calloc(1, r->barrier.buf.len);
r->barrier.buf.base = raft_malloc(r->barrier.buf.len);

if (r->barrier.buf.base == NULL) {
rv = RAFT_NOMEM;
goto err;
}

*(uint64_t *) r->barrier.buf.base = 0;

r->barrier.batch = r->barrier.buf.base;

rv = ClientSubmit(r, &r->barrier, 1);
Expand Down
7 changes: 5 additions & 2 deletions src/uv_encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ int uvEncodeMessage(const struct raft_message *message,
return RAFT_MALFORMED;
};

header.base = raft_calloc(1, header.len);
header.base = raft_malloc(header.len);
if (header.base == NULL) {
goto oom;
}
Expand Down Expand Up @@ -337,7 +337,10 @@ void uvEncodeBatchHeader(const struct raft_entry *entries,
/* Message type (Either RAFT_COMMAND or RAFT_CHANGE) */
bytePut8(&cursor, (uint8_t)entry->type);

cursor = (uint8_t *)cursor + 3; /* Unused */
/* Unused */
bytePut8(&cursor, 0);
bytePut8(&cursor, 0);
bytePut8(&cursor, 0);

/* Size of the log entry data, little endian. */
bytePut32(&cursor, (uint32_t)entry->buf.len);
Expand Down
15 changes: 9 additions & 6 deletions src/uv_tcp_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,23 @@ struct uvTcpConnect
static int uvTcpEncodeHandshake(raft_id id, const char *address, uv_buf_t *buf)
{
uint8_t *cursor;
size_t address_len = bytePad64(strlen(address) + 1);
size_t address_len = strlen(address) + 1;
size_t address_len_padded = bytePad64(address_len);
buf->len = sizeof(uint64_t) + /* Protocol version. */
sizeof(uint64_t) + /* Server ID. */
sizeof(uint64_t) /* Size of the address buffer */;
buf->len += address_len;
buf->base = RaftHeapCalloc(1, buf->len);
sizeof(uint64_t) + /* Size of the address buffer */
address_len_padded;
buf->base = RaftHeapMalloc(buf->len);
if (buf->base == NULL) {
return RAFT_NOMEM;
}
cursor = (uint8_t *)buf->base;
bytePut64(&cursor, UV__TCP_HANDSHAKE_PROTOCOL);
bytePut64(&cursor, id);
bytePut64(&cursor, address_len);
strcpy((char *)cursor, address);
bytePut64(&cursor, address_len_padded);
memcpy(cursor, address, address_len);
cursor += address_len;
memset(cursor, 0, address_len_padded - address_len);
return 0;
}

Expand Down

0 comments on commit 515b97b

Please sign in to comment.