Skip to content

Commit 626394e

Browse files
authored
gh-159739: Handle interpreter configuration encoding errors (#159747)
Propagate UTF-8 conversion errors before copying a string configuration value.
1 parent 9e262af commit 626394e

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid a crash when an interpreter configuration string cannot be encoded as
2+
UTF-8.

‎Python/interpconfig.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ _config_dict_copy_str(PyObject *dict, const char *name,
133133
config_dict_invalid_type(name);
134134
return -1;
135135
}
136-
strncpy(buf, PyUnicode_AsUTF8(item), bufsize-1);
136+
const char *str = PyUnicode_AsUTF8(item);
137+
if (str == NULL) {
138+
Py_DECREF(item);
139+
return -1;
140+
}
141+
strncpy(buf, str, bufsize-1);
137142
buf[bufsize-1] = '\0';
138143
Py_DECREF(item);
139144
return 0;

0 commit comments

Comments
 (0)