public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
* [Bug python/18779] New: collection of issues handling C strings in python
@ 2015-08-06 17:59 dje at google dot com
  2021-01-15 14:43 ` [Bug python/18779] " ssbssa at sourceware dot org
  2022-06-12 16:16 ` tromey at sourceware dot org
  0 siblings, 2 replies; 3+ messages in thread
From: dje at google dot com @ 2015-08-06 17:59 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=18779

            Bug ID: 18779
           Summary: collection of issues handling C strings in python
           Product: gdb
           Version: unknown
            Status: NEW
          Severity: normal
          Priority: P2
         Component: python
          Assignee: unassigned at sourceware dot org
          Reporter: dje at google dot com
  Target Milestone: ---

Wanting to extract a nul-terminated string from memory given just an ELF symbol
I ran into a few warts/bugs.
This bug is to record what I find, though I may not list them all in this
initial report (I *could* file a separate report per bug, but I'm not sure how
many there will be and since they're all related I wanted to keep them
together).

Given:

const char string[] = "hello";

bug #1:

(gdb) py print
gdb.parse_and_eval("&string[0]").string(length=7,encoding="ASCII")
Traceback (most recent call last):
  File "<string>", line 1, in <module>
gdb.MemoryError: Cannot access memory at address 0x40075c
Error while executing Python code.

One can certainly argue that's an error, but not a memory access error.

bug #2:

(gdb) py print gdb.parse_and_eval("&string[0]").lazy_string()
<gdb.LazyString object at 0x7f3444f8a750>

Question: what should str(lazy_string_object) be?
If one wants the above there is "repr".
The only useful thing to me seems to be the value returned as a python string.
IOW I expected to see "hello".

feature request #1:

IWBN if one could specify that the string is nul-terminated,
which we can by specifying a length of -1, but also specify
a maximum length (for those wanting more robustness).

maybe bug #3:

I don't have a string opinion on whether this works or not,
in c or c++, just filing this for reference sake since it seems odd:

(gdb) py print gdb.parse_and_eval("&string").string()
Traceback (most recent call last):
  File "<string>", line 1, in <module>
gdb.error: Trying to read string with inappropriate type `const char (*)[6]'.
Error while executing Python code.

maybe bug #4:

If we know the array's length, how come the lazy string length is always -1?

(gdb) pt &string
type = const char (*)[6]
(gdb) py print gdb.parse_and_eval("&string[0]").lazy_string().length
-1
(gdb) py print gdb.parse_and_eval("&string").lazy_string().length
-1
(gdb) py print gdb.parse_and_eval("string").lazy_string().length
-1

-- 
You are receiving this mail because:
You are on the CC list for the bug.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-06-12 16:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-06 17:59 [Bug python/18779] New: collection of issues handling C strings in python dje at google dot com
2021-01-15 14:43 ` [Bug python/18779] " ssbssa at sourceware dot org
2022-06-12 16:16 ` tromey at sourceware dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).