summaryrefslogtreecommitdiff
path: root/shared-module/msgpack
diff options
context:
space:
mode:
authorBernhard Boser <boser@berkeley.edu>2020-12-19 19:06:43 -0800
committerBernhard Boser <boser@berkeley.edu>2020-12-19 19:06:43 -0800
commitc875d7c22d447b9a29bcef62af7b46c902684d72 (patch)
treea024b2080d1eeffd9f73c294ead1644cdb6805e5 /shared-module/msgpack
parentd29184b5a0fb5c54663d48a8e8906564c3d518ad (diff)
speedup pack_bin, ext, str; catch short reads
Diffstat (limited to 'shared-module/msgpack')
-rw-r--r--shared-module/msgpack/__init__.c17
1 files changed, 7 insertions, 10 deletions
diff --git a/shared-module/msgpack/__init__.c b/shared-module/msgpack/__init__.c
index 204312109..39178d46e 100644
--- a/shared-module/msgpack/__init__.c
+++ b/shared-module/msgpack/__init__.c
@@ -67,6 +67,9 @@ STATIC void read(msgpack_stream_t *s, void *buf, mp_uint_t size) {
if (ret == 0) {
mp_raise_msg(&mp_type_EOFError, NULL);
}
+ if (ret < size) {
+ mp_raise_ValueError(translate("short read"));
+ }
}
STATIC uint8_t read1(msgpack_stream_t *s) {
@@ -92,7 +95,7 @@ STATIC uint32_t read4(msgpack_stream_t *s) {
}
STATIC size_t read_size(msgpack_stream_t *s, uint8_t len_index) {
- size_t res;
+ size_t res = 0;
switch (len_index) {
case 0: res = (size_t)read1(s); break;
case 1: res = (size_t)read2(s); break;
@@ -181,9 +184,7 @@ STATIC void pack_int(msgpack_stream_t *s, int32_t x) {
STATIC void pack_bin(msgpack_stream_t *s, const uint8_t* data, size_t len) {
write_size(s, 0xc4, len);
- for (size_t i=0; i<len; i++) {
- write1(s, data[i]);
- }
+ if (len > 0) write(s, data, len);
}
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
write_size(s, 0xc7, len);
}
write1(s, code); // type byte
- for (size_t i=0; i<len; i++) {
- write1(s, data[i]);
- }
+ if (len > 0) write(s, data, len);
}
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) {
} else {
write_size(s, 0xd9, len);
}
- for (size_t l=0; l<len; l++) {
- write1(s, str[l]);
- }
+ if (len > 0) write(s, str, len);
}
STATIC void pack_array(msgpack_stream_t *s, size_t len) {