From 7ea052a5e80ece5fd70640c918d6bcb8f50d3a5a Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 12 Aug 2016 02:55:49 +0200 Subject: [PATCH] xattr: buffer full check for freebsd freebsd 10.2: it does not give rc < 0 and errno == ERANGE if the buffer was too small, like linux or mac OS X does. rv == buffer len might be a signal of truncation. rv > buffer len would be even worse not sure if some implementation returns the total length of the data, not just the amount put into the buffer. but as we use the returned length to "truncate" the buffer, we better make sure it is not longer than the buffer. also: freebsd listxattr memoryview len bugfix --- borg/xattr.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/borg/xattr.py b/borg/xattr.py index 0439b193c..99f7657bb 100644 --- a/borg/xattr.py +++ b/borg/xattr.py @@ -128,6 +128,12 @@ def _check(rv, path=None, detect_buffer_too_small=False): if isinstance(path, int): path = '' % path raise OSError(e, msg, path) + if detect_buffer_too_small and rv >= len(get_buffer()): + # freebsd does not error with ERANGE if the buffer is too small, + # it just fills the buffer, truncates and returns. + # so, we play sure and just assume that result is truncated if + # it happens to be a full buffer. + raise BufferTooSmallError return rv @@ -323,7 +329,7 @@ elif sys.platform.startswith('freebsd'): # pragma: freebsd only if n == 0: return [] names = [] - mv = memoryview(buf) + mv = memoryview(buf)[:n] while mv: length = mv[0] names.append(os.fsdecode(bytes(mv[1:1 + length])))