* Re: [PATCH v2] remoteproc: mtk_scp: change the snprintf() checking
2025-10-27 7:08 [PATCH v2] remoteproc: mtk_scp: change the snprintf() checking Dan Carpenter
@ 2025-10-27 9:32 ` AngeloGioacchino Del Regno
2025-10-27 11:55 ` Zhongqiu Han
2025-11-10 17:43 ` Mathieu Poirier
2 siblings, 0 replies; 4+ messages in thread
From: AngeloGioacchino Del Regno @ 2025-10-27 9:32 UTC (permalink / raw)
To: Dan Carpenter, Bjorn Andersson
Cc: Mathieu Poirier, Matthias Brugger, linux-remoteproc, linux-kernel,
linux-arm-kernel, linux-mediatek, kernel-janitors
Il 27/10/25 08:08, Dan Carpenter ha scritto:
> The snprintf() calls here work but they have several minor style issues:
>
> 1) It uses ARRAY_SIZE() which is the number of elements in an array.
> Since were talking about char that works, but it's more common to
> use sizeof() which is the number of bytes.
> 2) The printf format is "%1d". The "1" ensures we always print at
> least 1 character but since numbers all have at least 1 digit this
> can be removed.
> 3) The kernel implementation of snprintf() cannot return negative error
> codes. Also these particular calls to snprintf() can't return zero
> and the code to handle that zero return is sort of questionable.
> 4) In the current kernel the only "core_id" we print is "0" but if it
> was more than 9 then the output would be truncated so GCC complains.
> Add an "a >= sizeof(scp_fw_file)" check for output which is too long.
>
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
> ---
> v2: The v1 introduced a W=1 warning because of the truncation issue.
> It's a false positive because GCC assumes that "core_id" can be
> every possible value of int but actually it can only be zero. And
> also generally, in the kernel, truncating is fine and it is fine
> here too.
>
> But let's use that as an opportunity to do more cleanups.
>
> drivers/remoteproc/mtk_scp.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/remoteproc/mtk_scp.c b/drivers/remoteproc/mtk_scp.c
> index 10e3f9eb8cd2..db8fd045468d 100644
> --- a/drivers/remoteproc/mtk_scp.c
> +++ b/drivers/remoteproc/mtk_scp.c
> @@ -1127,11 +1127,11 @@ static const char *scp_get_default_fw_path(struct device *dev, int core_id)
> return ERR_PTR(-EINVAL);
>
> if (core_id >= 0)
> - ret = snprintf(scp_fw_file, ARRAY_SIZE(scp_fw_file), "scp_c%1d", core_id);
> + ret = snprintf(scp_fw_file, sizeof(scp_fw_file), "scp_c%d", core_id);
> else
> - ret = snprintf(scp_fw_file, ARRAY_SIZE(scp_fw_file), "scp");
> - if (ret <= 0)
> - return ERR_PTR(ret);
> + ret = snprintf(scp_fw_file, sizeof(scp_fw_file), "scp");
> + if (ret >= sizeof(scp_fw_file))
> + return ERR_PTR(-ENAMETOOLONG);
>
> /* Not using strchr here, as strlen of a const gets optimized by compiler */
> soc = &compatible[strlen("mediatek,")];
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v2] remoteproc: mtk_scp: change the snprintf() checking
2025-10-27 7:08 [PATCH v2] remoteproc: mtk_scp: change the snprintf() checking Dan Carpenter
2025-10-27 9:32 ` AngeloGioacchino Del Regno
@ 2025-10-27 11:55 ` Zhongqiu Han
2025-11-10 17:43 ` Mathieu Poirier
2 siblings, 0 replies; 4+ messages in thread
From: Zhongqiu Han @ 2025-10-27 11:55 UTC (permalink / raw)
To: Dan Carpenter, Bjorn Andersson
Cc: Mathieu Poirier, Matthias Brugger, AngeloGioacchino Del Regno,
linux-remoteproc, linux-kernel, linux-arm-kernel, linux-mediatek,
kernel-janitors
On 10/27/2025 3:08 PM, Dan Carpenter wrote:
> The snprintf() calls here work but they have several minor style issues:
>
> 1) It uses ARRAY_SIZE() which is the number of elements in an array.
> Since were talking about char that works, but it's more common to
> use sizeof() which is the number of bytes.
> 2) The printf format is "%1d". The "1" ensures we always print at
> least 1 character but since numbers all have at least 1 digit this
> can be removed.
> 3) The kernel implementation of snprintf() cannot return negative error
> codes. Also these particular calls to snprintf() can't return zero
> and the code to handle that zero return is sort of questionable.
> 4) In the current kernel the only "core_id" we print is "0" but if it
> was more than 9 then the output would be truncated so GCC complains.
> Add an "a >= sizeof(scp_fw_file)" check for output which is too long.
>
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
Reviewed-by: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
> ---
> v2: The v1 introduced a W=1 warning because of the truncation issue.
> It's a false positive because GCC assumes that "core_id" can be
> every possible value of int but actually it can only be zero. And
> also generally, in the kernel, truncating is fine and it is fine
> here too.
>
> But let's use that as an opportunity to do more cleanups.
>
> drivers/remoteproc/mtk_scp.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/remoteproc/mtk_scp.c b/drivers/remoteproc/mtk_scp.c
> index 10e3f9eb8cd2..db8fd045468d 100644
> --- a/drivers/remoteproc/mtk_scp.c
> +++ b/drivers/remoteproc/mtk_scp.c
> @@ -1127,11 +1127,11 @@ static const char *scp_get_default_fw_path(struct device *dev, int core_id)
> return ERR_PTR(-EINVAL);
>
> if (core_id >= 0)
> - ret = snprintf(scp_fw_file, ARRAY_SIZE(scp_fw_file), "scp_c%1d", core_id);
> + ret = snprintf(scp_fw_file, sizeof(scp_fw_file), "scp_c%d", core_id);
> else
> - ret = snprintf(scp_fw_file, ARRAY_SIZE(scp_fw_file), "scp");
> - if (ret <= 0)
> - return ERR_PTR(ret);
> + ret = snprintf(scp_fw_file, sizeof(scp_fw_file), "scp");
> + if (ret >= sizeof(scp_fw_file))
> + return ERR_PTR(-ENAMETOOLONG);
>
> /* Not using strchr here, as strlen of a const gets optimized by compiler */
> soc = &compatible[strlen("mediatek,")];
--
Thx and BRs,
Zhongqiu Han
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v2] remoteproc: mtk_scp: change the snprintf() checking
2025-10-27 7:08 [PATCH v2] remoteproc: mtk_scp: change the snprintf() checking Dan Carpenter
2025-10-27 9:32 ` AngeloGioacchino Del Regno
2025-10-27 11:55 ` Zhongqiu Han
@ 2025-11-10 17:43 ` Mathieu Poirier
2 siblings, 0 replies; 4+ messages in thread
From: Mathieu Poirier @ 2025-11-10 17:43 UTC (permalink / raw)
To: Dan Carpenter
Cc: Bjorn Andersson, Matthias Brugger, AngeloGioacchino Del Regno,
linux-remoteproc, linux-kernel, linux-arm-kernel, linux-mediatek,
kernel-janitors
On Mon, Oct 27, 2025 at 10:08:51AM +0300, Dan Carpenter wrote:
> The snprintf() calls here work but they have several minor style issues:
>
> 1) It uses ARRAY_SIZE() which is the number of elements in an array.
> Since were talking about char that works, but it's more common to
> use sizeof() which is the number of bytes.
> 2) The printf format is "%1d". The "1" ensures we always print at
> least 1 character but since numbers all have at least 1 digit this
> can be removed.
> 3) The kernel implementation of snprintf() cannot return negative error
> codes. Also these particular calls to snprintf() can't return zero
> and the code to handle that zero return is sort of questionable.
> 4) In the current kernel the only "core_id" we print is "0" but if it
> was more than 9 then the output would be truncated so GCC complains.
> Add an "a >= sizeof(scp_fw_file)" check for output which is too long.
>
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---
> v2: The v1 introduced a W=1 warning because of the truncation issue.
> It's a false positive because GCC assumes that "core_id" can be
> every possible value of int but actually it can only be zero. And
> also generally, in the kernel, truncating is fine and it is fine
> here too.
>
> But let's use that as an opportunity to do more cleanups.
>
> drivers/remoteproc/mtk_scp.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/remoteproc/mtk_scp.c b/drivers/remoteproc/mtk_scp.c
> index 10e3f9eb8cd2..db8fd045468d 100644
> --- a/drivers/remoteproc/mtk_scp.c
> +++ b/drivers/remoteproc/mtk_scp.c
> @@ -1127,11 +1127,11 @@ static const char *scp_get_default_fw_path(struct device *dev, int core_id)
> return ERR_PTR(-EINVAL);
>
> if (core_id >= 0)
> - ret = snprintf(scp_fw_file, ARRAY_SIZE(scp_fw_file), "scp_c%1d", core_id);
> + ret = snprintf(scp_fw_file, sizeof(scp_fw_file), "scp_c%d", core_id);
> else
> - ret = snprintf(scp_fw_file, ARRAY_SIZE(scp_fw_file), "scp");
> - if (ret <= 0)
> - return ERR_PTR(ret);
> + ret = snprintf(scp_fw_file, sizeof(scp_fw_file), "scp");
> + if (ret >= sizeof(scp_fw_file))
> + return ERR_PTR(-ENAMETOOLONG);
>
Applied.
Thanks,
Mathieu
> /* Not using strchr here, as strlen of a const gets optimized by compiler */
> soc = &compatible[strlen("mediatek,")];
> --
> 2.51.0
>
^ permalink raw reply [flat|nested] 4+ messages in thread