From nobody Mon Feb 9 00:56:29 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1571752803; cv=none; d=zoho.com; s=zohoarc; b=ZJSEI9J8/5WZVbix0r45ofo3YpAC7L+hhD8FWc03+YWM3OaFpaf3iYjA0rElpC5mAMAufAyHsQsSkLP9nL57rlDZGjwsP0Ob6BncxIsRpLD9XGzHRegaDyYnEDuW/yjQEAw+6ZGQXGwtzSxI15nFZESghldyCW6B6+2V2eYwxcs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571752803; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=v5XisFHfgii0yEt7F8XIduwQQHNtFUXodLYtEHcPuZU=; b=FrNxrkMLHMWxNUS5/dQsU2JL01pFLwmu4YOAiAlFm5wTX7a6ijDbo/gJW55ZrSt09eSHnf7h1cBOM/Ict43kGi/LRZflFrbt8MXg1/ekIxGXJFHa3Ki6mLugagsNYArC/u5lRYMp00vbVj8ynhu0cgdhzCK0j45rJ0QYfSdjpvo= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1571752803299641.9806994802641; Tue, 22 Oct 2019 07:00:03 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-47-dlWPMRHxO8OFbHrIhpXW3w-1; Tue, 22 Oct 2019 09:58:37 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9CD0D80183E; Tue, 22 Oct 2019 13:58:31 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6E7405DC18; Tue, 22 Oct 2019 13:58:31 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 1C5374EE6E; Tue, 22 Oct 2019 13:58:31 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x9MDwMlE001516 for ; Tue, 22 Oct 2019 09:58:22 -0400 Received: by smtp.corp.redhat.com (Postfix) id 257F06012D; Tue, 22 Oct 2019 13:58:22 +0000 (UTC) Received: from moe.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id A2EF16012C for ; Tue, 22 Oct 2019 13:58:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571752802; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=v5XisFHfgii0yEt7F8XIduwQQHNtFUXodLYtEHcPuZU=; b=DyYs1Z1xgwnY+NwahKjEOaUZCIMey/ouuIHHcW57f72I/nF9UdBYnkihs6qpLKz27RpQSO FhkfzxdJM1fqYjpCN16MUP5fGz/nX+LvJG/PJUAWPdQsKCe4NO/SmCphLu9yUj1o/VoX/f y/KeAtikXfcjpiJJMHJLG/9CWrFz2+M= From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 22 Oct 2019 15:57:05 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 01/75] src: Don't rely on virAsprintf() returning string length X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: dlWPMRHxO8OFbHrIhpXW3w-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: J=C3=A1n Tomko --- 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/nwfilter/nwfilter_dhcpsnoop.c b/src/nwfilter/nwfilter_dhcp= snoop.c index bf25345e2c..665e7bff9c 100644 --- a/src/nwfilter/nwfilter_dhcpsnoop.c +++ b/src/nwfilter/nwfilter_dhcpsnoop.c @@ -1766,13 +1766,12 @@ virNWFilterSnoopLeaseFileWrite(int lfd, const char = *ifkey, } =20 /* time intf ip dhcpserver */ - len =3D virAsprintf(&lbuf, "%u %s %s %s\n", ipl->timeout, - ifkey, ipstr, dhcpstr); - - if (len < 0) { + if (virAsprintf(&lbuf, "%u %s %s %s\n", ipl->timeout, + ifkey, ipstr, dhcpstr) < 0) { ret =3D -1; goto cleanup; } + len =3D strlen(lbuf); =20 if (safewrite(lfd, lbuf, len) !=3D len) { virReportSystemError(errno, "%s", _("lease file write failed")); diff --git a/src/util/virfile.c b/src/util/virfile.c index c4d544be73..7c97317994 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3394,12 +3394,13 @@ int virFilePrintf(FILE *fp, const char *msg, ...) { va_list vargs; g_autofree char *str =3D NULL; - int ret; + int ret =3D -1; =20 va_start(vargs, msg); =20 - if ((ret =3D virVasprintf(&str, msg, vargs)) < 0) + if (virVasprintf(&str, msg, vargs) < 0) goto cleanup; + ret =3D strlen(str); =20 if (fwrite(str, 1, ret, fp) !=3D ret) { virReportSystemError(errno, "%s", 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. */ int virDoubleToStr(char **strp, double number) { virLocale oldlocale; - int ret =3D -1; + int rc =3D -1; =20 if (virLocaleSetRaw(&oldlocale) < 0) return -1; =20 - ret =3D virAsprintf(strp, "%lf", number); + rc =3D virAsprintf(strp, "%lf", number); =20 virLocaleRevert(&oldlocale); virLocaleFixupRadix(strp); =20 - return ret; + if (rc < 0) + return -1; + return 0; } =20 =20 --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list