From 2a488aaac1d4a7f5b48bce687adf430d24e0beb5 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Thu, 13 Jun 2019 11:01:14 -0400 Subject: [PATCH] bcachefs: fix __bch2_xattr_bcachefs_get() We were returning -ERANGE when the size of the buffer passed in was exactly the size of the xattr val Signed-off-by: Kent Overstreet --- fs/bcachefs/xattr.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/fs/bcachefs/xattr.c b/fs/bcachefs/xattr.c index 2ccf64db8147..5aeff1012f8b 100644 --- a/fs/bcachefs/xattr.c +++ b/fs/bcachefs/xattr.c @@ -387,6 +387,9 @@ static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler, bch2_inode_opts_to_opts(bch2_inode_opts_get(&inode->ei_inode)); const struct bch_option *opt; int id, inode_opt_id; + char buf[512]; + struct printbuf out = PBUF(buf); + unsigned val_len; u64 v; id = bch2_opt_lookup(name); @@ -407,23 +410,16 @@ static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler, return -ENODATA; v = bch2_opt_get_by_id(&opts, id); + bch2_opt_to_text(&out, c, opt, v, 0); - if (!buffer) { - char buf[512]; - struct printbuf out = PBUF(buf); + val_len = out.pos - buf; - bch2_opt_to_text(&out, c, opt, v, 0); + if (buffer && val_len > size) + return -ERANGE; - return out.pos - buf; - } else { - struct printbuf out = _PBUF(buffer, size); - - bch2_opt_to_text(&out, c, opt, v, 0); - - return printbuf_remaining(&out) - ? (void *) out.pos - buffer - : -ERANGE; - } + if (buffer) + memcpy(buffer, buf, val_len); + return val_len; } static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler, -- 2.25.1