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
This commit is contained in:
Thomas Waldmann 2016-08-12 02:55:49 +02:00
parent 4eac66fe2a
commit 7ea052a5e8

View file

@ -128,6 +128,12 @@ def _check(rv, path=None, detect_buffer_too_small=False):
if isinstance(path, int):
path = '<FD %d>' % 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])))