From: "Daniel P. Berrangé" <berrange@redhat.com>
To: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Cc: qemu-devel@nongnu.org, armbru@redhat.com
Subject: Re: [PATCH v3 23/24] hw/i2c/pmbus: fix undefined behavior in pmbus_direct_mode2data
Date: Thu, 4 Jun 2026 17:10:36 +0100 [thread overview]
Message-ID: <aiGjfMQamilAzhDg@redhat.com> (raw)
In-Reply-To: <20260516-qom-tests-v3-23-3f20c3a029a7@redhat.com>
On Sat, May 16, 2026 at 11:59:22AM +0400, Marc-André Lureau wrote:
> The intermediate result of (Y * 10^-R - b) / m can be negative when
> the bias (b) is large and the raw register value is small (e.g. zero
> on an uninitialized device). Assigning that negative double to uint32_t
> is undefined behavior, caught by UBSan/clang.
>
> Use a double intermediate and clamp negative results to zero.
>
> Fixes: 3746d5c15e70 ("hw/i2c: add support for PMBus")
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
> hw/i2c/pmbus_device.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/hw/i2c/pmbus_device.c b/hw/i2c/pmbus_device.c
> index 853dc4b4342..861f5b4fb63 100644
> --- a/hw/i2c/pmbus_device.c
> +++ b/hw/i2c/pmbus_device.c
> @@ -23,8 +23,8 @@ uint16_t pmbus_data2direct_mode(PMBusCoefficients c, uint32_t value)
> uint32_t pmbus_direct_mode2data(PMBusCoefficients c, uint16_t value)
> {
> /* X = (Y * 10^-R - b) / m */
> - uint32_t x = (value / pow(10, c.R) - c.b) / c.m;
> - return x;
> + double x = (value / pow(10, c.R) - c.b) / c.m;
> + return (x > 0) ? (uint32_t)x : 0;
Couldn't 'x' exceed G_MAXUINT32 and thus truncate here which
while not undefined would still seem undesirable ? If so, then
perhaps
return (x > 0
? (x < G_MAXUINT32 ? (uint32_t)x : G_MAXUINT32)
: 0);
?
> }
>
> uint16_t pmbus_data2linear_mode(uint16_t value, int exp)
>
> --
> 2.54.0
>
>
With regards,
Daniel
--
|: https://berrange.com ~~ https://hachyderm.io/@berrange :|
|: https://libvirt.org ~~ https://entangle-photo.org :|
|: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
next prev parent reply other threads:[~2026-06-04 16:11 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-16 7:58 [PATCH v3 00/24] Fix various QOM object life-cycle issues Marc-André Lureau
2026-05-16 7:59 ` [PATCH v3 01/24] ui/vt100: add vt100_fini() check Marc-André Lureau
2026-06-04 15:56 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 02/24] hw/pci: handle missing bus in prop_pci_busnr_get Marc-André Lureau
2026-06-04 14:50 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 03/24] chardev/char-socket: handle NULL addr in char_socket_get_addr Marc-André Lureau
2026-06-04 14:47 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 04/24] hw/pci-bridge: handle missing parent in prop_pxb_uid_get Marc-André Lureau
2026-06-04 14:47 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 05/24] hw/pci-host/i440fx: handle NULL bus in pci-hole64 getters Marc-André Lureau
2026-06-04 14:48 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 06/24] hw/pci-host/q35: " Marc-André Lureau
2026-06-04 14:49 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 07/24] hw/ipmi: reject NULL 'bmc' property rather than crash Marc-André Lureau
2026-06-04 16:01 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 08/24] hw/xlnx_dp: reject NULL 'dpdma' " Marc-André Lureau
2026-06-04 16:02 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 09/24] hw/intc/apic: move checks to realize() Marc-André Lureau
2026-06-04 16:03 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 10/24] backends/cryptodev-lkcf: skip cleanup when not initialized Marc-André Lureau
2026-06-04 16:04 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 11/24] system/ioport: minor code simplification Marc-André Lureau
2026-06-04 15:06 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 12/24] hw/core/machine: free shim_filename on finalization Marc-André Lureau
2026-06-04 14:50 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 13/24] net/filter: free old values in property setters Marc-André Lureau
2026-06-04 14:52 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 14/24] target/i386/sev: add finalize functions and fix leaking setters Marc-André Lureau
2026-06-04 14:55 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 15/24] target/i386/kvm/tdx: free strings in tdx_guest_finalize Marc-André Lureau
2026-06-04 14:56 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 16/24] hw/i386/nitro_enclave: add instance finalize Marc-André Lureau
2026-06-04 15:01 ` Daniel P. Berrangé
2026-06-04 20:18 ` Marc-André Lureau
2026-05-16 7:59 ` [PATCH v3 17/24] hw/i386/pc: free pcspk on finalization Marc-André Lureau
2026-06-04 15:02 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 18/24] hw/tpm: free PPI buffer " Marc-André Lureau
2026-05-21 20:17 ` Arun Menon
2026-05-21 21:16 ` Marc-André Lureau
2026-05-29 9:17 ` Peter Maydell
2026-05-29 10:55 ` Marc-André Lureau
2026-05-16 7:59 ` [PATCH v3 19/24] hw/loongarch/virt: free flash devices and OEM strings " Marc-André Lureau
2026-05-16 7:59 ` [PATCH v3 20/24] hw/ppc/spapr: free host_model and host_serial " Marc-André Lureau
2026-06-04 16:05 ` Daniel P. Berrangé
2026-05-16 7:59 ` [PATCH v3 21/24] target/riscv: fix general_user_opts hash table leak Marc-André Lureau
2026-06-03 1:01 ` Alistair Francis
2026-05-16 7:59 ` [PATCH v3 22/24] target/riscv: use hash table as set for user_options Marc-André Lureau
2026-06-03 1:02 ` Alistair Francis
2026-05-16 7:59 ` [PATCH v3 23/24] hw/i2c/pmbus: fix undefined behavior in pmbus_direct_mode2data Marc-André Lureau
2026-06-04 16:10 ` Daniel P. Berrangé [this message]
2026-06-04 20:34 ` Marc-André Lureau
2026-05-16 7:59 ` [PATCH v3 24/24] qtest: add "qom-tests" command Marc-André Lureau
2026-06-04 16:14 ` Daniel P. Berrangé
2026-05-20 13:34 ` [PATCH v3 00/24] Fix various QOM object life-cycle issues Marc-André Lureau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aiGjfMQamilAzhDg@redhat.com \
--to=berrange@redhat.com \
--cc=armbru@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.