From a1d801c37558ffee1c1baffe5c5b2ecf01eb24b4 Mon Sep 17 00:00:00 2001 From: Frank Stein Date: Tue, 14 Jul 2015 00:10:11 +0300 Subject: [PATCH 1/3] 2Gb buffer size checks fixed for Python Builder --- python/flatbuffers/builder.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/python/flatbuffers/builder.py b/python/flatbuffers/builder.py index 068c413f7..4c78d7a68 100644 --- a/python/flatbuffers/builder.py +++ b/python/flatbuffers/builder.py @@ -93,7 +93,7 @@ class Builder(object): The internal buffer is grown as needed. """ - if not (0 <= initialSize < (2**UOffsetTFlags.bytewidth - 1)): + if not (0 <= initialSize <= self.MaxBufferSize()): msg = "flatbuffers: Cannot create Builder larger than 2 gigabytes." raise BuilderSizeError(msg) @@ -104,6 +104,12 @@ class Builder(object): self.objectEnd = None self.vtables = [] + def MaxBufferSize(self): + """ + Maximum buffer size is 2Gb. + """ + return 2**31 + def Output(self): """ Output returns the portion of the buffer that has been used for @@ -238,7 +244,7 @@ class Builder(object): def growByteBuffer(self): """Doubles the size of the byteslice, and copies the old data towards the end of the new buffer (since we build the buffer backwards).""" - if not len(self.Bytes) <= 2**20: + if not len(self.Bytes) <= self.MaxBufferSize(): msg = "flatbuffers: cannot grow buffer beyond 2 gigabytes" raise BuilderSizeError(msg) From 7bcbb19569809123a14d64a0ca975a4ca5876fd8 Mon Sep 17 00:00:00 2001 From: Frank Stein Date: Fri, 7 Aug 2015 21:19:02 +0300 Subject: [PATCH 2/3] MaxBufferSize() changed to MAX_BUFFER_SIZE. Also buffer will never grow more MAX_BUFFER_SIZE. --- python/flatbuffers/builder.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/python/flatbuffers/builder.py b/python/flatbuffers/builder.py index 4c78d7a68..d59d8c4c9 100644 --- a/python/flatbuffers/builder.py +++ b/python/flatbuffers/builder.py @@ -87,13 +87,20 @@ class Builder(object): __slots__ = ("Bytes", "current_vtable", "head", "minalign", "objectEnd", "vtables") + """ + Maximum buffer size constant, in bytes. + Builder will never allow it's buffer grow over this size. + Currently equals 2Gb. + """ + MAX_BUFFER_SIZE = 2**31 + def __init__(self, initialSize): """ Initializes a Builder of size `initial_size`. The internal buffer is grown as needed. """ - if not (0 <= initialSize <= self.MaxBufferSize()): + if not (0 <= initialSize <= Builder.MAX_BUFFER_SIZE): msg = "flatbuffers: Cannot create Builder larger than 2 gigabytes." raise BuilderSizeError(msg) @@ -104,12 +111,6 @@ class Builder(object): self.objectEnd = None self.vtables = [] - def MaxBufferSize(self): - """ - Maximum buffer size is 2Gb. - """ - return 2**31 - def Output(self): """ Output returns the portion of the buffer that has been used for @@ -244,11 +245,11 @@ class Builder(object): def growByteBuffer(self): """Doubles the size of the byteslice, and copies the old data towards the end of the new buffer (since we build the buffer backwards).""" - if not len(self.Bytes) <= self.MaxBufferSize(): + if not len(self.Bytes) <= Builder.MAX_BUFFER_SIZE: msg = "flatbuffers: cannot grow buffer beyond 2 gigabytes" raise BuilderSizeError(msg) - newSize = len(self.Bytes) * 2 + newSize = min( len(self.Bytes) * 2, Builder.MAX_BUFFER_SIZE ) if newSize == 0: newSize = 1 bytes2 = bytearray(newSize) From 33e4ab65e9b2c3d6986523f803d83c695477bb38 Mon Sep 17 00:00:00 2001 From: Frank Stein Date: Fri, 14 Aug 2015 00:50:39 +0300 Subject: [PATCH 3/3] Incorrect buffer size check fixed. --- python/flatbuffers/builder.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/flatbuffers/builder.py b/python/flatbuffers/builder.py index d59d8c4c9..3e7f3cebb 100644 --- a/python/flatbuffers/builder.py +++ b/python/flatbuffers/builder.py @@ -245,11 +245,11 @@ class Builder(object): def growByteBuffer(self): """Doubles the size of the byteslice, and copies the old data towards the end of the new buffer (since we build the buffer backwards).""" - if not len(self.Bytes) <= Builder.MAX_BUFFER_SIZE: + if len(self.Bytes) == Builder.MAX_BUFFER_SIZE: msg = "flatbuffers: cannot grow buffer beyond 2 gigabytes" raise BuilderSizeError(msg) - newSize = min( len(self.Bytes) * 2, Builder.MAX_BUFFER_SIZE ) + newSize = min(len(self.Bytes) * 2, Builder.MAX_BUFFER_SIZE) if newSize == 0: newSize = 1 bytes2 = bytearray(newSize)