Skip to contentSkip to conversation

drivers/tty/hvc: using strlcpy instead of strncpy

PATCH In review Chen Gang 4 participants 20 messages Feb 26, 2013 – Mar 10, 2013

Subsystems (2)
lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 drivers/tty/hvc: using strlcpy instead of strncpy

when strlen pi->location_code is larger than HVCS_CLC_LENGTH + 1,

    original implementation can not let hvcsd->p_location_code NUL terminated.

so need fix it (also can simplify the code)

---
 drivers/tty/hvc/hvcs.c |    9 ++-------
 1 files changed, 2 insertions(+), 7 deletions(-)
drivers/tty/hvc/hvcs.c +2 -7
index 1956593..81e939e 100644
@@ -881,17 +881,12 @@ static struct vio_driver hvcs_vio_driver = {
 /* Only called from hvcs_get_pi please */
 static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
 {
-	int clclength;
-
 	hvcsd->p_unit_address = pi->unit_address;
 	hvcsd->p_partition_ID  = pi->partition_ID;
-	clclength = strlen(&pi->location_code[0]);
-	if (clclength > HVCS_CLC_LENGTH)
-		clclength = HVCS_CLC_LENGTH;
 
 	/* copy the null-term char too */
-	strncpy(&hvcsd->p_location_code[0],
-			&pi->location_code[0], clclength + 1);
+	strlcpy(&hvcsd->p_location_code[0],
+			&pi->location_code[0], sizeof(hvcsd->p_location_code));
 }
 
 /*
Signature · 1 line
1.7.7.6
#1 lore raw
Quoted 4 lines · Chen Gang

when strlen pi->location_code is larger than HVCS_CLC_LENGTH + 1,

    original implementation can not let hvcsd->p_location_code NUL terminated.

so need fix it (also can simplify the code)

It should never be larger because the +1 is exactly for NUL. But it is a cleanup, so why not...

Quoted 30 lines
---
 drivers/tty/hvc/hvcs.c |    9 ++-------
 1 files changed, 2 insertions(+), 7 deletions(-)
drivers/tty/hvc/hvcs.c +2 -7
index 1956593..81e939e 100644
@@ -881,17 +881,12 @@ static struct vio_driver hvcs_vio_driver = {
 /* Only called from hvcs_get_pi please */
 static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
 {
-	int clclength;
-
 	hvcsd->p_unit_address = pi->unit_address;
 	hvcsd->p_partition_ID  = pi->partition_ID;
-	clclength = strlen(&pi->location_code[0]);
-	if (clclength > HVCS_CLC_LENGTH)
-		clclength = HVCS_CLC_LENGTH;
 
 	/* copy the null-term char too */
-	strncpy(&hvcsd->p_location_code[0],
-			&pi->location_code[0], clclength + 1);
+	strlcpy(&hvcsd->p_location_code[0],
+			&pi->location_code[0], sizeof(hvcsd->p_location_code));
 }
 
 /*
Signature · 2 lines
js
suse labs
author
#2 lore raw
Quoted 8 lines · Jiri Slaby
Quoted 4 lines · Chen Gang
Quoted 4 lines

when strlen pi->location_code is larger than HVCS_CLC_LENGTH + 1,

    original implementation can not let hvcsd->p_location_code NUL terminated.

so need fix it (also can simplify the code)

It should never be larger because the +1 is exactly for NUL. But it is a cleanup, so why not...

when strlen(&pi->location_code[0]) == HVCS_CLC_LENGTH + 2
then clclength will be reset to HVCS_CLC_LENGTH.

when call strncpy, the clclength + 1 == HVCS_CLS_LENGTH + 1
but the '\0' of src buf is located at HVCS_CLS_LENGTH + 2. so no '\0' copied to dest buf.

then the dest buf will not be ended by '\0'.

is it correct ?

:-)

gchen.

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
---
 drivers/tty/hvc/hvcs.c |    9 ++-------
 1 files changed, 2 insertions(+), 7 deletions(-)
drivers/tty/hvc/hvcs.c +2 -7
index 1956593..81e939e 100644
@@ -881,17 +881,12 @@ static struct vio_driver hvcs_vio_driver = {
 /* Only called from hvcs_get_pi please */
 static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
 {
-	int clclength;
-
 	hvcsd->p_unit_address = pi->unit_address;
 	hvcsd->p_partition_ID  = pi->partition_ID;
-	clclength = strlen(&pi->location_code[0]);
-	if (clclength > HVCS_CLC_LENGTH)
-		clclength = HVCS_CLC_LENGTH;
 
 	/* copy the null-term char too */
-	strncpy(&hvcsd->p_location_code[0],
-			&pi->location_code[0], clclength + 1);
+	strlcpy(&hvcsd->p_location_code[0],
+			&pi->location_code[0], sizeof(hvcsd->p_location_code));
 }
 
 /*
Signature · 3 lines
Chen Gang

Asianux Corporation
author
#3 lore raw
Quoted 10 lines · Chen Gang
Quoted 8 lines · Jiri Slaby
Quoted 4 lines · Chen Gang
Quoted 4 lines

when strlen pi->location_code is larger than HVCS_CLC_LENGTH + 1,

    original implementation can not let hvcsd->p_location_code NUL terminated.

so need fix it (also can simplify the code)

It should never be larger because the +1 is exactly for NUL. But it is a cleanup, so why not...

Quoted 5 lines

when strlen(&pi->location_code[0]) == HVCS_CLC_LENGTH + 2
then clclength will be reset to HVCS_CLC_LENGTH.

when call strncpy, the clclength + 1 == HVCS_CLS_LENGTH + 1
but the '\0' of src buf is located at HVCS_CLS_LENGTH + 2.

but the '\0' of src buf is located at HVCS_CLS_LENGTH + 3. (not + 2)

Quoted 42 lines

so no '\0' copied to dest buf.

then the dest buf will not be ended by '\0'.

is it correct ?

:-)

gchen.

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
---
 drivers/tty/hvc/hvcs.c |    9 ++-------
 1 files changed, 2 insertions(+), 7 deletions(-)
drivers/tty/hvc/hvcs.c +2 -7
index 1956593..81e939e 100644
@@ -881,17 +881,12 @@ static struct vio_driver hvcs_vio_driver = {
 /* Only called from hvcs_get_pi please */
 static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
 {
-	int clclength;
-
 	hvcsd->p_unit_address = pi->unit_address;
 	hvcsd->p_partition_ID  = pi->partition_ID;
-	clclength = strlen(&pi->location_code[0]);
-	if (clclength > HVCS_CLC_LENGTH)
-		clclength = HVCS_CLC_LENGTH;
 
 	/* copy the null-term char too */
-	strncpy(&hvcsd->p_location_code[0],
-			&pi->location_code[0], clclength + 1);
+	strlcpy(&hvcsd->p_location_code[0],
+			&pi->location_code[0], sizeof(hvcsd->p_location_code));
 }
 
 /*
Signature · 3 lines
Chen Gang

Asianux Corporation
#4 lore raw
Quoted 4 lines · Chen Gang
Quoted 3 lines · Chen Gang
Quoted 2 lines · Jiri Slaby
Quoted 1 line · Chen Gang
Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

when strlen pi->location_code is larger than HVCS_CLC_LENGTH + 1,

    original implementation can not let hvcsd->p_location_code NUL terminated.

so need fix it (also can simplify the code)

It should never be larger because the +1 is exactly for NUL. But it is a cleanup, so why not...

when strlen(&pi->location_code[0]) == HVCS_CLC_LENGTH + 2

It cannot, pi->location_code is defined as char[HVCS_CLC_LENGTH + 1].

Signature · 2 lines
js
suse labs
author
#5 lore raw
Quoted 3 lines · Jiri Slaby
Quoted 1 line
Quoted 1 line

when strlen(&pi->location_code[0]) == HVCS_CLC_LENGTH + 2

It cannot, pi->location_code is defined as char[HVCS_CLC_LENGTH + 1].

really, it is, I did not notice it.

but I still prefer to modify it, but the patch should be changed such as:
subject: beautify code: deleting useless judging code.
comments: src buf len and dest buf len are the same, strcpy is better.
contents: using strcpy instead of strncpy, and delete judging code.

is it ok ?

BTW:
sorry for my reply is too late, and did not notify it, originally before.
I have to do some urgent things, during these days.
my father had a serious heart disease, and is in hospital. during these days, most of my time has to be in hospital. (God Bless, and thank Jesus Christ, my father is safe, now).
within my company (Asianux), I also have something to do.

excuse me:
within this week, maybe can not get my mail reply, too.

Signature · 3 lines
Chen Gang

Asianux Corporation
#6 lore raw
Quoted 7 lines · Chen Gang
Quoted 3 lines · Jiri Slaby
Quoted 1 line
Quoted 1 line

when strlen(&pi->location_code[0]) == HVCS_CLC_LENGTH + 2

It cannot, pi->location_code is defined as char[HVCS_CLC_LENGTH + 1].

really, it is, I did not notice it.

Quoted 7 lines

but I still prefer to modify it, but the patch should be changed such as:
subject: beautify code: deleting useless judging code.
comments: src buf len and dest buf len are the same, strcpy is better.
contents: using strcpy instead of strncpy, and delete judging code.

is it ok ?

Yeah.

Quoted 4 lines

BTW:
sorry for my reply is too late, and did not notify it, originally before.
I have to do some urgent things, during these days.
my father had a serious heart disease, and is in hospital.

No problem, these drivers are not so critical. Neither these code paths in them. Take care of your relatives first.

Signature · 2 lines
js
suse labs
author
#7 lore raw
Quoted 8 lines · Jiri Slaby
Quoted 7 lines · Chen Gang
Quoted 7 lines
Quoted 3 lines · Jiri Slaby
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

when strlen(&pi->location_code[0]) == HVCS_CLC_LENGTH + 2

It cannot, pi->location_code is defined as char[HVCS_CLC_LENGTH + 1].

really, it is, I did not notice it.

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

but I still prefer to modify it, but the patch should be changed such as:
subject: beautify code: deleting useless judging code.
comments: src buf len and dest buf len are the same, strcpy is better.
contents: using strcpy instead of strncpy, and delete judging code.

is it ok ?

Yeah.

I will send patch v2.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

BTW:
sorry for my reply is too late, and did not notify it, originally before.
I have to do some urgent things, during these days.
my father had a serious heart disease, and is in hospital.

No problem, these drivers are not so critical. Neither these code paths in them. Take care of your relatives first.

thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#8 lore raw

oh, this patch has integrated into next-20130307 tree.

    (commit 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b)

it seems we need a regression for this commit, then I send patch v2

is it correct ?

:-)

Quoted 31 lines · Chen Gang
Quoted 8 lines · Jiri Slaby
Quoted 7 lines · Chen Gang
Quoted 7 lines
Quoted 3 lines · Jiri Slaby
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

when strlen(&pi->location_code[0]) == HVCS_CLC_LENGTH + 2

It cannot, pi->location_code is defined as char[HVCS_CLC_LENGTH + 1].

really, it is, I did not notice it.

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

but I still prefer to modify it, but the patch should be changed such as:
subject: beautify code: deleting useless judging code.
comments: src buf len and dest buf len are the same, strcpy is better.
contents: using strcpy instead of strncpy, and delete judging code.

is it ok ?

Yeah.

I will send patch v2.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

BTW:
sorry for my reply is too late, and did not notify it, originally before.
I have to do some urgent things, during these days.
my father had a serious heart disease, and is in hospital.

No problem, these drivers are not so critical. Neither these code paths in them. Take care of your relatives first.

thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
#9 lore raw
Quoted 5 lines · Chen Gang

oh, this patch has integrated into next-20130307 tree.

    (commit 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b)

it seems we need a regression for this commit, then I send patch v2

is it correct ?

Just send a fixup patch on top of the existing upstream.

Ben.

Quoted 37 lines

:-)

Quoted 31 lines · Chen Gang
Quoted 8 lines · Jiri Slaby
Quoted 7 lines · Chen Gang
Quoted 7 lines
Quoted 3 lines · Jiri Slaby
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

when strlen(&pi->location_code[0]) == HVCS_CLC_LENGTH + 2

It cannot, pi->location_code is defined as char[HVCS_CLC_LENGTH + 1].

really, it is, I did not notice it.

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

but I still prefer to modify it, but the patch should be changed such as:
subject: beautify code: deleting useless judging code.
comments: src buf len and dest buf len are the same, strcpy is better.
contents: using strcpy instead of strncpy, and delete judging code.

is it ok ?

Yeah.

I will send patch v2.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

BTW:
sorry for my reply is too late, and did not notify it, originally before.
I have to do some urgent things, during these days.
my father had a serious heart disease, and is in hospital.

No problem, these drivers are not so critical. Neither these code paths in them. Take care of your relatives first.

thanks.

author
#10 lore raw
Quoted 7 lines · Benjamin Herrenschmidt
Quoted 5 lines · Chen Gang
Quoted 5 lines

oh, this patch has integrated into next-20130307 tree.

    (commit 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b)

it seems we need a regression for this commit, then I send patch v2

is it correct ?

Just send a fixup patch on top of the existing upstream.

ok, thanks.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#11 lore raw

PATCH 1/1 drivers/tty/hvc: fixup original commit: 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b

In reply to Benjamin Herrenschmidt

originally I did not notice src buf len and dest buf len are the same.
so origianlly, it is not a bug issue, it is only for beautify code. and now, using strcpy is better.

---
 drivers/tty/hvc/hvcs.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/tty/hvc/hvcs.c +0 -0
index 81e939e..9330a4b 100644
@@ -885,8 +885,7 @@ static void hvcs_set_pi(struct hvcs_partner_info

*pi, struct hvcs_struct *hvcsd)
hvcsd->p_partition_ID = pi->partition_ID;

/* copy the null-term char too */
- strlcpy(&hvcsd->p_location_code[0],
- &pi->location_code[0], sizeof(hvcsd->p_location_code));
+ strcpy(&hvcsd->p_location_code[0], &pi->location_code[0]);
}

/*

Signature · 1 line
1.7.7.6
#12 lore raw

Re: [PATCH] drivers/tty/hvc: fixup original commit: 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b

Quoted 3 lines · Chen Gang

originally I did not notice src buf len and dest buf len are the same.
so origianlly, it is not a bug issue, it is only for beautify code. and now, using strcpy is better.

Being the same len doesn't mean it's safe to use strcpy ... the source might be missing the 0 terminator. In this specific case though, I believe the source string comes was itself populated with strlcpy (at least since your patch 6b6680c4ea3952af8ae76915cbca41245147741b) so strcpy is indeed safe but using strlcpy doesn't hurt does it ?

Ben.

Quoted 21 lines
---
 drivers/tty/hvc/hvcs.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/tty/hvc/hvcs.c +1 -2
index 81e939e..9330a4b 100644
@@ -885,8 +885,7 @@ static void hvcs_set_pi(struct hvcs_partner_info
 *pi, struct hvcs_struct *hvcsd)
 	hvcsd->p_partition_ID  = pi->partition_ID;
 
 	/* copy the null-term char too */
-	strlcpy(&hvcsd->p_location_code[0],
-			&pi->location_code[0], sizeof(hvcsd->p_location_code));
+	strcpy(&hvcsd->p_location_code[0], &pi->location_code[0]);
 }
 

/*

author
#13 lore raw

Re: [PATCH] drivers/tty/hvc: fixup original commit: 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b

Quoted 1 line · Benjamin Herrenschmidt

strcpy is indeed safe but using strlcpy doesn't hurt does it ?

really it is: using strlcpy doesn't hurt.

the comments and subject of original commit are not quite precision:
it is not for a bug issue (originally I say it is for bug issue) it is really for beautify code.

can I send a fixup patch only for the comments ?

thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
#14 lore raw

Re: [PATCH] drivers/tty/hvc: fixup original commit: 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b

Quoted 7 lines · Chen Gang

really it is: using strlcpy doesn't hurt.

the comments and subject of original commit are not quite precision:
it is not for a bug issue (originally I say it is for bug issue) it is really for beautify code.

can I send a fixup patch only for the comments ?

No just drop it, it's fine as it is.

Ben.

author
#15 lore raw

Re: [PATCH] drivers/tty/hvc: fixup original commit: 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b

Quoted 9 lines · Benjamin Herrenschmidt
Quoted 7 lines · Chen Gang
Quoted 7 lines

really it is: using strlcpy doesn't hurt.

the comments and subject of original commit are not quite precision:
it is not for a bug issue (originally I say it is for bug issue) it is really for beautify code.

can I send a fixup patch only for the comments ?

No just drop it, it's fine as it is.

I guess your meaning is:
not need additional fix up patch (just drop it). original commit 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b is valid
(not need drop the original commit)

is it correct ?

thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
#16 lore raw

Re: [PATCH] drivers/tty/hvc: fixup original commit: 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b

Quoted 1 line · Chen Gang

is it correct ?

Yes, the code is fine as it is now.

Ben.

author
#17 lore raw

Re: [PATCH] drivers/tty/hvc: fixup original commit: 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b

Quoted 1 line · Benjamin Herrenschmidt

Yes, the code is fine as it is now.

ok, thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
#18 lore raw

RE: [PATCH] drivers/tty/hvc: fixup original commit: 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b

In reply to Benjamin Herrenschmidt
Quoted 2 lines · Chen Gang
Quoted 1 line

originally I did not notice src buf len and dest buf len are the =

same.

Quoted 1 line
Quoted 1 line

so origianlly, it is not a bug issue, it is only for beautify =

code.

Quoted 6 lines
Quoted 1 line

and now, using strcpy is better.

=20
Being the same len doesn't mean it's safe to use strcpy ... the source might be missing the 0 terminator. In this specific case though, I believe the source string comes was itself populated with strlcpy (at least since your patch 6b6680c4ea3952af8ae76915cbca41245147741b) =

so

Quoted 1 line

strcpy is indeed safe but using strlcpy doesn't hurt does it ?

Using strlcpy() also stops someone else having to check it again in a few years time.

David

author
#19 lore raw

Re: [PATCH] drivers/tty/hvc: fixup original commit: 9276dfd27897a0b29d8b5814f39a1f82f56b6b6b

Quoted 2 lines · David Laight

Using strlcpy() also stops someone else having to check it again in a few years time.

yes.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation