mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] commands: hwclock: replace strncpy with strlcpy
@ 2025-03-14 21:33 Jules Maselbas
  2025-03-17  8:42 ` Sascha Hauer
  2025-03-25 11:52 ` [PATCH v2] commands: hwclock: rework argument handling Jules Maselbas
  0 siblings, 2 replies; 5+ messages in thread
From: Jules Maselbas @ 2025-03-14 21:33 UTC (permalink / raw)
  To: barebox; +Cc: Jules Maselbas

strncpy could leave the rtc_name buffer not nul-terminated, strlcpy will
always nul-terminate the dst buffer.
Also replace the hardcoded size with sizeof(rtc_name)

Signed-off-by: Jules Maselbas <jmaselbas@zdiv.net>
---
 commands/hwclock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/commands/hwclock.c b/commands/hwclock.c
index c594e070ac..33829e4118 100644
--- a/commands/hwclock.c
+++ b/commands/hwclock.c
@@ -95,7 +95,7 @@ static int do_hwclock(int argc, char *argv[])
 
 		switch (opt) {
 		case 'f':
-			strncpy(rtc_name, optarg, 16);
+			strlcpy(rtc_name, optarg, sizeof(rtc_name));
 			break;
 		case 's':
 			memset(&stm, 0, sizeof(stm));
-- 
2.48.1




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] commands: hwclock: replace strncpy with strlcpy
  2025-03-14 21:33 [PATCH] commands: hwclock: replace strncpy with strlcpy Jules Maselbas
@ 2025-03-17  8:42 ` Sascha Hauer
  2025-03-25 11:52 ` [PATCH v2] commands: hwclock: rework argument handling Jules Maselbas
  1 sibling, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2025-03-17  8:42 UTC (permalink / raw)
  To: Jules Maselbas; +Cc: barebox

Hi Jules,

On Fri, Mar 14, 2025 at 10:33:44PM +0100, Jules Maselbas wrote:
> strncpy could leave the rtc_name buffer not nul-terminated, strlcpy will
> always nul-terminate the dst buffer.
> Also replace the hardcoded size with sizeof(rtc_name)
> 
> Signed-off-by: Jules Maselbas <jmaselbas@zdiv.net>
> ---
>  commands/hwclock.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/commands/hwclock.c b/commands/hwclock.c
> index c594e070ac..33829e4118 100644
> --- a/commands/hwclock.c
> +++ b/commands/hwclock.c
> @@ -95,7 +95,7 @@ static int do_hwclock(int argc, char *argv[])
>  
>  		switch (opt) {
>  		case 'f':
> -			strncpy(rtc_name, optarg, 16);
> +			strlcpy(rtc_name, optarg, sizeof(rtc_name));
>  			break;

rtc_name is an array for no good reason. Can we just do a

const char *rtc_name = "rtc0";

		case 'f':
			rtc_name = optarg;

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] commands: hwclock: rework argument handling
  2025-03-14 21:33 [PATCH] commands: hwclock: replace strncpy with strlcpy Jules Maselbas
  2025-03-17  8:42 ` Sascha Hauer
@ 2025-03-25 11:52 ` Jules Maselbas
  2025-03-25 13:46   ` Sascha Hauer
  2025-03-27  9:17   ` Sascha Hauer
  1 sibling, 2 replies; 5+ messages in thread
From: Jules Maselbas @ 2025-03-25 11:52 UTC (permalink / raw)
  To: barebox; +Cc: Jules Maselbas

The rtc_name doesn't need to be an array, thus the argument doesn't
have to be copied, we can simply use a pointer on optarg.
Drop the array and the string copy.

Suggested-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Jules Maselbas <jmaselbas@zdiv.net>
---
v2:
 - remove strcpy call and use a pointer instead (Sascha)

 commands/hwclock.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/commands/hwclock.c b/commands/hwclock.c
index c594e070ac..b04b34cd7c 100644
--- a/commands/hwclock.c
+++ b/commands/hwclock.c
@@ -83,7 +83,7 @@ static int do_hwclock(int argc, char *argv[])
 	struct rtc_device *r;
 	struct rtc_time tm;
 	struct rtc_time stm;
-	char rtc_name[16] = "rtc0";
+	const char *rtc_name = "rtc0";
 	char *env_name = NULL;
 	int opt;
 	int set = 0;
@@ -95,7 +95,7 @@ static int do_hwclock(int argc, char *argv[])
 
 		switch (opt) {
 		case 'f':
-			strncpy(rtc_name, optarg, 16);
+			rtc_name = optarg;
 			break;
 		case 's':
 			memset(&stm, 0, sizeof(stm));
-- 
2.48.1




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] commands: hwclock: rework argument handling
  2025-03-25 11:52 ` [PATCH v2] commands: hwclock: rework argument handling Jules Maselbas
@ 2025-03-25 13:46   ` Sascha Hauer
  2025-03-27  9:17   ` Sascha Hauer
  1 sibling, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2025-03-25 13:46 UTC (permalink / raw)
  To: barebox, Jules Maselbas


On Tue, 25 Mar 2025 12:52:26 +0100, Jules Maselbas wrote:
> The rtc_name doesn't need to be an array, thus the argument doesn't
> have to be copied, we can simply use a pointer on optarg.
> Drop the array and the string copy.
> 
> 

Applied, thanks!

[1/1] commands: hwclock: rework argument handling
      https://git.pengutronix.de/cgit/barebox/commit/?id=8c796cc0e810 (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] commands: hwclock: rework argument handling
  2025-03-25 11:52 ` [PATCH v2] commands: hwclock: rework argument handling Jules Maselbas
  2025-03-25 13:46   ` Sascha Hauer
@ 2025-03-27  9:17   ` Sascha Hauer
  1 sibling, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2025-03-27  9:17 UTC (permalink / raw)
  To: barebox, Jules Maselbas


On Tue, 25 Mar 2025 12:52:26 +0100, Jules Maselbas wrote:
> The rtc_name doesn't need to be an array, thus the argument doesn't
> have to be copied, we can simply use a pointer on optarg.
> Drop the array and the string copy.
> 
> 

Applied, thanks!

[1/1] commands: hwclock: rework argument handling
      https://git.pengutronix.de/cgit/barebox/commit/?id=7b40a21fb6fb (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-03-27  9:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-14 21:33 [PATCH] commands: hwclock: replace strncpy with strlcpy Jules Maselbas
2025-03-17  8:42 ` Sascha Hauer
2025-03-25 11:52 ` [PATCH v2] commands: hwclock: rework argument handling Jules Maselbas
2025-03-25 13:46   ` Sascha Hauer
2025-03-27  9:17   ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox