@@ -287,7 +287,7 @@ Py_Main(int argc, wchar_t **argv)
287287{
288288 int c ;
289289 int sts ;
290- wchar_t * command = NULL ;
290+ char * command = NULL ;
291291 wchar_t * filename = NULL ;
292292 wchar_t * module = NULL ;
293293 FILE * fp = stdin ;
@@ -299,6 +299,7 @@ Py_Main(int argc, wchar_t **argv)
299299 int version = 0 ;
300300 int saw_unbuffered_flag = 0 ;
301301 PyCompilerFlags cf ;
302+ char * oldloc ;
302303
303304 cf .cf_flags = 0 ;
304305
@@ -309,19 +310,30 @@ Py_Main(int argc, wchar_t **argv)
309310
310311 while ((c = _PyOS_GetOpt (argc , argv , PROGRAM_OPTS )) != EOF ) {
311312 if (c == 'c' ) {
312- size_t len ;
313+ size_t r1 , r2 ;
314+ oldloc = setlocale (LC_ALL , NULL );
315+ setlocale (LC_ALL , "" );
316+ r1 = wcslen (_PyOS_optarg );
317+ r2 = wcstombs (NULL , _PyOS_optarg , r1 );
318+ if (r2 == (size_t ) -1 )
319+ Py_FatalError (
320+ "cannot convert character encoding of -c argument" );
321+ if (r2 > r1 )
322+ r1 = r2 ;
323+ r1 += 2 ;
313324 /* -c is the last option; following arguments
314325 that look like options are left for the
315326 command to interpret. */
316-
317- len = wcslen (_PyOS_optarg ) + 1 + 1 ;
318- command = (wchar_t * )malloc (sizeof (wchar_t ) * len );
327+ command = (char * )malloc (r1 );
319328 if (command == NULL )
320329 Py_FatalError (
321330 "not enough memory to copy -c argument" );
322- wcscpy (command , _PyOS_optarg );
323- command [len - 2 ] = '\n' ;
324- command [len - 1 ] = 0 ;
331+ r2 = wcstombs (command , _PyOS_optarg , r1 );
332+ if (r2 > r1 - 1 )
333+ Py_FatalError (
334+ "not enough memory to copy -c argument" );
335+ strcat (command , "\n" );
336+ setlocale (LC_ALL , oldloc );
325337 break ;
326338 }
327339
@@ -531,18 +543,8 @@ Py_Main(int argc, wchar_t **argv)
531543 }
532544
533545 if (command ) {
534- PyObject * commandObj = PyUnicode_FromUnicode (
535- command , wcslen (command ));
546+ sts = PyRun_SimpleStringFlags (command , & cf ) != 0 ;
536547 free (command );
537- if (commandObj != NULL ) {
538- sts = PyRun_SimpleStringFlags (
539- _PyUnicode_AsString (commandObj ), & cf ) != 0 ;
540- }
541- else {
542- PyErr_Print ();
543- sts = 1 ;
544- }
545- Py_DECREF (commandObj );
546548 } else if (module ) {
547549 sts = RunModule (module , 1 );
548550 }
0 commit comments