mailarchive of the pengutronix oss-tools mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: oss-tools@pengutronix.de
Subject: [OSS-Tools] [PATCH 3/5] libdt: use memcpy instead of strncpy
Date: Wed, 31 May 2023 17:10:13 +0200	[thread overview]
Message-ID: <20230531151015.1404262-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230531151015.1404262-1-a.fatoum@pengutronix.de>

Despite the name, GCC objects at the strncpy use in safe_strncpy on
safety grounds.  While that seems to be a false positive, we could
just be using memcpy instead and side step this altogether.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 src/dt/common.h | 34 ++++++++++++++--------------------
 1 file changed, 14 insertions(+), 20 deletions(-)

diff --git a/src/dt/common.h b/src/dt/common.h
index c3c4f53fc216..69a264cfc1a9 100644
--- a/src/dt/common.h
+++ b/src/dt/common.h
@@ -36,6 +36,12 @@ typedef uint64_t u64;
 #undef offsetof
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 
+#define min(x, y) ({				\
+	typeof(x) _min1 = (x);			\
+	typeof(y) _min2 = (y);			\
+	(void) (&_min1 == &_min2);		\
+	_min1 < _min2 ? _min1 : _min2; })
+
 struct device_d;
 
 void pr_level_set(int level);
@@ -199,14 +205,6 @@ static inline size_t DT_strlcpy(char *dest, const char *src, size_t size)
 	return ret;
 }
 
-/* Like strncpy but make sure the resulting string is always 0 terminated. */
-static inline char * safe_strncpy(char *dst, const char *src, size_t size)
-{
-	if (!size) return dst;
-	dst[--size] = '\0';
-	return strncpy(dst, src, size);
-}
-
 static inline char *xstrdup(const char *s)
 {
 	char *p = strdup(s);
@@ -415,21 +413,23 @@ static inline int dev_set_name(struct device_d *dev, const char *fmt, ...)
 {
 	char newname[MAX_DRIVER_NAME];
 	va_list vargs;
-	int err;
+	int ret;
 
 	va_start(vargs, fmt);
-	err = vsnprintf(newname, MAX_DRIVER_NAME, fmt, vargs);
+	ret = vsnprintf(newname, MAX_DRIVER_NAME, fmt, vargs);
 	va_end(vargs);
 
+	if (WARN_ON(ret < 0))
+		return ret;
+
 	/*
 	 * Copy new name into dev structure, we do this after vsnprintf call in
 	 * case old device name was in one of vargs
 	 */
-	safe_strncpy(dev->name, newname, MAX_DRIVER_NAME);
+	memcpy(dev->name, newname, min(MAX_DRIVER_NAME - 1, ret));
+	dev->name[MAX_DRIVER_NAME - 1] = '\0';
 
-	WARN_ON(err < 0);
-
-	return err;
+	return 0;
 }
 
 struct driver_d;
@@ -577,12 +577,6 @@ static inline __u32 ror32(__u32 word, unsigned int shift)
 	return (word >> shift) | (word << (32 - shift));
 }
 
-#define min(x, y) ({				\
-	typeof(x) _min1 = (x);			\
-	typeof(y) _min2 = (y);			\
-	(void) (&_min1 == &_min2);		\
-	_min1 < _min2 ? _min1 : _min2; })
-
 /*
  * Helper macros to use CONFIG_ options in C expressions. Note that
  * these only work with boolean and tristate options.
-- 
2.39.2




  parent reply	other threads:[~2023-05-31 15:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-31 15:10 [OSS-Tools] [PATCH 1/5] configure: pass -fno-strict-aliasing to GCC Ahmad Fatoum
2023-05-31 15:10 ` [OSS-Tools] [PATCH 2/5] libdt: fix issues of external function without prototype Ahmad Fatoum
2023-05-31 15:10 ` Ahmad Fatoum [this message]
2023-05-31 15:10 ` [OSS-Tools] [PATCH 4/5] libdt: don't use old-style function definition Ahmad Fatoum
2023-05-31 15:10 ` [OSS-Tools] [PATCH 5/5] barebox-state: fix use after free in error path Ahmad Fatoum
2023-06-02 12:45   ` Roland Hieber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230531151015.1404262-3-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=oss-tools@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox