File tree Expand file tree Collapse file tree 4 files changed +33
-3
lines changed Expand file tree Collapse file tree 4 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,9 @@ struct _Py_unicode_state {
76
76
77
77
extern void _PyUnicode_ClearInterned (PyInterpreterState * interp );
78
78
79
+ // Like PyUnicode_AsUTF8(), but check for embedded null characters.
80
+ extern const char * _PyUnicode_AsUTF8NoNUL (PyObject * );
81
+
79
82
80
83
#ifdef __cplusplus
81
84
}
Original file line number Diff line number Diff line change @@ -787,6 +787,19 @@ def test_issue105979(self):
787
787
self .assertIn ("Frozen object named 'x' is invalid" ,
788
788
str (cm .exception ))
789
789
790
+ def test_create_dynamic_null (self ):
791
+ with self .assertRaisesRegex (ValueError , 'embedded null character' ):
792
+ class Spec :
793
+ name = "a\x00 b"
794
+ origin = "abc"
795
+ _imp .create_dynamic (Spec ())
796
+
797
+ with self .assertRaisesRegex (ValueError , 'embedded null character' ):
798
+ class Spec2 :
799
+ name = "abc"
800
+ origin = "a\x00 b"
801
+ _imp .create_dynamic (Spec2 ())
802
+
790
803
791
804
@skip_if_dont_write_bytecode
792
805
class FilePermissionTests (unittest .TestCase ):
Original file line number Diff line number Diff line change @@ -3991,6 +3991,18 @@ PyUnicode_AsUTF8(PyObject *unicode)
3991
3991
return PyUnicode_AsUTF8AndSize (unicode , NULL );
3992
3992
}
3993
3993
3994
+ const char *
3995
+ _PyUnicode_AsUTF8NoNUL (PyObject * unicode )
3996
+ {
3997
+ Py_ssize_t size ;
3998
+ const char * s = PyUnicode_AsUTF8AndSize (unicode , & size );
3999
+ if (s && strlen (s ) != (size_t )size ) {
4000
+ PyErr_SetString (PyExc_ValueError , "embedded null character" );
4001
+ return NULL ;
4002
+ }
4003
+ return s ;
4004
+ }
4005
+
3994
4006
/*
3995
4007
PyUnicode_GetSize() has been deprecated since Python 3.3
3996
4008
because it returned length of Py_UNICODE.
Original file line number Diff line number Diff line change @@ -917,12 +917,14 @@ extensions_lock_release(void)
917
917
static void *
918
918
hashtable_key_from_2_strings (PyObject * str1 , PyObject * str2 , const char sep )
919
919
{
920
- Py_ssize_t str1_len , str2_len ;
921
- const char * str1_data = PyUnicode_AsUTF8AndSize (str1 , & str1_len );
922
- const char * str2_data = PyUnicode_AsUTF8AndSize (str2 , & str2_len );
920
+ const char * str1_data = _PyUnicode_AsUTF8NoNUL (str1 );
921
+ const char * str2_data = _PyUnicode_AsUTF8NoNUL (str2 );
923
922
if (str1_data == NULL || str2_data == NULL ) {
924
923
return NULL ;
925
924
}
925
+ Py_ssize_t str1_len = strlen (str1_data );
926
+ Py_ssize_t str2_len = strlen (str2_data );
927
+
926
928
/* Make sure sep and the NULL byte won't cause an overflow. */
927
929
assert (SIZE_MAX - str1_len - str2_len > 2 );
928
930
size_t size = str1_len + 1 + str2_len + 1 ;
You can’t perform that action at this time.
0 commit comments