From nobody Fri Oct 18 08:41:16 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 17066376429121012.765302844204; Tue, 30 Jan 2024 10:00:42 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id CE7121E15; Tue, 30 Jan 2024 13:00:41 -0500 (EST) Received: from lists.libvirt.org.85.43.8.in-addr.arpa (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id AF4631D27; Tue, 30 Jan 2024 12:12:33 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 1A85A1D90; Tue, 30 Jan 2024 12:10:42 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 88D1B1C7D for ; Tue, 30 Jan 2024 12:08:44 -0500 (EST) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-59-3rDLhzdhNneWoiGRCDQPkQ-1; Tue, 30 Jan 2024 12:08:36 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A003985A58B for ; Tue, 30 Jan 2024 17:08:36 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.45.242.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0FA3A2166B31 for ; Tue, 30 Jan 2024 17:08:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 X-MC-Unique: 3rDLhzdhNneWoiGRCDQPkQ-1 From: Peter Krempa To: devel@lists.libvirt.org Subject: [PATCH 24/31] virPCIVPDReadVPDBytes: Refactor error handling Date: Tue, 30 Jan 2024 18:08:02 +0100 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Message-ID-Hash: 47J7F6TW7C66ACJNFAKS4B5QCSUF6OX7 X-Message-ID-Hash: 47J7F6TW7C66ACJNFAKS4B5QCSUF6OX7 X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1706637644210100001 Each caller was checking that the function read as many bytes as it expected. Move the check inside virPCIVPDReadVPDBytes and make it report a proper error rather than just a combination of VIR_DEBUG inside the function and a random VIR_INFO in the caller. Signed-off-by: Peter Krempa --- src/util/virpcivpd.c | 73 +++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/src/util/virpcivpd.c b/src/util/virpcivpd.c index 0021a88f2d..10cabff0b9 100644 --- a/src/util/virpcivpd.c +++ b/src/util/virpcivpd.c @@ -361,34 +361,40 @@ virPCIVPDResourceUpdateKeyword(virPCIVPDResource *res, * @offset: The offset at which bytes need to be read. * @csum: A pointer to a byte containing the current checksum value. Mutat= ed by this function. * - * Returns: the number of VPD bytes read from the specified file descripto= r. The csum value is + * Returns 0 if exactly @count bytes were read from @vpdFileFd. The csum v= alue is * also modified as bytes are read. If an error occurs while reading data = from the VPD file - * descriptor, it is reported and -1 is returned to the caller. If EOF is = occurred, 0 is returned - * to the caller. + * descriptor, it is reported and -1 is returned to the caller. */ -static size_t -virPCIVPDReadVPDBytes(int vpdFileFd, uint8_t *buf, size_t count, off_t off= set, uint8_t *csum) +static int +virPCIVPDReadVPDBytes(int vpdFileFd, + uint8_t *buf, + size_t count, + off_t offset, + uint8_t *csum) { ssize_t numRead =3D pread(vpdFileFd, buf, count, offset); - if (numRead =3D=3D -1) { - VIR_DEBUG("Unable to read %zu bytes at offset %zd from fd: %d", - count, (ssize_t)offset, vpdFileFd); - } else if (numRead) { - /* - * Update the checksum for every byte read. Per the PCI(e) specs - * the checksum is correct if the sum of all bytes in VPD from - * VPD address 0 up to and including the VPD-R RV field's first - * data byte is zero. - */ - while (count--) { - *csum +=3D *buf; - buf++; - } + if (numRead !=3D count) { + virReportError(VIR_ERR_OPERATION_FAILED, "%s", + _("failed to read the PCI VPD data")); + return -1; + } + + /* + * Update the checksum for every byte read. Per the PCI(e) specs + * the checksum is correct if the sum of all bytes in VPD from + * VPD address 0 up to and including the VPD-R RV field's first + * data byte is zero. + */ + while (count--) { + *csum +=3D *buf; + buf++; } - return numRead; + + return 0; } + /** * virPCIVPDParseVPDLargeResourceFields: * @vpdFileFd: A file descriptor associated with a file containing PCI dev= ice VPD. @@ -423,12 +429,9 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, ui= nt16_t resPos, uint16_t re g_autofree char *fieldValue =3D NULL; /* Keyword resources consist of keywords (2 ASCII bytes per the sp= ec) and 1-byte length. */ - if (virPCIVPDReadVPDBytes(vpdFileFd, buf, 3, fieldPos, csum) !=3D = 3) { - /* Invalid field encountered which means the resource itself i= s invalid too. Report - * That VPD has invalid format and bail. */ - VIR_INFO("Could not read a resource field header - VPD has inv= alid format"); + if (virPCIVPDReadVPDBytes(vpdFileFd, buf, 3, fieldPos, csum) < 0) return false; - } + fieldDataLen =3D buf[2]; /* Change the position to the field's data portion skipping the ke= yword and length bytes. */ fieldPos +=3D 3; @@ -474,10 +477,10 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, u= int16_t resPos, uint16_t re VIR_INFO("A field data length violates the resource length bou= ndary."); return false; } - if (virPCIVPDReadVPDBytes(vpdFileFd, buf, bytesToRead, fieldPos, c= sum) !=3D bytesToRead) { - VIR_INFO("Could not parse a resource field data - VPD has inva= lid format"); + + if (virPCIVPDReadVPDBytes(vpdFileFd, buf, bytesToRead, fieldPos, c= sum) < 0) return false; - } + /* Advance the position to the first byte of the next field. */ fieldPos +=3D fieldDataLen; @@ -566,10 +569,9 @@ virPCIVPDParseVPDLargeResourceString(int vpdFileFd, ui= nt16_t resPos, /* The resource value is not NULL-terminated so add one more byte. */ g_autofree char *buf =3D g_malloc0(resDataLen + 1); - if (virPCIVPDReadVPDBytes(vpdFileFd, (uint8_t *)buf, resDataLen, resPo= s, csum) !=3D resDataLen) { - VIR_INFO("Could not read a part of a resource - VPD has invalid fo= rmat"); + if (virPCIVPDReadVPDBytes(vpdFileFd, (uint8_t *)buf, resDataLen, resPo= s, csum) < 0) return false; - } + resValue =3D g_strdup(g_strstrip(buf)); if (!virPCIVPDResourceIsValidTextValue(resValue)) { VIR_INFO("The string resource has invalid characters in its value"= ); @@ -610,8 +612,8 @@ virPCIVPDParse(int vpdFileFd) while (resPos <=3D PCI_VPD_ADDR_MASK) { /* Read the resource data type tag. */ - if (virPCIVPDReadVPDBytes(vpdFileFd, &tag, 1, resPos, &csum) !=3D = 1) - break; + if (virPCIVPDReadVPDBytes(vpdFileFd, &tag, 1, resPos, &csum) < 0) + return NULL; /* 0x80 =3D=3D 0b10000000 - the large resource data type flag. */ if (tag & PCI_VPD_LARGE_RESOURCE_FLAG) { @@ -620,9 +622,10 @@ virPCIVPDParse(int vpdFileFd) * where the end tag should be. */ break; } + /* Read the two length bytes of the large resource record. */ - if (virPCIVPDReadVPDBytes(vpdFileFd, headerBuf, 2, resPos + 1,= &csum) !=3D 2) - break; + if (virPCIVPDReadVPDBytes(vpdFileFd, headerBuf, 2, resPos + 1,= &csum) < 0) + return NULL; resDataLen =3D headerBuf[0] + (headerBuf[1] << 8); /* Change the position to the byte following the tag and lengt= h bytes. */ --=20 2.43.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org