* [PATCH] Documentation: laptops: freefall.c: Cleaning up missing null-terminate in conjunction with strncpy
@ 2014-07-27 14:40 Rickard Strandqvist
2014-07-27 19:29 ` Pavel Machek
2014-07-27 23:03 ` Randy Dunlap
0 siblings, 2 replies; 7+ messages in thread
From: Rickard Strandqvist @ 2014-07-27 14:40 UTC (permalink / raw)
To: Randy Dunlap, Rickard Strandqvist
Cc: Pavel Machek, Pali Rohár, Stefan Weil, Jiri Kosina,
linux-doc, linux-kernel
Added a guaranteed null-terminate after call to strncpy.
Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
---
Documentation/laptops/freefall.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/Documentation/laptops/freefall.c b/Documentation/laptops/freefall.c
index aab2ff0..113d004 100644
--- a/Documentation/laptops/freefall.c
+++ b/Documentation/laptops/freefall.c
@@ -33,8 +33,10 @@ static int set_unload_heads_path(char *device)
if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
return -EINVAL;
- strncpy(devname, device + 5, sizeof(devname) - 1);
- strncpy(device_path, device, sizeof(device_path) - 1);
+ strncpy(devname, device + 5, sizeof(devname));
+ devname[sizeof(devname) - 1] = '\0';
+ strncpy(device_path, device, sizeof(device_path));
+ device_path[sizeof(device_path) - 1] = '\0';
snprintf(unload_heads_path, sizeof(unload_heads_path) - 1,
"/sys/block/%s/device/unload_heads", devname);
--
1.7.10.4
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] Documentation: laptops: freefall.c: Cleaning up missing null-terminate in conjunction with strncpy
2014-07-27 14:40 [PATCH] Documentation: laptops: freefall.c: Cleaning up missing null-terminate in conjunction with strncpy Rickard Strandqvist
@ 2014-07-27 19:29 ` Pavel Machek
2014-07-27 23:03 ` Randy Dunlap
1 sibling, 0 replies; 7+ messages in thread
From: Pavel Machek @ 2014-07-27 19:29 UTC (permalink / raw)
To: Rickard Strandqvist
Cc: Randy Dunlap, Pali Roh?r, Stefan Weil, Jiri Kosina, linux-doc,
linux-kernel
On Sun 2014-07-27 16:40:43, Rickard Strandqvist wrote:
> Added a guaranteed null-terminate after call to strncpy.
>
> Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
Acked-by: Pavel Machek <pavel@ucw.cz>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Documentation: laptops: freefall.c: Cleaning up missing null-terminate in conjunction with strncpy
2014-07-27 14:40 [PATCH] Documentation: laptops: freefall.c: Cleaning up missing null-terminate in conjunction with strncpy Rickard Strandqvist
2014-07-27 19:29 ` Pavel Machek
@ 2014-07-27 23:03 ` Randy Dunlap
2014-07-28 10:20 ` Pavel Machek
1 sibling, 1 reply; 7+ messages in thread
From: Randy Dunlap @ 2014-07-27 23:03 UTC (permalink / raw)
To: Rickard Strandqvist
Cc: Pavel Machek, Pali Rohár, Stefan Weil, Jiri Kosina,
linux-doc, linux-kernel
On 07/27/14 07:40, Rickard Strandqvist wrote:
> Added a guaranteed null-terminate after call to strncpy.
>
> Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
> ---
> Documentation/laptops/freefall.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/Documentation/laptops/freefall.c b/Documentation/laptops/freefall.c
> index aab2ff0..113d004 100644
> --- a/Documentation/laptops/freefall.c
> +++ b/Documentation/laptops/freefall.c
> @@ -33,8 +33,10 @@ static int set_unload_heads_path(char *device)
>
> if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
> return -EINVAL;
> - strncpy(devname, device + 5, sizeof(devname) - 1);
> - strncpy(device_path, device, sizeof(device_path) - 1);
Maybe I am overlooking something here, but what was wrong with
the - 1 (2 times) above? and then just add the 2 lines below that set the
last byte to '\0'?
> + strncpy(devname, device + 5, sizeof(devname));
> + devname[sizeof(devname) - 1] = '\0';
> + strncpy(device_path, device, sizeof(device_path));
> + device_path[sizeof(device_path) - 1] = '\0';
>
> snprintf(unload_heads_path, sizeof(unload_heads_path) - 1,
> "/sys/block/%s/device/unload_heads", devname);
>
--
~Randy
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Documentation: laptops: freefall.c: Cleaning up missing null-terminate in conjunction with strncpy
2014-07-27 23:03 ` Randy Dunlap
@ 2014-07-28 10:20 ` Pavel Machek
2014-07-28 11:10 ` [PATCHv2] Documentation: laptops: freefall.c: Fix missing null-termination Pavel Machek
2014-07-28 11:13 ` [PATCHv3] Documentation: laptops: freefall.c: simplify to avoid null-termination problems Pavel Machek
0 siblings, 2 replies; 7+ messages in thread
From: Pavel Machek @ 2014-07-28 10:20 UTC (permalink / raw)
To: Randy Dunlap
Cc: Rickard Strandqvist, Pali Rohár, Stefan Weil, Jiri Kosina,
linux-doc, linux-kernel
On Sun 2014-07-27 16:03:44, Randy Dunlap wrote:
> On 07/27/14 07:40, Rickard Strandqvist wrote:
> > Added a guaranteed null-terminate after call to strncpy.
> >
> > Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
> > ---
> > Documentation/laptops/freefall.c | 6 ++++--
> > 1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/Documentation/laptops/freefall.c b/Documentation/laptops/freefall.c
> > index aab2ff0..113d004 100644
> > --- a/Documentation/laptops/freefall.c
> > +++ b/Documentation/laptops/freefall.c
> > @@ -33,8 +33,10 @@ static int set_unload_heads_path(char *device)
> >
> > if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
> > return -EINVAL;
> > - strncpy(devname, device + 5, sizeof(devname) - 1);
> > - strncpy(device_path, device, sizeof(device_path) - 1);
>
> Maybe I am overlooking something here, but what was wrong with
> the - 1 (2 times) above? and then just add the 2 lines below that set the
> last byte to '\0'?
Actually device_path is static, so it will be zero-initialized and
explicit set is not neccessary AFAICT.
(devname is auto -> needs explicit set).
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCHv2] Documentation: laptops: freefall.c: Fix missing null-termination
2014-07-28 10:20 ` Pavel Machek
@ 2014-07-28 11:10 ` Pavel Machek
2014-07-28 11:13 ` [PATCHv3] Documentation: laptops: freefall.c: simplify to avoid null-termination problems Pavel Machek
1 sibling, 0 replies; 7+ messages in thread
From: Pavel Machek @ 2014-07-28 11:10 UTC (permalink / raw)
To: Randy Dunlap
Cc: Rickard Strandqvist, Pali Rohár, Stefan Weil, Jiri Kosina,
linux-doc, linux-kernel, trivial
devname is automatic variable, so it may not contain 0 at the last
position.
Reported-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
Signed-off-by: Pavel Machek <pavel@ucw.cz>
diff --git a/Documentation/laptops/freefall.c b/Documentation/laptops/freefall.c
index aab2ff0..93a7ebf 100644
--- a/Documentation/laptops/freefall.c
+++ b/Documentation/laptops/freefall.c
@@ -29,7 +29,8 @@ static const char app_name[] = "FREE FALL";
static int set_unload_heads_path(char *device)
{
- char devname[64];
+ /* static for zero termination */
+ static char devname[64];
if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
return -EINVAL;
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCHv3] Documentation: laptops: freefall.c: simplify to avoid null-termination problems
2014-07-28 10:20 ` Pavel Machek
2014-07-28 11:10 ` [PATCHv2] Documentation: laptops: freefall.c: Fix missing null-termination Pavel Machek
@ 2014-07-28 11:13 ` Pavel Machek
2014-07-29 0:17 ` Randy Dunlap
1 sibling, 1 reply; 7+ messages in thread
From: Pavel Machek @ 2014-07-28 11:13 UTC (permalink / raw)
To: Randy Dunlap
Cc: Rickard Strandqvist, Pali Rohár, Stefan Weil, Jiri Kosina,
linux-doc, linux-kernel, trivial
Copying to local variable is actually not neccessary, if all we need
to do is snprintf(). This also removes problem where devname could be
missing zero termination.
Reported-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
Signed-off-by: Pavel Machek <pavel@ucw.cz>
diff --git a/Documentation/laptops/freefall.c b/Documentation/laptops/freefall.c
index aab2ff0..5e44b20 100644
--- a/Documentation/laptops/freefall.c
+++ b/Documentation/laptops/freefall.c
@@ -29,15 +29,12 @@ static const char app_name[] = "FREE FALL";
static int set_unload_heads_path(char *device)
{
- char devname[64];
-
if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
return -EINVAL;
- strncpy(devname, device + 5, sizeof(devname) - 1);
strncpy(device_path, device, sizeof(device_path) - 1);
snprintf(unload_heads_path, sizeof(unload_heads_path) - 1,
- "/sys/block/%s/device/unload_heads", devname);
+ "/sys/block/%s/device/unload_heads", device+5);
return 0;
}
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCHv3] Documentation: laptops: freefall.c: simplify to avoid null-termination problems
2014-07-28 11:13 ` [PATCHv3] Documentation: laptops: freefall.c: simplify to avoid null-termination problems Pavel Machek
@ 2014-07-29 0:17 ` Randy Dunlap
0 siblings, 0 replies; 7+ messages in thread
From: Randy Dunlap @ 2014-07-29 0:17 UTC (permalink / raw)
To: Pavel Machek
Cc: Rickard Strandqvist, Pali Rohár, Stefan Weil, Jiri Kosina,
linux-doc, linux-kernel, trivial
On 07/28/14 04:13, Pavel Machek wrote:
>
> Copying to local variable is actually not neccessary, if all we need
> to do is snprintf(). This also removes problem where devname could be
> missing zero termination.
>
> Reported-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
> Signed-off-by: Pavel Machek <pavel@ucw.cz>
Applied. Thanks.
>
> diff --git a/Documentation/laptops/freefall.c b/Documentation/laptops/freefall.c
> index aab2ff0..5e44b20 100644
> --- a/Documentation/laptops/freefall.c
> +++ b/Documentation/laptops/freefall.c
> @@ -29,15 +29,12 @@ static const char app_name[] = "FREE FALL";
>
> static int set_unload_heads_path(char *device)
> {
> - char devname[64];
> -
> if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
> return -EINVAL;
> - strncpy(devname, device + 5, sizeof(devname) - 1);
> strncpy(device_path, device, sizeof(device_path) - 1);
>
> snprintf(unload_heads_path, sizeof(unload_heads_path) - 1,
> - "/sys/block/%s/device/unload_heads", devname);
> + "/sys/block/%s/device/unload_heads", device+5);
> return 0;
> }
>
>
>
>
--
~Randy
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2014-07-29 0:18 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-27 14:40 [PATCH] Documentation: laptops: freefall.c: Cleaning up missing null-terminate in conjunction with strncpy Rickard Strandqvist
2014-07-27 19:29 ` Pavel Machek
2014-07-27 23:03 ` Randy Dunlap
2014-07-28 10:20 ` Pavel Machek
2014-07-28 11:10 ` [PATCHv2] Documentation: laptops: freefall.c: Fix missing null-termination Pavel Machek
2014-07-28 11:13 ` [PATCHv3] Documentation: laptops: freefall.c: simplify to avoid null-termination problems Pavel Machek
2014-07-29 0:17 ` Randy Dunlap
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).