2022-11-26 14:02:52

by wangyufen

[permalink] [raw]
Subject: [PATCH v3 1/2] RDMA/hfi1: Fix error return code in parse_platform_config()

In the previous while loop, "ret" may be assigned zero, so the error
return code may be incorrectly set to 0 instead of -EINVAL.
Add bail_with_einval goto label and covert all "goto bail;" to "goto
bail_with_einval:" where it's appropriate. Add dropping some duplicative
"ret = -EINVAL;" lines, as Andy suggessted.

Fixes: 97167e813415 ("staging/rdma/hfi1: Tune for unknown channel if configuration file is absent")
Signed-off-by: Wang Yufen <[email protected]>
Reviewed-by: Andy Shevchenko <[email protected]>
---
drivers/infiniband/hw/hfi1/firmware.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/drivers/infiniband/hw/hfi1/firmware.c b/drivers/infiniband/hw/hfi1/firmware.c
index 1d77514..44d8628 100644
--- a/drivers/infiniband/hw/hfi1/firmware.c
+++ b/drivers/infiniband/hw/hfi1/firmware.c
@@ -1730,7 +1730,7 @@ int parse_platform_config(struct hfi1_devdata *dd)
u32 *ptr = NULL;
u32 header1 = 0, header2 = 0, magic_num = 0, crc = 0, file_length = 0;
u32 record_idx = 0, table_type = 0, table_length_dwords = 0;
- int ret = -EINVAL; /* assume failure */
+ int ret;

/*
* For integrated devices that did not fall back to the default file,
@@ -1743,7 +1743,7 @@ int parse_platform_config(struct hfi1_devdata *dd)

if (!dd->platform_config.data) {
dd_dev_err(dd, "%s: Missing config file\n", __func__);
- goto bail;
+ goto bail_with_einval;
}
ptr = (u32 *)dd->platform_config.data;

@@ -1751,7 +1751,7 @@ int parse_platform_config(struct hfi1_devdata *dd)
ptr++;
if (magic_num != PLATFORM_CONFIG_MAGIC_NUM) {
dd_dev_err(dd, "%s: Bad config file\n", __func__);
- goto bail;
+ goto bail_with_einval;
}

/* Field is file size in DWORDs */
@@ -1774,7 +1774,7 @@ int parse_platform_config(struct hfi1_devdata *dd)
if (file_length > dd->platform_config.size) {
dd_dev_info(dd, "%s:File claims to be larger than read size\n",
__func__);
- goto bail;
+ goto bail_with_einval;
} else if (file_length < dd->platform_config.size) {
dd_dev_info(dd,
"%s:File claims to be smaller than read size, continuing\n",
@@ -1794,7 +1794,7 @@ int parse_platform_config(struct hfi1_devdata *dd)
dd_dev_err(dd, "%s: Failed validation at offset %ld\n",
__func__, (ptr - (u32 *)
dd->platform_config.data));
- goto bail;
+ goto bail_with_einval;
}

record_idx = *ptr &
@@ -1837,7 +1837,7 @@ int parse_platform_config(struct hfi1_devdata *dd)
__func__, table_type,
(ptr - (u32 *)
dd->platform_config.data));
- goto bail; /* We don't trust this file now */
+ goto bail_with_einval; /* We don't trust this file now */
}
pcfgcache->config_tables[table_type].table = ptr;
} else {
@@ -1856,7 +1856,7 @@ int parse_platform_config(struct hfi1_devdata *dd)
__func__, table_type,
(ptr -
(u32 *)dd->platform_config.data));
- goto bail; /* We don't trust this file now */
+ goto bail_with_einval; /* We don't trust this file now */
}
pcfgcache->config_tables[table_type].table_metadata =
ptr;
@@ -1873,8 +1873,7 @@ int parse_platform_config(struct hfi1_devdata *dd)
dd_dev_err(dd, "%s: Failed CRC check at offset %ld\n",
__func__, (ptr -
(u32 *)dd->platform_config.data));
- ret = -EINVAL;
- goto bail;
+ goto bail_with_einval;
}
/* Jump the CRC DWORD */
ptr++;
@@ -1882,6 +1881,9 @@ int parse_platform_config(struct hfi1_devdata *dd)

pcfgcache->cache_valid = 1;
return 0;
+
+bail_with_einval:
+ ret = -EINVAL;
bail:
memset(pcfgcache, 0, sizeof(struct platform_config_cache));
return ret;
--
1.8.3.1


2022-11-28 12:03:13

by Leon Romanovsky

[permalink] [raw]
Subject: Re: [PATCH v3 1/2] RDMA/hfi1: Fix error return code in parse_platform_config()

On Sat, Nov 26, 2022 at 09:50:53PM +0800, Wang Yufen wrote:
> In the previous while loop, "ret" may be assigned zero, so the error
> return code may be incorrectly set to 0 instead of -EINVAL.
> Add bail_with_einval goto label and covert all "goto bail;" to "goto
> bail_with_einval:" where it's appropriate. Add dropping some duplicative
> "ret = -EINVAL;" lines, as Andy suggessted.
>
> Fixes: 97167e813415 ("staging/rdma/hfi1: Tune for unknown channel if configuration file is absent")
> Signed-off-by: Wang Yufen <[email protected]>
> Reviewed-by: Andy Shevchenko <[email protected]>
> ---
> drivers/infiniband/hw/hfi1/firmware.c | 20 +++++++++++---------
> 1 file changed, 11 insertions(+), 9 deletions(-)

<...>


> +bail_with_einval:
> + ret = -EINVAL;

Sorry for being late, but no. It can be seen as anti-pattern as it
causes to the situations where unrelated code changes can potentially
overwrite return value. Please set valid return code before calling to
goto.

Thanks

> bail:
> memset(pcfgcache, 0, sizeof(struct platform_config_cache));
> return ret;
> --
> 1.8.3.1
>

2022-11-29 01:50:40

by wangyufen

[permalink] [raw]
Subject: Re: [PATCH v3 1/2] RDMA/hfi1: Fix error return code in parse_platform_config()



在 2022/11/28 19:57, Leon Romanovsky 写道:
> On Sat, Nov 26, 2022 at 09:50:53PM +0800, Wang Yufen wrote:
>> In the previous while loop, "ret" may be assigned zero, so the error
>> return code may be incorrectly set to 0 instead of -EINVAL.
>> Add bail_with_einval goto label and covert all "goto bail;" to "goto
>> bail_with_einval:" where it's appropriate. Add dropping some duplicative
>> "ret = -EINVAL;" lines, as Andy suggessted.
>>
>> Fixes: 97167e813415 ("staging/rdma/hfi1: Tune for unknown channel if configuration file is absent")
>> Signed-off-by: Wang Yufen <[email protected]>
>> Reviewed-by: Andy Shevchenko <[email protected]>
>> ---
>> drivers/infiniband/hw/hfi1/firmware.c | 20 +++++++++++---------
>> 1 file changed, 11 insertions(+), 9 deletions(-)
>
> <...>
>
>
>> +bail_with_einval:
>> + ret = -EINVAL;
>
> Sorry for being late, but no. It can be seen as anti-pattern as it
> causes to the situations where unrelated code changes can potentially
> overwrite return value. Please set valid return code before calling to
> goto.

OK, will change in v4.
Thanks
>
> Thanks
>
>> bail:
>> memset(pcfgcache, 0, sizeof(struct platform_config_cache));
>> return ret;
>> --
>> 1.8.3.1
>>