1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-10 01:18:15 +03:00

ctdb-protocol: Fix marshalling for ctdb_dbid_map

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
Amitay Isaacs 2017-06-29 19:33:04 +10:00 committed by Martin Schwenke
parent 917c0eca0d
commit e347e2b496
6 changed files with 125 additions and 44 deletions

View File

@ -1435,7 +1435,7 @@ static void ctdb_reply_control_data_push(struct ctdb_reply_control_data *cd,
break;
case CTDB_CONTROL_GET_DBMAP:
ctdb_dbid_map_push(cd->data.dbmap, buf);
ctdb_dbid_map_push(cd->data.dbmap, buf, &np);
break;
case CTDB_CONTROL_PULL_DB:
@ -1602,7 +1602,7 @@ static int ctdb_reply_control_data_pull(uint8_t *buf, size_t buflen,
case CTDB_CONTROL_GET_DBMAP:
ret = ctdb_dbid_map_pull(buf, buflen, mem_ctx,
&cd->data.dbmap);
&cd->data.dbmap, &np);
break;
case CTDB_CONTROL_PULL_DB:

View File

@ -127,10 +127,11 @@ void ctdb_dbid_push(struct ctdb_dbid *in, uint8_t *buf, size_t *npush);
int ctdb_dbid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
struct ctdb_dbid **out, size_t *npull);
size_t ctdb_dbid_map_len(struct ctdb_dbid_map *dbmap);
void ctdb_dbid_map_push(struct ctdb_dbid_map *dbmap, uint8_t *buf);
size_t ctdb_dbid_map_len(struct ctdb_dbid_map *in);
void ctdb_dbid_map_push(struct ctdb_dbid_map *in, uint8_t *buf,
size_t *npush);
int ctdb_dbid_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
struct ctdb_dbid_map **out);
struct ctdb_dbid_map **out, size_t *npull);
size_t ctdb_pulldb_len(struct ctdb_pulldb *pulldb);
void ctdb_pulldb_push(struct ctdb_pulldb *pulldb, uint8_t *buf);

View File

@ -1007,60 +1007,81 @@ int ctdb_dbid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
return 0;
}
struct ctdb_dbid_map_wire {
uint32_t num;
struct ctdb_dbid dbs[1];
};
size_t ctdb_dbid_map_len(struct ctdb_dbid_map *dbmap)
size_t ctdb_dbid_map_len(struct ctdb_dbid_map *in)
{
return sizeof(uint32_t) + dbmap->num * sizeof(struct ctdb_dbid);
size_t len;
len = ctdb_uint32_len(&in->num);
if (in->num > 0) {
len += in->num * ctdb_dbid_len(&in->dbs[0]);
}
return len;
}
void ctdb_dbid_map_push(struct ctdb_dbid_map *dbmap, uint8_t *buf)
void ctdb_dbid_map_push(struct ctdb_dbid_map *in, uint8_t *buf, size_t *npush)
{
struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
size_t offset = 0, np;
uint32_t i;
wire->num = dbmap->num;
memcpy(wire->dbs, dbmap->dbs, dbmap->num * sizeof(struct ctdb_dbid));
ctdb_uint32_push(&in->num, buf+offset, &np);
offset += np;
for (i=0; i<in->num; i++) {
ctdb_dbid_push(&in->dbs[i], buf+offset, &np);
offset += np;
}
*npush = offset;
}
int ctdb_dbid_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
struct ctdb_dbid_map **out)
struct ctdb_dbid_map **out, size_t *npull)
{
struct ctdb_dbid_map *dbmap;
struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
struct ctdb_dbid_map *val;
size_t offset = 0, np;
uint32_t i;
int ret;
if (buflen < sizeof(uint32_t)) {
return EMSGSIZE;
}
if (wire->num > buflen / sizeof(struct ctdb_dbid)) {
return EMSGSIZE;
}
if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid) <
sizeof(uint32_t)) {
return EMSGSIZE;
}
if (buflen < sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid)) {
return EMSGSIZE;
}
dbmap = talloc(mem_ctx, struct ctdb_dbid_map);
if (dbmap == NULL) {
val = talloc(mem_ctx, struct ctdb_dbid_map);
if (val == NULL) {
return ENOMEM;
}
dbmap->num = wire->num;
ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->num, &np);
if (ret != 0) {
goto fail;
}
offset += np;
dbmap->dbs = talloc_memdup(dbmap, wire->dbs,
wire->num * sizeof(struct ctdb_dbid));
if (dbmap->dbs == NULL) {
talloc_free(dbmap);
return ENOMEM;
if (val->num == 0) {
val->dbs = NULL;
goto done;
}
*out = dbmap;
val->dbs = talloc_array(val, struct ctdb_dbid, val->num);
if (val->dbs == NULL) {
ret = ENOMEM;
goto fail;
}
for (i=0; i<val->num; i++) {
ret = ctdb_dbid_pull_elems(buf+offset, buflen-offset, val,
&val->dbs[i], &np);
if (ret != 0) {
goto fail;
}
offset += np;
}
done:
*out = val;
*npull = offset;
return 0;
fail:
talloc_free(val);
return ret;
}
size_t ctdb_pulldb_len(struct ctdb_pulldb *pulldb)

View File

@ -468,7 +468,7 @@ void fill_ctdb_dbid_map(TALLOC_CTX *mem_ctx, struct ctdb_dbid_map *p)
p->num = rand_int(40);
if (p->num > 0) {
p->dbs = talloc_array(mem_ctx, struct ctdb_dbid, p->num);
p->dbs = talloc_zero_array(mem_ctx, struct ctdb_dbid, p->num);
assert(p->dbs != NULL);
for (i=0; i<p->num; i++) {
fill_ctdb_dbid(mem_ctx, &p->dbs[i]);

View File

@ -185,9 +185,67 @@ static int ctdb_vnn_map_pull_old(uint8_t *buf, size_t buflen,
return 0;
}
struct ctdb_dbid_map_wire {
uint32_t num;
struct ctdb_dbid dbs[1];
};
static size_t ctdb_dbid_map_len_old(struct ctdb_dbid_map *in)
{
return sizeof(uint32_t) + in->num * sizeof(struct ctdb_dbid);
}
static void ctdb_dbid_map_push_old(struct ctdb_dbid_map *in, uint8_t *buf)
{
struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
wire->num = in->num;
memcpy(wire->dbs, in->dbs, in->num * sizeof(struct ctdb_dbid));
}
static int ctdb_dbid_map_pull_old(uint8_t *buf, size_t buflen,
TALLOC_CTX *mem_ctx,
struct ctdb_dbid_map **out)
{
struct ctdb_dbid_map *val;
struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
if (buflen < sizeof(uint32_t)) {
return EMSGSIZE;
}
if (wire->num > buflen / sizeof(struct ctdb_dbid)) {
return EMSGSIZE;
}
if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid) <
sizeof(uint32_t)) {
return EMSGSIZE;
}
if (buflen < sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid)) {
return EMSGSIZE;
}
val = talloc(mem_ctx, struct ctdb_dbid_map);
if (val == NULL) {
return ENOMEM;
}
val->num = wire->num;
val->dbs = talloc_memdup(val, wire->dbs,
wire->num * sizeof(struct ctdb_dbid));
if (val->dbs == NULL) {
talloc_free(val);
return ENOMEM;
}
*out = val;
return 0;
}
COMPAT_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics);
COMPAT_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map);
COMPAT_TYPE3_TEST(struct ctdb_dbid_map, ctdb_dbid_map);
int main(int argc, char *argv[])
{
@ -198,6 +256,7 @@ int main(int argc, char *argv[])
COMPAT_TEST_FUNC(ctdb_statistics)();
COMPAT_TEST_FUNC(ctdb_vnn_map)();
COMPAT_TEST_FUNC(ctdb_dbid_map)();
return 0;
}

View File

@ -66,7 +66,7 @@ static void test_ctdb_g_lock(void)
PROTOCOL_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics);
PROTOCOL_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map);
PROTOCOL_TYPE3_TEST(struct ctdb_dbid, ctdb_dbid);
DEFINE_TEST(struct ctdb_dbid_map, ctdb_dbid_map);
PROTOCOL_TYPE3_TEST(struct ctdb_dbid_map, ctdb_dbid_map);
DEFINE_TEST(struct ctdb_pulldb, ctdb_pulldb);
DEFINE_TEST(struct ctdb_pulldb_ext, ctdb_pulldb_ext);
DEFINE_TEST(struct ctdb_rec_data, ctdb_rec_data);