Re: [PATCH] pata_lagacy: Handle failed ATA timing computation in opti82c46x_set_piomode

From: Haonan Li
Date: Tue Oct 17 2023 - 20:55:52 EST


The function opti82c46x_set_piomode utilizes the ata_timing_compute()
to determine the appropriate ATA timings for a given device. However,
in certain conditions where the ata_timing_find_mode() function does
not find a valid mode, ata_timing_compute() returns an error (-EINVAL),
leaving the tp struct uninitialized.

This patch checks the return value of ata_timing_compute() and print
err message. This avoids any potential use of uninitialized `tp`
struct in the opti82c46x_set_piomode function.

Signed-off-by: Haonan Li <lihaonan1105@xxxxxxxxx>
---
drivers/ata/pata_legacy.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/ata/pata_legacy.c b/drivers/ata/pata_legacy.c
index 448a511cbc17..3c7163f97aaf 100644
--- a/drivers/ata/pata_legacy.c
+++ b/drivers/ata/pata_legacy.c
@@ -579,12 +579,19 @@ static void opti82c46x_set_piomode(struct ata_port *ap, struct ata_device *adev)
clock = 1000000000 / khz[sysclk];

/* Get the timing data in cycles */
- ata_timing_compute(adev, adev->pio_mode, &t, clock, 1000);
+ if (ata_timing_compute(adev, adev->pio_mode, &t, clock, 1000)) {
+ dev_err(ap->dev, "adev: unknown mode %d\n", adev->pio_mode);
+ return;
+ }

/* Setup timing is shared */
if (pair) {
struct ata_timing tp;
- ata_timing_compute(pair, pair->pio_mode, &tp, clock, 1000);
+
+ if (ata_timing_compute(pair, pair->pio_mode, &tp, clock, 1000)) {
+ dev_err(ap->dev, "pair: unknown mode %d\n", pair->pio_mode);
+ return;
+ }

ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
}
--
2.34.1