Skip to content

Commit 025e5f2

Browse files
committed
py/binary: Change internal bytearray typecode from 0 to 1.
The value of 0 can't be used because otherwise mp_binary_get_size will let a null byte through as the type code (intepreted as byterray). This can lead to invalid type-specifier strings being let through without an error in the struct module, and even buffer overruns.
1 parent e4ab404 commit 025e5f2

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

py/binary.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
#include "py/obj.h"
3030

3131
// Use special typecode to differentiate repr() of bytearray vs array.array('B')
32-
// (underlyingly they're same).
33-
#define BYTEARRAY_TYPECODE 0
32+
// (underlyingly they're same). Can't use 0 here because that's used to detect
33+
// type-specification errors due to end-of-string.
34+
#define BYTEARRAY_TYPECODE 1
3435

3536
size_t mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign);
3637
mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index);

tests/basics/struct2.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,30 @@
4040
struct.calcsize('0z')
4141
except:
4242
print('Exception')
43+
44+
# check that a count without a type specifier raises an exception
45+
46+
try:
47+
struct.calcsize('1')
48+
except:
49+
print('Exception')
50+
51+
try:
52+
struct.pack('1')
53+
except:
54+
print('Exception')
55+
56+
try:
57+
struct.pack_into('1', bytearray(4), 0, 'xx')
58+
except:
59+
print('Exception')
60+
61+
try:
62+
struct.unpack('1', 'xx')
63+
except:
64+
print('Exception')
65+
66+
try:
67+
struct.unpack_from('1', 'xx')
68+
except:
69+
print('Exception')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy