From 3a7ae150b8fac1c108c28aa934b4650b7ad76577 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Sun, 27 Apr 2025 17:52:42 +0800 Subject: [PATCH] handle NULL text --- compiler/capnpc-c.c | 23 +++++++++++++++++------ examples/book/book.capnp | 1 + examples/book/book.h | 1 + examples/book/test.c | 1 + 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index 67dcf5f..8d98544 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -1070,9 +1070,21 @@ static void encode_member(capnp_ctx_t *ctx, struct str *func, struct field *f, break; case Type_text: str_add(func, tab, -1); - str_addf(func, "d->%s.str = s->%s;\n", var, var2); + str_addf(func, "if (s->%s != NULL) {\n", var2); str_add(func, tab, -1); - str_addf(func, "d->%s.len = strlen(s->%s);\n", var, var2); + str_addf(func, "\td->%s.str = s->%s;\n", var, var2); + str_add(func, tab, -1); + str_addf(func, "\td->%s.len = strlen(s->%s);\n", var, var2); + str_add(func, tab, -1); + str_addf(func, "}\n"); + str_add(func, tab, -1); + str_addf(func, "else{\n"); + str_add(func, tab, -1); + str_addf(func, "\td->%s.str = \"\";\n", var); + str_add(func, tab, -1); + str_addf(func, "\td->%s.len = 0;\n", var); + str_add(func, tab, -1); + str_addf(func, "}\n"); str_add(func, tab, -1); str_addf(func, "d->%s.seg = NULL;\n", var); break; @@ -1081,8 +1093,8 @@ static void encode_member(capnp_ctx_t *ctx, struct str *func, struct field *f, if (n != NULL) { str_add(func, tab, -1); - str_addf(func, "encode_%s_ptr(cs, &(d->%s), s->%s);\n", n->name.str, - var, var2); + str_addf(func, "encode_%s_ptr(cs, &(d->%s), s->%s);\n", n->name.str, var, + var2); } break; case Type__list: @@ -1323,8 +1335,7 @@ void mk_struct_ptr_decoder(capnp_ctx_t *ctx, struct node *n) { "%s_ptr p) {\n", n->name.str, buf, n->name.str); str_addf(&(ctx->SRC), "\tstruct %s s;\n", n->name.str); - str_addf(&(ctx->SRC), "\t*d = (%s *)calloc(1, sizeof(%s));\n", - buf, buf); + str_addf(&(ctx->SRC), "\t*d = (%s *)calloc(1, sizeof(%s));\n", buf, buf); str_addf(&(ctx->SRC), "\tread_%s(&s, p);\n", n->name.str); str_addf(&(ctx->SRC), "\tdecode_%s(*d, &s);\n", n->name.str); str_addf(&(ctx->SRC), "}\n"); diff --git a/examples/book/book.capnp b/examples/book/book.capnp index 7035124..c9c13ac 100644 --- a/examples/book/book.capnp +++ b/examples/book/book.capnp @@ -27,6 +27,7 @@ struct Book $C.mapname("book_t") { publish @6: Publish; nulldata @7: Nulldata; magic1 @2: List(UInt32) $C.mapname("magic_1") $C.maplistcount("n_magic1"); + description @8: Text; acquire :union $C.mapuniontag("acquire_method") { buy @3: Text; donation @4: Text; diff --git a/examples/book/book.h b/examples/book/book.h index fb11afd..2c6bd77 100644 --- a/examples/book/book.h +++ b/examples/book/book.h @@ -30,6 +30,7 @@ typedef struct { nulldata_t *nulldata; int n_magic1; uint32_t *magic_1; + char *description; int acquire_method; union { char *buy; diff --git a/examples/book/test.c b/examples/book/test.c index 34334fa..357f918 100644 --- a/examples/book/test.c +++ b/examples/book/test.c @@ -48,6 +48,7 @@ int encode() { book.nulldata = NULL; book.n_magic1 = 2; book.magic_1 = &magic1[0]; + book.description = NULL; book.acquire_method = Book_acquire_buy; book.acquire.buy = "bought from Xinhua book store";