Skip to content

Commit 4212799

Browse files
jimmodpgeorge
authored andcommitted
py/qstr: Special case qstr_find_strn for empty string.
This handles the case where an empty bytes/bytearray/str could pass in NULL as the str argument (with length zero). This would result in UB in strncmp. Even though our bare-metal implementation of strncmp handles this, best to avoid it for when we're using system strncmp. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent 9be0623 commit 4212799

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

py/qstr.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) {
233233
}
234234

235235
qstr qstr_find_strn(const char *str, size_t str_len) {
236+
if (str_len == 0) {
237+
// strncmp behaviour is undefined for str==NULL.
238+
return MP_QSTR_;
239+
}
240+
236241
// work out hash of str
237242
size_t str_hash = qstr_compute_hash((const byte *)str, str_len);
238243

0 commit comments

Comments
 (0)