lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date:   Thu,  4 May 2023 15:57:54 +0800
From:   "Peng Fan (OSS)" <peng.fan@....nxp.com>
To:     gregkh@...uxfoundation.org, robh+dt@...nel.org,
        krzysztof.kozlowski+dt@...aro.org, rafael@...nel.org,
        andriy.shevchenko@...ux.intel.com, hdegoede@...hat.com,
        jgg@...pe.ca, saravanak@...gle.com, keescook@...omium.org,
        tglx@...utronix.de
Cc:     linux-kernel@...r.kernel.org, abel.vesa@...aro.org,
        Peng Fan <peng.fan@....com>
Subject: [PATCH] devres: provide API devm_kstrndup

From: Peng Fan <peng.fan@....com>

This patch introduces devm_kstrndup API so that the
device's driver can allocate memory and copy string.

Signed-off-by: Peng Fan <peng.fan@....com>
---
 drivers/base/devres.c  | 28 ++++++++++++++++++++++++++++
 include/linux/device.h |  1 +
 2 files changed, 29 insertions(+)

diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index 5c998cfac335..87e2965e5bab 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -963,6 +963,34 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
 }
 EXPORT_SYMBOL_GPL(devm_kstrdup);
 
+/**
+ * devm_kstrndup - Allocate resource managed space and
+ *                copy an existing string into that.
+ * @dev: Device to allocate memory for
+ * @s: the string to duplicate
+ * @max: max length to duplicate
+ * @gfp: the GFP mask used in the devm_kmalloc() call when allocating memory
+ * RETURNS:
+ * Pointer to allocated string on success, NULL on failure.
+ */
+char *devm_kstrndup(struct device *dev, const char *s, size_t max, gfp_t gfp)
+{
+	size_t len;
+	char *buf;
+
+	if (!s)
+		return NULL;
+
+	len = strnlen(s, max);
+	buf = devm_kmalloc(dev, len + 1, gfp);
+	if (buf) {
+		memcpy(buf, s, len);
+		buf[len] = '\0';
+	}
+	return buf;
+}
+EXPORT_SYMBOL_GPL(devm_kstrndup);
+
 /**
  * devm_kstrdup_const - resource managed conditional string duplication
  * @dev: device for which to duplicate the string
diff --git a/include/linux/device.h b/include/linux/device.h
index 472dd24d4823..56b092883c64 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -226,6 +226,7 @@ static inline void *devm_kcalloc(struct device *dev,
 void devm_kfree(struct device *dev, const void *p);
 char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
 const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
+char *devm_kstrndup(struct device *dev, const char *s, size_t max, gfp_t gfp) __malloc;
 void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
 	__realloc_size(3);
 
-- 
2.37.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ