Util-Linux package development
 help / color / mirror / Atom feed
* [PATCH] last: Always terminate domain with '\0'
@ 2017-03-12 16:51 Tobias Stoeckmann
  2017-03-13 12:10 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Tobias Stoeckmann @ 2017-03-12 16:51 UTC (permalink / raw)
  To: util-linux

Although in a practical sense, the stack will most likely be filled
with zeros when the function is called, the C language requires an
explicit '\0' termination which strncat() does not guarantee.

While at it, I replaced strncat with strncpy because that's what the
program logic wants to do here.
---
 login-utils/last.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/login-utils/last.c b/login-utils/last.c
index 340705757..ddbdd84a3 100644
--- a/login-utils/last.c
+++ b/login-utils/last.c
@@ -507,8 +507,8 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t
 	if (r < 0) {
 		len = sizeof(p->ut_host);
 		if (len >= (int)sizeof(domain)) len = sizeof(domain) - 1;
-		domain[0] = 0;
-		strncat(domain, p->ut_host, len);
+		strncpy(domain, p->ut_host, len);
+		domain[len] = 0;
 	}
 
 
-- 
2.12.0


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

* Re: [PATCH] last: Always terminate domain with '\0'
  2017-03-12 16:51 [PATCH] last: Always terminate domain with '\0' Tobias Stoeckmann
@ 2017-03-13 12:10 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2017-03-13 12:10 UTC (permalink / raw)
  To: Tobias Stoeckmann; +Cc: util-linux

On Sun, Mar 12, 2017 at 05:51:00PM +0100, Tobias Stoeckmann wrote:
>  login-utils/last.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/login-utils/last.c b/login-utils/last.c
> index 340705757..ddbdd84a3 100644
> --- a/login-utils/last.c
> +++ b/login-utils/last.c
> @@ -507,8 +507,8 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t
>  	if (r < 0) {
>  		len = sizeof(p->ut_host);
>  		if (len >= (int)sizeof(domain)) len = sizeof(domain) - 1;
> -		domain[0] = 0;
> -		strncat(domain, p->ut_host, len);
> +		strncpy(domain, p->ut_host, len);
> +		domain[len] = 0;

 I have applied a little bit different solution, we have xstrcpy()
 where is set \0 on len-1, so let's use it.

    Karel

diff --git a/login-utils/last.c b/login-utils/last.c
index 3407057..679ea6c 100644
--- a/login-utils/last.c
+++ b/login-utils/last.c
@@ -505,10 +505,12 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t
        if (ctl->usedns || ctl->useip)
                r = dns_lookup(domain, sizeof(domain), ctl->useip, p->ut_addr_v6);
        if (r < 0) {
-               len = sizeof(p->ut_host);
-               if (len >= (int)sizeof(domain)) len = sizeof(domain) - 1;
-               domain[0] = 0;
-               strncat(domain, p->ut_host, len);
+               size_t sz = sizeof(p->ut_host);
+
+               if (sz > sizeof(domain))
+                       sz = sizeof(domain);
+
+               xstrncpy(domain, p->ut_host, sz);
        }

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

end of thread, other threads:[~2017-03-13 12:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-12 16:51 [PATCH] last: Always terminate domain with '\0' Tobias Stoeckmann
2017-03-13 12:10 ` Karel Zak

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