Skip to content

Commit 06667b7

Browse files
committed
More tweaks
1 parent 078a167 commit 06667b7

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

Modules/main.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,15 @@ pymain_import_readline(const PyConfig *config)
231231

232232
// #define DEBUG_DEDENT
233233

234-
/* Strip common leading whitespace utf encoded string
235-
* returns a new PyBytes object that must be deallocated
236-
*/
234+
/* Strip common leading whitespace utf encoded string */
237235
PyObject* _utf_8_bytes_dedent(PyObject *bytes){
238-
char *input_data = PyBytes_AsString(bytes);
236+
char *input_data;
237+
Py_ssize_t nchars;
239238

240-
// Security problem? what is the right way to do this?
241-
Py_ssize_t nchars = strlen(input_data);
239+
PyBytes_AsStringAndSize(bytes, &input_data, &nchars);
242240

243-
// Allocate new data for the output
244-
PyBytesObject *new_bytes = PyBytes_FromStringAndSize(NULL, nchars);
241+
// Allocate new data for the output as a copy of the input
242+
PyBytesObject *new_bytes = PyBytes_FromStringAndSize(input_data, nchars);
245243
if (new_bytes == NULL) {
246244
return NULL;
247245
}
@@ -255,13 +253,11 @@ PyObject* _utf_8_bytes_dedent(PyObject *bytes){
255253

256254
// Step 1: Find N = the common number leading whitespace chars
257255

258-
// Use the output array as a temporary buffer (because we haven't populated it yet)
259256
// so we can use the descructive strtok to tokenize the input.
260-
strcpy(new_data, input_data);
261-
262257
Py_ssize_t num_common_leading_spaces = nchars + 1;
263258

264259
// Count the number of leading spaces on each line
260+
// Use the output array as a temporary buffer (we will repopulate it later)
265261
char *line = strtok(new_data, "\n");
266262
while (line) {
267263
// Move the pointer up to the first non-space character

0 commit comments

Comments
 (0)