@@ -67,6 +67,9 @@ STATIC void read(msgpack_stream_t *s, void *buf, mp_uint_t size) {
67
67
if (ret == 0 ) {
68
68
mp_raise_msg (& mp_type_EOFError , NULL );
69
69
}
70
+ if (ret < size ) {
71
+ mp_raise_ValueError (translate ("short read" ));
72
+ }
70
73
}
71
74
72
75
STATIC uint8_t read1 (msgpack_stream_t * s ) {
@@ -92,7 +95,7 @@ STATIC uint32_t read4(msgpack_stream_t *s) {
92
95
}
93
96
94
97
STATIC size_t read_size (msgpack_stream_t * s , uint8_t len_index ) {
95
- size_t res ;
98
+ size_t res = 0 ;
96
99
switch (len_index ) {
97
100
case 0 : res = (size_t )read1 (s ); break ;
98
101
case 1 : res = (size_t )read2 (s ); break ;
@@ -181,9 +184,7 @@ STATIC void pack_int(msgpack_stream_t *s, int32_t x) {
181
184
182
185
STATIC void pack_bin (msgpack_stream_t * s , const uint8_t * data , size_t len ) {
183
186
write_size (s , 0xc4 , len );
184
- for (size_t i = 0 ; i < len ; i ++ ) {
185
- write1 (s , data [i ]);
186
- }
187
+ if (len > 0 ) write (s , data , len );
187
188
}
188
189
189
190
STATIC void pack_ext (msgpack_stream_t * s , int8_t code , const uint8_t * data , size_t len ) {
@@ -201,9 +202,7 @@ STATIC void pack_ext(msgpack_stream_t *s, int8_t code, const uint8_t* data, siz
201
202
write_size (s , 0xc7 , len );
202
203
}
203
204
write1 (s , code ); // type byte
204
- for (size_t i = 0 ; i < len ; i ++ ) {
205
- write1 (s , data [i ]);
206
- }
205
+ if (len > 0 ) write (s , data , len );
207
206
}
208
207
209
208
STATIC void pack_str (msgpack_stream_t * s , const char * str , size_t len ) {
@@ -212,9 +211,7 @@ STATIC void pack_str(msgpack_stream_t *s, const char* str, size_t len) {
212
211
} else {
213
212
write_size (s , 0xd9 , len );
214
213
}
215
- for (size_t l = 0 ; l < len ; l ++ ) {
216
- write1 (s , str [l ]);
217
- }
214
+ if (len > 0 ) write (s , str , len );
218
215
}
219
216
220
217
STATIC void pack_array (msgpack_stream_t * s , size_t len ) {
0 commit comments