Skip to contentSkip to conversation

staging: atomisp: use kstrdup to replace kmalloc and memcpy

PATCH Superseded Hari Prasath 2 participants 5 messages Jul 7, 2017 – Jul 10, 2017

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: atomisp: use kstrdup to replace kmalloc and memcpy

kstrdup kernel primitive can be used to replace kmalloc followed by string copy. This was reported by coccinelle tool

---
 .../media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c       | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c +3 -7
index 34cc56f..68db87b 100644
@@ -144,14 +144,10 @@ sh_css_load_blob_info(const char *fw, const struct ia_css_fw_info *bi, struct ia
 	)
 	{
 		char *namebuffer;
-		int namelength = (int)strlen(name);
-
-		namebuffer = (char *) kmalloc(namelength + 1, GFP_KERNEL);
-		if (namebuffer == NULL)
-			return IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
-
-		memcpy(namebuffer, name, namelength + 1);
 
+		namebuffer = kstrdup(name, GFP_KERNEL);
+		if (!namebuffer)
+			return -ENOMEM;
 		bd->name = fw_minibuffer[index].name = namebuffer;
 	} else {
 		bd->name = name;
Signature · 1 line
2.10.0.GIT
#1 lore raw

Hi Hari,

Quoted 17 lines · Hari Prasath

kstrdup kernel primitive can be used to replace kmalloc followed by string copy. This was reported by coccinelle tool

---
 .../media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c       | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c +0 -1
index 34cc56f..68db87b 100644
@@ -144,14 +144,10 @@ sh_css_load_blob_info(const char *fw, const struct ia_css_fw_info *bi, struct ia
 	)
 	{
 		char *namebuffer;
-		int namelength = (int)strlen(name);

drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c sh_css_load_blob_info(const char *fw, const struct ia_css_fw_info *bi, struct ia · line 150

Quoted 10 lines
drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c +3 -6
-
-		namebuffer = (char *) kmalloc(namelength + 1, GFP_KERNEL);
-		if (namebuffer == NULL)
-			return IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
-
-		memcpy(namebuffer, name, namelength + 1);
 
+		namebuffer = kstrdup(name, GFP_KERNEL);
+		if (!namebuffer)
+			return -ENOMEM;

The patch also changes the return value in error cases. I believe the caller(s) expect to get errors in the IA_CCS_ERR_* range.

Quoted 3 lines
drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c +0 -0
 		bd->name = fw_minibuffer[index].name = namebuffer;
 	} else {
 		bd->name = name;
Signature · 4 lines
Regards,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#2 lore raw
Quoted 27 lines · Sakari Ailus

Hi Hari,

Quoted 24 lines · Hari Prasath

kstrdup kernel primitive can be used to replace kmalloc followed by string copy. This was reported by coccinelle tool

---
 .../media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c       | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c +0 -7
index 34cc56f..68db87b 100644
@@ -144,14 +144,10 @@ sh_css_load_blob_info(const char *fw, const struct ia_css_fw_info *bi, struct ia
      )
      {
              char *namebuffer;
-             int namelength = (int)strlen(name);
-
-             namebuffer = (char *) kmalloc(namelength + 1, GFP_KERNEL);
-             if (namebuffer == NULL)
-                     return IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
-
-             memcpy(namebuffer, name, namelength + 1);
 
Quoted 6 lines

drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c sh_css_load_blob_info(const char *fw, const struct ia_css_fw_info *bi, struct ia · line 150

Quoted 3 lines
drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c +3 -0
+             namebuffer = kstrdup(name, GFP_KERNEL);
+             if (!namebuffer)
+                     return -ENOMEM;

The patch also changes the return value in error cases. I believe the caller(s) expect to get errors in the IA_CCS_ERR_* range.

Hi,

In this particular case, the calling function just checks if it's not success defined by a enum. I think returning -ENOMEM would not effect, at least in this case.

- Hari Prasath

Quoted 10 lines

drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c sh_css_load_blob_info(const char *fw, const struct ia_css_fw_info *bi, struct ia · line 153

Quoted 4 lines
drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c +0 -0
              bd->name = fw_minibuffer[index].name = namebuffer;
      } else {
              bd->name = name;

--
Regards,

Sakari Ailus
e-mail: [email protected] XMPP: [email protected]

Signature · 2 lines
Regards,
G.E.Hari Prasath
#3 lore raw
Quoted 35 lines · hari prasath
Quoted 27 lines · Sakari Ailus

Hi Hari,

Quoted 24 lines · Hari Prasath

kstrdup kernel primitive can be used to replace kmalloc followed by string copy. This was reported by coccinelle tool

---
 .../media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c       | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c +0 -7
index 34cc56f..68db87b 100644
@@ -144,14 +144,10 @@ sh_css_load_blob_info(const char *fw, const struct ia_css_fw_info *bi, struct ia
      )
      {
              char *namebuffer;
-             int namelength = (int)strlen(name);
-
-             namebuffer = (char *) kmalloc(namelength + 1, GFP_KERNEL);
-             if (namebuffer == NULL)
-                     return IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
-
-             memcpy(namebuffer, name, namelength + 1);
 
Quoted 6 lines

drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c sh_css_load_blob_info(const char *fw, const struct ia_css_fw_info *bi, struct ia · line 150

Quoted 3 lines
drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c +3 -0
+             namebuffer = kstrdup(name, GFP_KERNEL);
+             if (!namebuffer)
+                     return -ENOMEM;

The patch also changes the return value in error cases. I believe the caller(s) expect to get errors in the IA_CCS_ERR_* range.

Quoted 5 lines

Hi,

In this particular case, the calling function just checks if it's not success defined by a enum. I think returning -ENOMEM would not effect, at least in this case.

It might not, but the function now returns both negative Posix and positive CSS error codes. The CSS error codes could well be converted to Posix but it should be done consistently and preferrably in a separate patch.

Signature · 2 lines
Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#4 lore raw
Quoted 38 lines · Sakari Ailus
Quoted 37 lines · hari prasath
Quoted 27 lines · Sakari Ailus

Hi Hari,

Quoted 24 lines · Hari Prasath

kstrdup kernel primitive can be used to replace kmalloc followed by string copy. This was reported by coccinelle tool

---
 .../media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c       | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c +0 -7
index 34cc56f..68db87b 100644
@@ -144,14 +144,10 @@ sh_css_load_blob_info(const char *fw, const struct ia_css_fw_info *bi, struct ia
      )
      {
              char *namebuffer;
-             int namelength = (int)strlen(name);
-
-             namebuffer = (char *) kmalloc(namelength + 1, GFP_KERNEL);
-             if (namebuffer == NULL)
-                     return IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
-
-             memcpy(namebuffer, name, namelength + 1);
 
Quoted 6 lines

drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c sh_css_load_blob_info(const char *fw, const struct ia_css_fw_info *bi, struct ia · line 150

Quoted 3 lines
drivers/staging/media/atomisp/pci/atomisp2/css2400/sh_css_firmware.c +3 -0
+             namebuffer = kstrdup(name, GFP_KERNEL);
+             if (!namebuffer)
+                     return -ENOMEM;

The patch also changes the return value in error cases. I believe the caller(s) expect to get errors in the IA_CCS_ERR_* range.

Hi,

Quoted 7 lines
Quoted 3 lines

In this particular case, the calling function just checks if it's not success defined by a enum. I think returning -ENOMEM would not effect, at least in this case.

It might not, but the function now returns both negative Posix and positive CSS error codes. The CSS error codes could well be converted to Posix but it should be done consistently and preferrably in a separate patch.

Hi Sakari, Thanks for your comments. I will stick with just replacing with kstrdup and retain the original error return value. I will send a v3.

Regards,
Hari

Quoted 4 lines

--
Sakari Ailus
e-mail: [email protected] XMPP: [email protected]

Signature · 2 lines
Regards,
G.E.Hari Prasath