2017-04-18 0:36 GMT+02:00 Sri Ramanujam <sramanujam@xxxxxxxxx>: > Add virNumToStr(), which safely converts numbers into their string > representation. > > Functions added: > * virNumToStr_l > * virNumToStr_ul > --- > src/util/virstring.c | 34 ++++++++++++++++++++++++++++++++++ > src/util/virstring.h | 8 ++++++++ > 2 files changed, 42 insertions(+) > > diff --git a/src/util/virstring.c b/src/util/virstring.c > index 69abc26..f0d9e19 100644 > --- a/src/util/virstring.c > +++ b/src/util/virstring.c > @@ -536,6 +536,40 @@ virStrToDouble(char const *s, > return 0; > } > > +/** > + * Converts signed number to string representation. The caller is responsible > + * for freeing the result. > + */ > +int > +virNumToStr_l(long num, char **dst) > +{ > + int sz; > + > + sz = snprintf(NULL, 0, "%ld", num); > + if (sz > 0 && VIR_ALLOC_N(*dst, sz + 1) < 0) > + return -1; > + > + snprintf(*dst, sz + 1, "%ld", num); > + return 0; > +} > + > +/** > + * Converts unsigned number to string representation. The caller is responsible > + * for freeing the result. > + */ > +int > +virNumToStr_ul(unsigned long num, char **dst) > +{ > + int sz; > + > + sz = snprintf(NULL, 0, "%lu", num); > + if (sz > 0 && VIR_ALLOC_N(*dst, sz + 1) < 0) > + return -1; > + > + snprintf(*dst, sz + 1, "%lu", num); > + return 0; > +} > + What's the gain of if (virNumToStr_ul(memory_mb, &memory_str) < 0) goto cleanup; over if (virAsprintf(&memory_str, "%lu", memory_mb) < 0) goto cleanup; ? I think those two new functions are not necessary at all. -- Matthias Bolte http://photron.blogspot.com -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list