diff mbox

[2/4] btrfs-progs: fix segment fault when exec btrfs-debug-tree as non-root

Message ID 1392864546-545-2-git-send-email-guihc.fnst@cn.fujitsu.com (mailing list archive)
State Under Review, archived
Headers show

Commit Message

Gui Hecheng Feb. 20, 2014, 2:49 a.m. UTC
When exec btrfs-debug-tree as non-root user, we get a segment fault.
Because the btrfs_scan_block_devices return a success 0 when we fail
to open a device. Now we just return the errno if this case happens.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
---
 utils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Mike Fleetwood Feb. 20, 2014, 8:48 a.m. UTC | #1
On 20 February 2014 02:49, Gui Hecheng <guihc.fnst@cn.fujitsu.com> wrote:
> When exec btrfs-debug-tree as non-root user, we get a segment fault.
> Because the btrfs_scan_block_devices return a success 0 when we fail
> to open a device. Now we just return the errno if this case happens.
>
> Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
> ---
>  utils.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/utils.c b/utils.c
> index 97e23d5..1878abc 100644
> --- a/utils.c
> +++ b/utils.c
> @@ -1517,7 +1517,8 @@ scan_again:
>                 scans++;
>                 goto scan_again;
>         }
> -       return 0;
> +
> +       return errno ? -errno : 0;
>  }
>
>  u64 parse_size(char *s)
> --
> 1.8.1.4

Hi Gui,

This strikes me as not not right because errno is only documented as
being set when open() returns -1 on failure.  In the success case
errno is not set so you can't assume it will be 0.

I think the following might work:
1) Initilase ret = 0 at the start of btrfs_scan_block_devices()
2) In the open(fullpath, O_RDONLY) failure case set ret = fd
3) return ret

Thanks,
Mike
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Gui Hecheng Feb. 20, 2014, 9:59 a.m. UTC | #2
On Thu, 2014-02-20 at 08:48 +0000, Mike Fleetwood wrote:
> On 20 February 2014 02:49, Gui Hecheng <guihc.fnst@cn.fujitsu.com> wrote:
> > When exec btrfs-debug-tree as non-root user, we get a segment fault.
> > Because the btrfs_scan_block_devices return a success 0 when we fail
> > to open a device. Now we just return the errno if this case happens.
> >
> > Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
> > ---
> >  utils.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/utils.c b/utils.c
> > index 97e23d5..1878abc 100644
> > --- a/utils.c
> > +++ b/utils.c
> > @@ -1517,7 +1517,8 @@ scan_again:
> >                 scans++;
> >                 goto scan_again;
> >         }
> > -       return 0;
> > +
> > +       return errno ? -errno : 0;
> >  }
> >
> >  u64 parse_size(char *s)
> > --
> > 1.8.1.4
> 
> Hi Gui,
> 
> This strikes me as not not right because errno is only documented as
> being set when open() returns -1 on failure.  In the success case
> errno is not set so you can't assume it will be 0.
> 
> I think the following might work:
> 1) Initilase ret = 0 at the start of btrfs_scan_block_devices()
> 2) In the open(fullpath, O_RDONLY) failure case set ret = fd
> 3) return ret
> 
> Thanks,
> Mike
Hi Mike,
Thanks for your comment, I think it is a reasonable way to fix the ret
value problem.
But I am sorry that my patch does not really address the segmentfault
problem.

Please *ignore* this patch...

-Gui
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/utils.c b/utils.c
index 97e23d5..1878abc 100644
--- a/utils.c
+++ b/utils.c
@@ -1517,7 +1517,8 @@  scan_again:
 		scans++;
 		goto scan_again;
 	}
-	return 0;
+
+	return errno ? -errno : 0;
 }
 
 u64 parse_size(char *s)