<div dir="ltr">Ah, you're absolutely right, I completely forgot this function existed. </div><br><div class="gmail_quote"><div dir="ltr">On Tue, Apr 18, 2017 at 5:18 PM Matthias Bolte <<a href="mailto:matthias.bolte@googlemail.com">matthias.bolte@googlemail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">2017-04-18 0:36 GMT+02:00 Sri Ramanujam <<a href="mailto:sramanujam@datto.com" target="_blank">sramanujam@datto.com</a>>:<br>
> Add virNumToStr(), which safely converts numbers into their string<br>
> representation.<br>
><br>
> Functions added:<br>
> * virNumToStr_l<br>
> * virNumToStr_ul<br>
> ---<br>
>  src/util/virstring.c | 34 ++++++++++++++++++++++++++++++++++<br>
>  src/util/virstring.h |  8 ++++++++<br>
>  2 files changed, 42 insertions(+)<br>
><br>
> diff --git a/src/util/virstring.c b/src/util/virstring.c<br>
> index 69abc26..f0d9e19 100644<br>
> --- a/src/util/virstring.c<br>
> +++ b/src/util/virstring.c<br>
> @@ -536,6 +536,40 @@ virStrToDouble(char const *s,<br>
>      return 0;<br>
>  }<br>
><br>
> +/**<br>
> + * Converts signed number to string representation. The caller is responsible<br>
> + * for freeing the result.<br>
> + */<br>
> +int<br>
> +virNumToStr_l(long num, char **dst)<br>
> +{<br>
> +    int sz;<br>
> +<br>
> +    sz = snprintf(NULL, 0, "%ld", num);<br>
> +    if (sz > 0 && VIR_ALLOC_N(*dst, sz + 1) < 0)<br>
> +        return -1;<br>
> +<br>
> +    snprintf(*dst, sz + 1, "%ld", num);<br>
> +    return 0;<br>
> +}<br>
> +<br>
> +/**<br>
> + * Converts unsigned number to string representation. The caller is responsible<br>
> + * for freeing the result.<br>
> + */<br>
> +int<br>
> +virNumToStr_ul(unsigned long num, char **dst)<br>
> +{<br>
> +    int sz;<br>
> +<br>
> +    sz = snprintf(NULL, 0, "%lu", num);<br>
> +    if (sz > 0 && VIR_ALLOC_N(*dst, sz + 1) < 0)<br>
> +        return -1;<br>
> +<br>
> +    snprintf(*dst, sz + 1, "%lu", num);<br>
> +    return 0;<br>
> +}<br>
> +<br>
<br>
What's the gain of<br>
<br>
if (virNumToStr_ul(memory_mb, &memory_str) < 0)<br>
   goto cleanup;<br>
<br>
over<br>
<br>
if (virAsprintf(&memory_str, "%lu", memory_mb) < 0)<br>
   goto cleanup;<br>
<br>
?<br>
<br>
I think those two new functions are not necessary at all.<br>
<br>
--<br>
Matthias Bolte<br>
<a href="http://photron.blogspot.com" rel="noreferrer" target="_blank">http://photron.blogspot.com</a><br>
</blockquote></div><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature"><div dir="ltr"><table style="border-collapse:collapse;width:270pt" dir="ltr" cellspacing="0" cellpadding="0" border="0" width="270"><tbody><tr style="min-height:17pt" height="17"><td style="min-height:17pt;width:270pt" width="270" height="17"><font size="4"><b>Sri Ramanujam</b></font>
                                                </td>
                                        </tr>
                                        <tr style="min-height:16pt" height="16">
                                                <td style="min-height:16pt" height="16">Software Engineer</td>
                                        </tr>
                                        <tr style="min-height:16pt" height="16">
                                                <td style="min-height:16pt;padding-bottom:0in;padding-top:0in" height="16">Datto, Inc.</td>
                                        </tr>
                                        <tr style="min-height:16pt" height="16">
                                                <td style="min-height:16pt;padding-bottom:0in;padding-top:0in" height="16"></td>
                                        </tr>
                                        <tr style="min-height:16pt" height="16">
                                                <td style="min-height:16pt;padding-bottom:0in;padding-top:0in" height="16">
                                                        <a href="http://www.datto.com"><font color="#0b5394">www.datto.com</font></a>
                                                </td>
                                        </tr>
                                        <tr style="min-height:16pt" height="16">
                                                <td style="min-height:16pt;padding-bottom:0in;padding-top:0in" height="16">
                                                        <br>
                                                        <a href="http://datto.com/datto-signature/"><img src="http://connect.dattobackup.com/rs/datto/images/generic-sig.png"></a>
                                                        <br>
                                                        <br>
                                                </td>
                                        </tr>
                                        <tr style="min-height:15pt" height="15">
                                                <td style="min-height:15pt;padding-bottom:0in;padding-top:0in" height="15">
                                                        <font size="1">Join the conversation!</font> <a style="font-family:verdana,arial,helvetica,sans-serif" href="http://www.facebook.com/dattoinc"><img src="https://s3.amazonaws.com/images.wisestamp.com/facebook.png" style="padding: 0px 0px 5px; vertical-align: middle;" alt="Facebook" border="0" width="16" height="16"></a><span style="font-family:verdana,arial,helvetica,sans-serif"> </span><a style="font-family:verdana,arial,helvetica,sans-serif" href="https://twitter.com/dattobackup"><img src="http://dattobackup.com/wp-content/uploads/2014/06/twitter.png" style="padding: 0px 0px 5px; vertical-align: middle;" alt="Twitter" border="0" width="16" height="16"></a><span style="font-family:verdana,arial,helvetica,sans-serif"> </span><a style="font-family:verdana,arial,helvetica,sans-serif" href="http://www.linkedin.com/company/1477873?trk=tyah"><img src="https://s3.amazonaws.com/images.wisestamp.com/linkedin.png" style="padding: 0px 0px 5px; vertical-align: middle;" alt="LinkedIn" border="0" width="16" height="16"></a><span style="font-family:verdana,arial,helvetica,sans-serif"> </span><a style="font-family:verdana,arial,helvetica,sans-serif" href="http://pinterest.com/dattobackup/"><img src="https://s3.amazonaws.com/images.wisestamp.com/pinterest.png" style="padding: 0px 0px 5px; vertical-align: middle;" alt="pinterest" border="0" width="16" height="16"></a><span style="font-family:verdana,arial,helvetica,sans-serif"> </span><a style="font-family:verdana,arial,helvetica,sans-serif" href="http://blog.dattobackup.com/blog"><img src="https://s3.amazonaws.com/images.wisestamp.com/blogRSS.png" style="padding: 0px 0px 5px; vertical-align: middle;" alt="Blog RSS" border="0" width="16" height="16"></a><span style="font-family:verdana,arial,helvetica,sans-serif"> </span><a style="font-family:verdana,arial,helvetica,sans-serif" href="http://www.youtube.com/user/DattoInc/featured"><img src="https://s3.amazonaws.com/images.wisestamp.com/youtube.png" style="padding: 0px 0px 5px; vertical-align: middle;" alt="YouTube" border="0" width="16" height="16"></a><span style="font-family:verdana,arial,helvetica,sans-serif"> </span><a style="font-family:verdana,arial,helvetica,sans-serif" href="https://plus.google.com/u/0/108292366419623632143/posts"><img src="https://s3.amazonaws.com/images.wisestamp.com/googlepluspage.png" style="padding: 0px 0px 5px; vertical-align: middle;" alt="Google Plus Page" border="0" width="16" height="16"></a></td></tr></tbody></table><br></div></div>