diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index 4484632..67dcf5f 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -1256,7 +1256,11 @@ void mk_struct_ptr_encoder(capnp_ctx_t *ctx, struct node *n) { n->name.str, n->name.str, buf); str_addf(&(ctx->SRC), "\t%s_ptr ptr;\n", n->name.str); str_addf(&(ctx->SRC), "\tstruct %s d;\n", n->name.str); + str_addf(&(ctx->SRC), "\t%s zero_ = {0}; \n", buf); str_addf(&(ctx->SRC), "\tptr = new_%s(cs);\n", n->name.str); + str_addf(&(ctx->SRC), "\tif (s == NULL) {\n"); + str_addf(&(ctx->SRC), "\t\ts = &zero_;\n"); + str_addf(&(ctx->SRC), "\t}\n"); str_addf(&(ctx->SRC), "\tencode_%s(cs, &d, s);\n", n->name.str); str_addf(&(ctx->SRC), "\twrite_%s(&d, ptr);\n", n->name.str); str_addf(&(ctx->SRC), "\t(*p) = ptr;\n"); diff --git a/examples/book/book.capnp b/examples/book/book.capnp index 1192166..7035124 100644 --- a/examples/book/book.capnp +++ b/examples/book/book.capnp @@ -15,11 +15,17 @@ struct Publish $C.mapname("publish_t") { isbn @0: UInt64; year @1: UInt32; } + +struct Nulldata $C.mapname("nulldata_t") { + null @0: UInt32 $C.mapname("null_"); +} + struct Book $C.mapname("book_t") { title @0: Text; authors @1: List(Text) $C.mapname("authors") $C.maplistcount("n_authors"); chapters @5: List(Chapter) $C.mapname("chapters_") $C.maplistcount("n_chapters"); publish @6: Publish; + nulldata @7: Nulldata; magic1 @2: List(UInt32) $C.mapname("magic_1") $C.maplistcount("n_magic1"); acquire :union $C.mapuniontag("acquire_method") { buy @3: Text; diff --git a/examples/book/book.h b/examples/book/book.h index d678176..fb11afd 100644 --- a/examples/book/book.h +++ b/examples/book/book.h @@ -16,6 +16,10 @@ typedef struct { uint32_t year; } publish_t; +typedef struct { + int null_; +} nulldata_t; + typedef struct { char *title; int n_authors; @@ -23,6 +27,7 @@ typedef struct { int n_chapters; chapter_t *chapters_; publish_t *publish; + nulldata_t *nulldata; int n_magic1; uint32_t *magic_1; int acquire_method; diff --git a/examples/book/test.c b/examples/book/test.c index 7e771d1..34334fa 100644 --- a/examples/book/test.c +++ b/examples/book/test.c @@ -44,7 +44,8 @@ int encode() { book.authors = authors; book.n_chapters = 3; book.chapters_ = &chapters[0]; - book.publish = &publish; + book.publish = &publish ; + book.nulldata = NULL; book.n_magic1 = 2; book.magic_1 = &magic1[0]; book.acquire_method = Book_acquire_buy;