@@ -135,6 +135,8 @@ cdef class Packer(object):
135
135
else :
136
136
ret = msgpack_pack_false(& self .pk)
137
137
elif PyLong_Check(o):
138
+ # PyInt_Check(long) is True for Python 3.
139
+ # Sow we should test long before int.
138
140
if o > 0 :
139
141
ullval = o
140
142
ret = msgpack_pack_unsigned_long_long(& self .pk, ullval)
@@ -152,30 +154,41 @@ cdef class Packer(object):
152
154
dval = o
153
155
ret = msgpack_pack_double(& self .pk, dval)
154
156
elif PyBytes_Check(o):
155
- rawval = o
156
157
L = len (o)
158
+ if L > (2 ** 32 )- 1 :
159
+ raise ValueError (" bytes is too large" )
160
+ rawval = o
157
161
ret = msgpack_pack_bin(& self .pk, L)
158
162
if ret == 0 :
159
163
ret = msgpack_pack_raw_body(& self .pk, rawval, L)
160
164
elif PyUnicode_Check(o):
161
165
if not self .encoding:
162
166
raise TypeError (" Can't encode unicode string: no encoding is specified" )
163
167
o = PyUnicode_AsEncodedString(o, self .encoding, self .unicode_errors)
168
+ L = len (o)
169
+ if L > (2 ** 32 )- 1 :
170
+ raise ValueError (" dict is too large" )
164
171
rawval = o
165
172
ret = msgpack_pack_raw(& self .pk, len (o))
166
173
if ret == 0 :
167
174
ret = msgpack_pack_raw_body(& self .pk, rawval, len (o))
168
175
elif PyDict_CheckExact(o):
169
176
d = < dict > o
170
- ret = msgpack_pack_map(& self .pk, len (d))
177
+ L = len (d)
178
+ if L > (2 ** 32 )- 1 :
179
+ raise ValueError (" dict is too large" )
180
+ ret = msgpack_pack_map(& self .pk, L)
171
181
if ret == 0 :
172
182
for k, v in d.iteritems():
173
183
ret = self ._pack(k, nest_limit- 1 )
174
184
if ret != 0 : break
175
185
ret = self ._pack(v, nest_limit- 1 )
176
186
if ret != 0 : break
177
187
elif PyDict_Check(o):
178
- ret = msgpack_pack_map(& self .pk, len (o))
188
+ L = len (o)
189
+ if L > (2 ** 32 )- 1 :
190
+ raise ValueError (" dict is too large" )
191
+ ret = msgpack_pack_map(& self .pk, L)
179
192
if ret == 0 :
180
193
for k, v in o.items():
181
194
ret = self ._pack(k, nest_limit- 1 )
@@ -187,10 +200,15 @@ cdef class Packer(object):
187
200
longval = o.code
188
201
rawval = o.data
189
202
L = len (o.data)
203
+ if L > (2 ** 32 )- 1 :
204
+ raise ValueError (" EXT data is too large" )
190
205
ret = msgpack_pack_ext(& self .pk, longval, L)
191
206
ret = msgpack_pack_raw_body(& self .pk, rawval, L)
192
207
elif PyTuple_Check(o) or PyList_Check(o):
193
- ret = msgpack_pack_array(& self .pk, len (o))
208
+ L = len (o)
209
+ if L > (2 ** 32 )- 1 :
210
+ raise ValueError (" list is too large" )
211
+ ret = msgpack_pack_array(& self .pk, L)
194
212
if ret == 0 :
195
213
for v in o:
196
214
ret = self ._pack(v, nest_limit- 1 )
@@ -220,6 +238,8 @@ cdef class Packer(object):
220
238
msgpack_pack_raw_body(& self .pk, data, len (data))
221
239
222
240
def pack_array_header (self , size_t size ):
241
+ if size > (2 ** 32 - 1 ):
242
+ raise ValueError
223
243
cdef int ret = msgpack_pack_array(& self .pk, size)
224
244
if ret == - 1 :
225
245
raise MemoryError
@@ -231,6 +251,8 @@ cdef class Packer(object):
231
251
return buf
232
252
233
253
def pack_map_header (self , size_t size ):
254
+ if size > (2 ** 32 - 1 ):
255
+ raise ValueError
234
256
cdef int ret = msgpack_pack_map(& self .pk, size)
235
257
if ret == - 1 :
236
258
raise MemoryError
0 commit comments