Fix Windows gdb build failure with python support

Message ID fd8731d0-1306-3329-5a5f-ac079b773c30@foss.arm.com
State New, archived
Headers

Commit Message

Thomas Preud'homme April 6, 2017, 2:28 p.m. UTC
  Hi,

GDB fails to build for Windows host with python support enabled due to
python_file's second argument being of type char * and being passed a
string litteral. This patch takes the conservative assumptions that the
function might indeed modify the character string and use a local char
array to pass the mode instead.

ChangeLog entry is as follows:

*** gdb/ChangeLog ***

2017-04-06  Thomas Preud'homme  <thomas.preudhomme@arm.com>

         * python/python.c (python_run_simple_file): Pass mode as a char
         pointer using local char array.

Testing: build for Windows with python support succeed with this patch and fails 
without.

Is this ok to commit to master?

Best regards,

Thomas
  

Comments

Pedro Alves April 6, 2017, 3:05 p.m. UTC | #1
On 04/06/2017 03:28 PM, Thomas Preudhomme wrote:
> 
> 
> GDB fails to build for Windows host with python support enabled due to

Uppercase "Python", and would be a good idea to mention that this is
Python 2.

> python_file's second argument 

It's actually PyFile_FromString's second parameter, not python_file's.
"python_file" is a reference that is being initialized from the result
of PyFile_FromString.

> being of type char * and being passed a
> string litteral. 

Typo "literal".

> This patch takes the conservative assumptions that the
> function might indeed modify the character string and use a local char
> array to pass the mode instead.

The Python API is notoriously buggy wrt to const-correctness when
it comes to string parameters.

Note that in gdb/python/python-internal.h, we have fixups for all of:

 PyObject_GetAttrString
 PyObject_HasAttrString
 PyObject_CallMethod
 PyErr_NewException
 PySys_GetObject
 PySys_SetPath
 PyGetSetDef
 PyArg_ParseTupleAndKeywords

Over the years it's been getting better.

A few of those were added just yesterday, but I didn't build Windows
with Python so I missed this one.

Now, this API in particular seems to have been removed completely
in Python 3, so there's no "it was fixed in 3.x" that we could put
in a comment.  Anyway, since this is just one single place, let's just
not bother with anything fancy.

Please just add a cast instead (and update the commit log accordingly):

  PyFile_FromString (full_path.get (), (char *) "r")

OK with that change.

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/python/python.c b/gdb/python/python.c
index cc58267b9557a9d6893f03b99d7912b5a3affdbb..590af7311ea6a43db4983a936d5f85f18f7debe0 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -348,11 +348,12 @@  python_run_simple_file (FILE *file, const char *filename)
   PyRun_SimpleFile (file, filename);
 
 #else /* _WIN32 */
+  char mode[] = "r";
 
   /* Because we have a string for a filename, and are using Python to
      open the file, we need to expand any tilde in the path first.  */
   gdb::unique_xmalloc_ptr<char> full_path (tilde_expand (filename));
-  gdbpy_ref<> python_file (PyFile_FromString (full_path.get (), "r"));
+  gdbpy_ref<> python_file (PyFile_FromString (full_path.get (), mode));
   if (python_file == NULL)
     {
       gdbpy_print_stack ();