[PATCH] drm/nouveau: replace snprintf() with scnprintf() in nvkm_snprintbf()

Seyediman Seyedarab posted 1 patch 2 months, 1 week ago
drivers/gpu/drm/nouveau/nvkm/core/enum.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] drm/nouveau: replace snprintf() with scnprintf() in nvkm_snprintbf()
Posted by Seyediman Seyedarab 2 months, 1 week ago
snprintf() returns the number of characters that *would* have been
written, which can overestimate how much you actually wrote to the
buffer in case of truncation. That leads to 'data += this' advancing
the pointer past the end of the buffer and size going negative.

Switching to scnprintf() prevents potential buffer overflows and ensures
consistent behavior when building the output string.

Signed-off-by: Seyediman Seyedarab <ImanDevel@gmail.com>
---
 drivers/gpu/drm/nouveau/nvkm/core/enum.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/nouveau/nvkm/core/enum.c b/drivers/gpu/drm/nouveau/nvkm/core/enum.c
index b9581feb24cc..a23b40b27b81 100644
--- a/drivers/gpu/drm/nouveau/nvkm/core/enum.c
+++ b/drivers/gpu/drm/nouveau/nvkm/core/enum.c
@@ -44,7 +44,7 @@ nvkm_snprintbf(char *data, int size, const struct nvkm_bitfield *bf, u32 value)
 	bool space = false;
 	while (size >= 1 && bf->name) {
 		if (value & bf->mask) {
-			int this = snprintf(data, size, "%s%s",
+			int this = scnprintf(data, size, "%s%s",
 					    space ? " " : "", bf->name);
 			size -= this;
 			data += this;
-- 
2.50.1
Re: [PATCH] drm/nouveau: replace snprintf() with scnprintf() in nvkm_snprintbf()
Posted by Seyediman Seyedarab 2 months ago
On 25/07/24 03:59PM, Seyediman Seyedarab wrote:
> snprintf() returns the number of characters that *would* have been
> written, which can overestimate how much you actually wrote to the
> buffer in case of truncation. That leads to 'data += this' advancing
> the pointer past the end of the buffer and size going negative.
> 
> Switching to scnprintf() prevents potential buffer overflows and ensures
> consistent behavior when building the output string.
> 
> Signed-off-by: Seyediman Seyedarab <ImanDevel@gmail.com>
> ---
>  drivers/gpu/drm/nouveau/nvkm/core/enum.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/nouveau/nvkm/core/enum.c b/drivers/gpu/drm/nouveau/nvkm/core/enum.c
> index b9581feb24cc..a23b40b27b81 100644
> --- a/drivers/gpu/drm/nouveau/nvkm/core/enum.c
> +++ b/drivers/gpu/drm/nouveau/nvkm/core/enum.c
> @@ -44,7 +44,7 @@ nvkm_snprintbf(char *data, int size, const struct nvkm_bitfield *bf, u32 value)
>  	bool space = false;
>  	while (size >= 1 && bf->name) {
>  		if (value & bf->mask) {
> -			int this = snprintf(data, size, "%s%s",
> +			int this = scnprintf(data, size, "%s%s",
>  					    space ? " " : "", bf->name);
>  			size -= this;
>  			data += this;
> -- 
> 2.50.1
> 

Hi there,

Just following up on this patch. Please let me know if there's any
feedback or if further changes are needed.

Regards,
Seyediman
Re: [PATCH] drm/nouveau: replace snprintf() with scnprintf() in nvkm_snprintbf()
Posted by Danilo Krummrich 1 month, 3 weeks ago
On 7/24/25 9:59 PM, Seyediman Seyedarab wrote:
> snprintf() returns the number of characters that *would* have been
> written, which can overestimate how much you actually wrote to the
> buffer in case of truncation. That leads to 'data += this' advancing
> the pointer past the end of the buffer and size going negative.
> 
> Switching to scnprintf() prevents potential buffer overflows and ensures
> consistent behavior when building the output string.
> 
> Signed-off-by: Seyediman Seyedarab <ImanDevel@gmail.com>

Applied to drm-misc-next, thanks!