enhance to generate encode routine to be more robust to encode NULL

This commit is contained in:
Rongsong Shen 2025-04-27 17:23:50 +08:00
parent 3425f75440
commit 07e2126e25
4 changed files with 17 additions and 1 deletions

View file

@ -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");