This is the mail archive of the mailing list for the GDB project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [pushed] gdb/python: exception trying to create empty array

On Tue, Jan 6, 2015 at 7:09 AM, Joel Brobecker <> wrote:
> Hello,
> The following python command fails:
>     (gdb) python print gdb.lookup_type('char').array(1, 0)
>     Traceback (most recent call last):
>       File "<string>", line 1, in <module>
>     ValueError: Array length must not be negative
>     Error while executing Python code.
> The above is trying to create an empty array, which is fairly command
> in Ada.
> gdb/ChangeLog:
>         * python/py-type.c (typy_array_1): Do not raise negative-length
>         exception if N2 is equal to N1 - 1.
> gdb/testsuite/ChangeLog:
>         * gdb.python/py-type.exp: Add a couple test about empty
>         array creation, and negative-length array creation.
> Tested on x86_64-linux, no regression.  Pushed a obvious.
> [...]
> diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
> index 54fc30f..8e82c99 100644
> --- a/gdb/python/py-type.c
> +++ b/gdb/python/py-type.c
> @@ -528,7 +528,7 @@ typy_array_1 (PyObject *self, PyObject *args, int is_vector)
>        n1 = 0;
>      }
> -  if (n2 < n1)
> +  if (n2 < n1 - 1)
>      {
>        PyErr_SetString (PyExc_ValueError,
>                        _("Array length must not be negative"));


I think it might not be immediately obvious to the reader why the test
is "n2 < n1 - 1".
[E.g, there's no
Can you add a comment?


[fwiw, I don't want to introduce thoughts of what might or might not
be an obvious
patch. I don't have a problem with checking in something one thinks is obvious.
I just read code like this and scratch my head for a bit, and wish we had more
comments explaining the why of things. A one liner

  /* Note: An empty array has n2 == n1 - 1.  */

would help this poor reader a lot.

And while not a requirement of this patch,
lookup_array_range_type could use a comment explaining what the
correct way of specifying an empty array is.
[I'm assuming that's the correct way, I haven't actually tried it. :-)]

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]