Skip to contentSkip to conversation

powerpc/rtas_flash: Use scnprintf() in rtas_flash_read_num()

PATCH In review Thorsten Blum 2 participants 2 messages Jul 22 – Sep 25 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/rtas_flash: Use scnprintf() in rtas_flash_read_num()

Use scnprintf() and its return value to avoid a separate strlen() call.

---
 arch/powerpc/kernel/rtas_flash.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/powerpc/kernel/rtas_flash.c +3 -2
index 583dc16e9d3c..aff8a67e274f 100644
@@ -291,6 +291,7 @@ static ssize_t rtas_flash_read_num(struct file *file, char __user *buf,
 {
 	struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
 	char msg[RTAS_MSG_MAXLEN];
+	size_t len;
 	int status;
 
 	mutex_lock(&rtas_update_flash_mutex);
@@ -298,8 +299,8 @@ static ssize_t rtas_flash_read_num(struct file *file, char __user *buf,
 	mutex_unlock(&rtas_update_flash_mutex);
 
 	/* Read as number */
-	sprintf(msg, "%d\n", status);
-	return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg));
+	len = scnprintf(msg, sizeof(msg), "%d\n", status);
+	return simple_read_from_buffer(buf, count, ppos, msg, len);
 }
 
 /* We could be much more efficient here.  But to keep this function
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Thorsten Blum

Use scnprintf() and its return value to avoid a separate strlen() call.

---
  arch/powerpc/kernel/rtas_flash.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
arch/powerpc/kernel/rtas_flash.c +1 -0
index 583dc16e9d3c..aff8a67e274f 100644
@@ -291,6 +291,7 @@ static ssize_t rtas_flash_read_num(struct file *file, char __user *buf,
  {
  	struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
  	char msg[RTAS_MSG_MAXLEN];
+	size_t len;
  	int status;
 
  	mutex_lock(&rtas_update_flash_mutex);

arch/powerpc/kernel/rtas_flash.c static ssize_t rtas_flash_read_num(struct file *file, char __user *buf, · line 303

Quoted 7 lines
arch/powerpc/kernel/rtas_flash.c +2 -2
@@ -298,8 +299,8 @@ static ssize_t rtas_flash_read_num(struct file *file, char __user *buf,
  	mutex_unlock(&rtas_update_flash_mutex);
 
  	/* Read as number */
-	sprintf(msg, "%d\n", status);
-	return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg));
+	len = scnprintf(msg, sizeof(msg), "%d\n", status);
+	return simple_read_from_buffer(buf, count, ppos, msg, len);

Though there is no real buffer overflow issue here, it is a good idea to convert sprintf to scnprintf. This change helps avoid calling strlen on the same buffer.

Changes looks good to me. Feel free to add:

Quoted 3 lines
arch/powerpc/kernel/rtas_flash.c +0 -0
  }
 
  /* We could be much more efficient here.  But to keep this function