platform/x86: think-lmi: Replace kstrdup() + strreplace() with kstrdup_and_replace()
PATCH v2 2/2 platform/x86: think-lmi: Use strreplace() to replace a character by nul
In reply to Andy ShevchenkoWe can replace
p = strchrnul(str, '$OLD'); *p = '\0'; with strreplace(str, '$OLD', '\0');
that does the compatible modification without a need of the temporary variable.
---
v2: rebased on top of review-hans (Hans), added tag (Ilpo)
drivers/platform/x86/think-lmi.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-)
drivers/platform/x86/think-lmi.c +4 -15
@@ -198,14 +198,6 @@ static struct think_lmi tlmi_priv;static struct class *fw_attr_class;static DEFINE_MUTEX (tlmi_mutex); -/* ------ Utility functions ------------*/ -/* Strip out CR if one is present */ -static void strip_cr (char *str) -{ -char *p =strchrnul (str,'\n' ); - *p ='\0' ; -} -/* Convert BIOS WMI error string to suitable error code */ static int tlmi_errstr_to_err (const char *errstr) {
@@ -411,7 +403,7 @@ static ssize_t current_password_store(struct kobject *kobj,strscpy (setting->password, buf, setting->maxlen);/* Strip out CR if one is present, setting password won't work if it is present */ -strip_cr (setting->password); +strreplace (setting->password,'\n' ,'\0' );return count; }
@@ -921,7 +913,7 @@ static ssize_t display_name_show(struct kobject *kobj, struct kobj_attribute *atstatic ssize_t current_value_show (struct kobject *kobj,struct kobj_attribute *attr,char *buf) {struct tlmi_attr_setting *setting =to_tlmi_attr_setting (kobj); -char *item, *value, *p; +char *item, *value;int ret; ret =tlmi_setting (setting->index, &item,LENOVO_BIOS_SETTING_GUID );
@@ -934,8 +926,7 @@ static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *a ret = -EINVAL ; else {/* On Workstations remove the Options part after the value */ - p =strchrnul (value,';' ); - *p ='\0' ; +strreplace (value,';' ,'\0' ); ret =sysfs_emit (buf,"%s\n" , value +1 ); }kfree (item);
@@ -1540,7 +1531,6 @@ static int tlmi_analyze(void)for (i =0 ; i <TLMI_SETTINGS_COUNT ; ++i) {struct tlmi_attr_setting *setting;char *item =NULL ; -char *p; tlmi_priv.setting[i] =NULL ; ret =tlmi_setting (i, &item,LENOVO_BIOS_SETTING_GUID );
@@ -1557,8 +1547,7 @@ static int tlmi_analyze(void)strreplace (item,'/' ,'\\' );/* Remove the value part */ - p =strchrnul (item,',' ); - *p ='\0' ; +strreplace (item,',' ,'\0' );/* Create a setting entry */ setting =kzalloc (sizeof (*setting),GFP_KERNEL );
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH v2 1/2 platform/x86: think-lmi: Replace kstrdup() + strreplace() with kstrdup_and_replace()
Replace open coded functionalify of kstrdup_and_replace() with a call.
---
v2: rebased on top of review-hans (Hans), added tag (Ilpo)
drivers/platform/x86/think-lmi.c | 43 +++++++++++--------------------- 1 file changed, 15 insertions(+), 28 deletions(-)
drivers/platform/x86/think-lmi.c +15 -28
@@ -15,7 +15,7 @@#include <linux/errno.h> #include <linux/fs.h> #include <linux/mutex.h> -#include <linux/string.h> +#include <linux/string_helpers.h> #include <linux/types.h> #include <linux/dmi.h> #include <linux/wmi.h>
@@ -432,13 +432,11 @@ static ssize_t new_password_store(struct kobject *kobj,if (!tlmi_priv.can_set_bios_password)return -EOPNOTSUPP ; - new_pwd =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present, setting password won't work if it is present */ + new_pwd =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_pwd)return -ENOMEM ; -/* Strip out CR if one is present, setting password won't work if it is present */ -strip_cr (new_pwd); -/* Use lock in case multiple WMI operations needed */ mutex_lock (&tlmi_mutex);
@@ -709,13 +707,11 @@ static ssize_t cert_to_password_store(struct kobject *kobj,if (!setting->signature || !setting->signature[0 ])return -EACCES ; - passwd =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + passwd =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!passwd)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (passwd); -/* Format: 'Password,Signature' */ auth_str =kasprintf (GFP_KERNEL ,"%s,%s" , passwd, setting->signature);if (!auth_str) {
@@ -765,11 +761,10 @@ static ssize_t certificate_store(struct kobject *kobj,return ret ?: count; } - new_cert =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + new_cert =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_cert)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (new_cert);if (setting->cert_installed) {/* Certificate is installed so this is an update */
@@ -817,13 +812,11 @@ static ssize_t signature_store(struct kobject *kobj,if (!tlmi_priv.certificate_support)return -EOPNOTSUPP ; - new_signature =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + new_signature =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_signature)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (new_signature); -/* Free any previous signature */ kfree (setting->signature); setting->signature = new_signature;
@@ -846,13 +839,11 @@ static ssize_t save_signature_store(struct kobject *kobj,if (!tlmi_priv.certificate_support)return -EOPNOTSUPP ; - new_signature =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + new_signature =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_signature)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (new_signature); -/* Free any previous signature */ kfree (setting->save_signature); setting->save_signature = new_signature;
@@ -992,13 +983,11 @@ static ssize_t current_value_store(struct kobject *kobj,if (tlmi_priv.save_mode ==TLMI_SAVE_BULK && tlmi_priv.reboot_required)return -EPERM ; - new_setting =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + new_setting =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_setting)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (new_setting); -/* Use lock in case multiple WMI operations needed */ mutex_lock (&tlmi_mutex);
@@ -1279,13 +1268,11 @@ static ssize_t debug_cmd_store(struct kobject *kobj, struct kobj_attribute *attrif (!tlmi_priv.can_debug_cmd)return -EOPNOTSUPP ; - new_setting =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + new_setting =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_setting)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (new_setting); -if (tlmi_priv.pwd_admin->valid && tlmi_priv.pwd_admin->password[0 ]) { auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;", tlmi_priv.pwd_admin->password,
Signature · 1 line
2.40.0.1.gaa8946217a0b
Hi Andy,
Quoted 8 lines · Andy Shevchenko
Replace open coded functionalify of
kstrdup_and_replace()with a call.Reviewed-by Ilpo Järvinen <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>---
v2: rebased on top of review-hans (Hans), added tag (Ilpo)drivers/platform/x86/think-lmi.c | 43 +++++++++++--------------------- 1 file changed, 15 insertions(+), 28 deletions(-)
Thank you for the new version.
Thank you for your patch-series, I've applied the series to my review-hans branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=review-hans
Note it will show up in my review-hans branch once I've pushed my local branch there, which might take a while.
Once I've run some tests on this branch the patches there will be added to the platform-drivers-x86/for-next branch and eventually will be included in the pdx86 pull-request to Linus for the next merge-window.
Regards,
Hans
Quoted 124 lines
drivers/platform/x86/think-lmi.c +15
-28@@ -15,7 +15,7 @@#include <linux/errno.h> #include <linux/fs.h> #include <linux/mutex.h> -#include <linux/string.h> +#include <linux/string_helpers.h> #include <linux/types.h> #include <linux/dmi.h> #include <linux/wmi.h> @@ -432,13 +432,11 @@ static ssize_t new_password_store(struct kobject *kobj,if (!tlmi_priv.can_set_bios_password)return -EOPNOTSUPP ; - new_pwd =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present, setting password won't work if it is present */ + new_pwd =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_pwd)return -ENOMEM ; -/* Strip out CR if one is present, setting password won't work if it is present */ -strip_cr (new_pwd); -/* Use lock in case multiple WMI operations needed */ mutex_lock (&tlmi_mutex);@@ -709,13 +707,11 @@ static ssize_t cert_to_password_store(struct kobject *kobj,if (!setting->signature || !setting->signature[0 ])return -EACCES ; - passwd =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + passwd =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!passwd)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (passwd); -/* Format: 'Password,Signature' */ auth_str =kasprintf (GFP_KERNEL ,"%s,%s" , passwd, setting->signature);if (!auth_str) {@@ -765,11 +761,10 @@ static ssize_t certificate_store(struct kobject *kobj,return ret ?: count; } - new_cert =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + new_cert =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_cert)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (new_cert);if (setting->cert_installed) {/* Certificate is installed so this is an update */ @@ -817,13 +812,11 @@ static ssize_t signature_store(struct kobject *kobj,if (!tlmi_priv.certificate_support)return -EOPNOTSUPP ; - new_signature =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + new_signature =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_signature)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (new_signature); -/* Free any previous signature */ kfree (setting->signature); setting->signature = new_signature;@@ -846,13 +839,11 @@ static ssize_t save_signature_store(struct kobject *kobj,if (!tlmi_priv.certificate_support)return -EOPNOTSUPP ; - new_signature =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + new_signature =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_signature)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (new_signature); -/* Free any previous signature */ kfree (setting->save_signature); setting->save_signature = new_signature;@@ -992,13 +983,11 @@ static ssize_t current_value_store(struct kobject *kobj,if (tlmi_priv.save_mode ==TLMI_SAVE_BULK && tlmi_priv.reboot_required)return -EPERM ; - new_setting =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + new_setting =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_setting)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (new_setting); -/* Use lock in case multiple WMI operations needed */ mutex_lock (&tlmi_mutex);@@ -1279,13 +1268,11 @@ static ssize_t debug_cmd_store(struct kobject *kobj, struct kobj_attribute *attrif (!tlmi_priv.can_debug_cmd)return -EOPNOTSUPP ; - new_setting =kstrdup (buf,GFP_KERNEL ); +/* Strip out CR if one is present */ + new_setting =kstrdup_and_replace (buf,'\n' ,'\0' ,GFP_KERNEL );if (!new_setting)return -ENOMEM ; -/* Strip out CR if one is present */ -strip_cr (new_setting); -if (tlmi_priv.pwd_admin->valid && tlmi_priv.pwd_admin->password[0 ]) { auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;", tlmi_priv.pwd_admin->password,