On Tue, Oct 22, 2019 at 03:57:05PM +0200, Michal Privoznik wrote:
In a few places our code relies on the fact that virAsprintf()
not only prints to allocated string but also that it returns the
length of that string. Fortunately, only few such places were
identified:
https://www.redhat.com/archives/libvir-list/2019-September/msg01382.html
In case of virNWFilterSnoopLeaseFileWrite() and virFilePrintf()
we can use strlen() right after virAsprintf() to calculate the
length. In case of virDoubleToStr() it's only caller checks for
error case only, so we can limit the set of returned values to
just [-1, 0].
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
src/nwfilter/nwfilter_dhcpsnoop.c | 7 +++----
src/util/virfile.c | 5 +++--
src/util/virstring.c | 10 ++++++----
3 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 1494eab132..4294b7456e 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -703,23 +703,25 @@ virStrToDouble(char const *s,
*
* converts double to string with C locale (thread-safe).
*
- * Returns -1 on error, size of the string otherwise.
+ * Returns: on success, -1 otherwise.
0 on success
*/
int
virDoubleToStr(char **strp, double number)
{
virLocale oldlocale;
- int ret = -1;
+ int rc = -1;
int rc;
I don't think initialization is needed here.
if (virLocaleSetRaw(&oldlocale) < 0)
return -1;
- ret = virAsprintf(strp, "%lf", number);
+ rc = virAsprintf(strp, "%lf", number);
virLocaleRevert(&oldlocale);
virLocaleFixupRadix(strp);
- return ret;
+ if (rc < 0)
+ return -1;
+ return 0;
}
Reviewed-by: Ján Tomko <jtomko(a)redhat.com>
Jano