opensourcerouting / c-capnproto

C library/compiler for the Cap'n Proto serialization/RPC protocol
MIT License
118 stars 40 forks source link

generated code broken for Data type with empty string as default #23

Open assaf758 opened 7 years ago

assaf758 commented 7 years ago

The generated .c file for the below capnp does not compile due to undeclared capn_buf:

$ cat Test.capnp.c                 
#include "Test.capnp.h"            
/* AUTO GENERATED - DO NOT EDIT */ 
#ifdef __GNUC__                    
# define capnp_unused __attribute__((unused))                                                                                                 
# define capnp_use(x) (void) x;    
#else                              
# define capnp_unused              
# define capnp_use(x)              
#endif                             

static const capn_ptr capn_null = {CAPN_NULL};                         
static capn_data capn_val1 = {{2,0,0,0,1,0,0,(char*)&capn_buf[0],(struct capn_segment*)&capn_seg}};                                           
...

File Test.capnp:

@0xa4ba9dce9642ee58;

struct Record {
   info     @0: Data = "";
}

struct RootStruct {
   myList   @0: List(Record);
}
assaf758 commented 7 years ago

this seems to fix it for me, not sure its the right approach

@@ -1026,7 +1026,11 @@ capn_ptr capn_new_list(struct capn_segment *seg, int sz, int datasz, int ptrs) {                                      
                new_object(&p, p.len * 4);                            
        } else {                   
                p.datasz = datasz; 
-               new_object(&p, p.len * datasz);                       
+               if (p.len > 0) {   
+                       new_object(&p, p.len * datasz);               
+               } else {           
+                       new_object(&p, 1);                            
+               }                  
        }                          

        return p;