[sysrepo-devel] New idea - let compiler check the types of variadic arguments

Pavol Hanzel Pavol.Hanzel at pantheon.tech
Mon Dec 10 11:50:41 UTC 2018

Hello sysrepo team,

I am Pavol Hanzel, a software engineer from Pantheon Technologies. Currently we're contributing to the sweetcomb<https://wiki.fd.io/view/Project_Proposals/Sweetcomb> project, which uses sysrepo as a datastore for VPP.

We're generating a lot of code that uses sr_val_build_xpath, and we think it would be great if the compiler could warn us when we supply an incorrect argument, Like this:

error: format '%s' expects argument of type 'char *', but argument 3 has type 'u32 {aka unsigned int}' [-Werror=format=]
     sr_val_build_xpath(&vals[0], "/test-bridge-domain:bridge-domains/bridge-domain[id='%s']/interfaces/interface[name='%s']/state/name",
                        reply->bd_id, interface_name);

To achieve that, one just needs to change the signature of variadic functions like this:
int sr_val_build_xpath(sr_val_t *value, const char *format, ...) __attribute__ ((__format__(printf, 2, 3)));

These are the functions that would need the change:

./inc/sysrepo/trees.h:114:int sr_node_build_str_data(sr_node_t *node, sr_type_t type, const char *format, ...);
./inc/sysrepo/values.h:96:int sr_val_build_xpath(sr_val_t *value, const char *format, ...);
./inc/sysrepo/values.h:115:int sr_val_build_str_data(sr_val_t *value, sr_type_t type, const char *format, ...);
./src/common/sr_logger.h:227:void sr_log_to_cb(sr_log_level_t level, const char *format, ...);
./src/common/sr_utils.h:187:int sr_asprintf(char **strp, const char *fmt, ...);
./src/common/sr_utils.h:621:        const char *msg_fmt, ...);
./src/common/sr_utils.h:726:int sr_print(sr_print_ctx_t *print_ctx, const char *format, ...);

Please let me know your remarks about this idea!
Pavol Hanzel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sysrepo.org/archives/sysrepo-devel/attachments/20181210/20abd396/attachment.html>

More information about the sysrepo-devel mailing list